当前位置: 首页>>代码示例>>Scala>>正文


Scala BouncyCastleProvider类代码示例

本文整理汇总了Scala中org.bouncycastle.jce.provider.BouncyCastleProvider的典型用法代码示例。如果您正苦于以下问题:Scala BouncyCastleProvider类的具体用法?Scala BouncyCastleProvider怎么用?Scala BouncyCastleProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了BouncyCastleProvider类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Settings

//设置package包名称以及导入依赖的类
// Copyright (c) 2017 Grier Forensics. All Rights Reserved.

package com.grierforensics.greatdane.connector

import java.security.Security

import com.typesafe.config.ConfigFactory
import org.bouncycastle.jce.provider.BouncyCastleProvider

object Settings {

  private val config = {
    val cfg = ConfigFactory.load()
    cfg.getConfig("com.grierforensics.greatdane.connector")
  }

  // This must occur once, so this is a logical place to do it
  val SecurityProvider = new BouncyCastleProvider
  Security.addProvider(SecurityProvider)

  val Host: String = config.getString("host")
  val Port: Int = config.getInt("port")
  val ApiKey: String = config.getString("apiKey")

  case class ZoneFileDetails(origin: String, baseFile: String, outFile: String,
                             ttl: Long, writePeriod: Int)

  val ManageZone: Boolean = config.getBoolean("manageZone")

  val Zone = ZoneFileDetails(
    config.getString("zone.origin"),
    config.getString("zone.basefile"),
    config.getString("zone.outfile"),
    config.getLong("zone.ttl"),
    config.getInt("zone.write.period")
  )

  case class GeneratorDetails(keyAlgo: String, keyBits: Int, selfSign: Boolean,
                              signingKey: String, signingCert: String, signingAlgo: String,
                              expiryDays: Int)

  private val certs = config.getConfig("certificates")
  val Generator: Option[GeneratorDetails] = if (certs.getBoolean("generate")) {
    Some(
      GeneratorDetails(
        certs.getString("key.algorithm"),
        certs.getInt("key.bits"),

        certs.getBoolean("selfSign"),

        certs.getString("signing.key"),
        certs.getString("signing.certificate"),
        certs.getString("signing.algorithm"),

        certs.getInt("expiry.days")
      )
    )
  } else {
    None
  }
} 
开发者ID:grierforensics,项目名称:Great-DANE-Connector,代码行数:62,代码来源:Settings.scala

示例2: PublicKey

//设置package包名称以及导入依赖的类
package im.actor.server.api.rpc.service.auth

import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.{ Security, MessageDigest }
import scala.annotation.tailrec

object PublicKey {
  Security.addProvider(new BouncyCastleProvider())

  def keyHash(pk: Array[Byte]): Long = {
    val md = MessageDigest.getInstance("SHA-256")
    val buf: Array[Byte] = md.digest(pk)
    val longSize = 8

    @tailrec
    def f(res: Long, index: Int): Long = {
      if (index == longSize) res
      else {
        val n = buf(index) ^ buf(index + longSize) ^ buf(index + longSize * 2) ^ buf(index + longSize * 3)
        f(((n & 0xffL) << (longSize - index - 1) * longSize) | res, index + 1)
      }
    }
    f(0L, 0)
  }
}

trait PublicKeyHelpers {
  def keyHash(pk: Array[Byte]) = PublicKey.keyHash(pk)
} 
开发者ID:wex5,项目名称:dangchat-server,代码行数:30,代码来源:PublicHeyHelpers.scala

示例3: EllipticCurveKeyUtils

//设置package包名称以及导入依赖的类
package com.flipkart.connekt.busybees.encryption

import java.security._
import java.security.interfaces.ECPublicKey
import java.security.spec.ECPublicKeySpec
import java.util.Base64
import javax.crypto.KeyAgreement

import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.jce.spec.ECNamedCurveSpec
import org.bouncycastle.jce.{ECNamedCurveTable, ECPointUtil}

object EllipticCurveKeyUtils {

  private val cryptoTypeEcdh = "ECDH"
  private val secp256r1 = "secp256r1"
  private val keyFactory = KeyFactory.getInstance(cryptoTypeEcdh, BouncyCastleProvider.PROVIDER_NAME)
  private val ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec(secp256r1)
  private val ecNamedCurveSpec = new ECNamedCurveSpec(secp256r1, ecNamedCurveParameterSpec.getCurve, ecNamedCurveParameterSpec.getG, ecNamedCurveParameterSpec.getN)
  private val keyPairGenerator = KeyPairGenerator.getInstance(cryptoTypeEcdh, BouncyCastleProvider.PROVIDER_NAME)

  keyPairGenerator.initialize(ECNamedCurveTable.getParameterSpec(secp256r1), new SecureRandom())

  private def hexToBytes(hex: String): Array[Byte] = {
    hex.replaceAll("[^0-9A-Fa-f]", "").sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toByte)
  }

  def generateServerKeyPair(): KeyPair = keyPairGenerator.generateKeyPair()

  def publicKeyToBytes(publicKey: ECPublicKey): Array[Byte] = {
    val x = publicKey.getW.getAffineX.toString(16)
    val y = publicKey.getW.getAffineY.toString(16)
    hexToBytes("04" + "0" * (64 - x.length) + x + "0" * (64 - y.length) + y)
  }

  def loadP256Dh(p256dh: String): ECPublicKey = {
    val point = ECPointUtil.decodePoint(ecNamedCurveSpec.getCurve, Base64.getUrlDecoder.decode(p256dh))
    val pubKeySpec = new ECPublicKeySpec(point, ecNamedCurveSpec)
    keyFactory.generatePublic(pubKeySpec).asInstanceOf[ECPublicKey]
  }

  def generateSharedSecret(serverKeys: KeyPair, clientPublicKey: PublicKey): Array[Byte] = {
    val keyAgreement = KeyAgreement.getInstance(cryptoTypeEcdh, BouncyCastleProvider.PROVIDER_NAME)
    keyAgreement.init(serverKeys.getPrivate)
    keyAgreement.doPhase(clientPublicKey, true)
    keyAgreement.generateSecret()
  }
} 
开发者ID:ayush03agarwal,项目名称:connekt,代码行数:49,代码来源:EllipticCurveKeyUtils.scala

示例4: Main

//设置package包名称以及导入依赖的类
package crypto

import java.security.Security
import crypto.Crypto._
import org.bouncycastle.jce.provider.BouncyCastleProvider

object Main extends App {

  Security.addProvider(new BouncyCastleProvider())

  val rootCN = mkX500Name(
    "Weyland-Yutani Class 3 Public Primary Certification Authority - G5",
    Vector("(c) 2016 Weyland-Yutani, Inc. - For authorized use only",
      "Weyland-Yutani Trust Network"),
    "Weyland-Yutani, Inc.",
    "US"
  )
  val intCN = mkX500Name(
    "Weyland-Yutani Class 3 EV SSL CA - G3",
    Vector("Weyland-Yutani Trust Network"),
    "Weyland-Yutani Corporation",
    "US"
  )
  val clientCN = mkX500Name2(
    "*.compute-1.amazonaws.com",Vector("Enterprise IT"),
    "Weyland-Yutani, Inc",
    "1 Weyland-Yutani Way",
    "New York",
    "New York",
    "10003",
    "US",
    1
  )

  val rootKey = genRSA(RSA2048)
  val rootCert = createRootCACert(rootKey.getPublic, 1L, rootKey.getPrivate, rootCN, 365*20)
  write(rootKey.getPrivate.getEncoded, "root_rsa.der")
  write(rootKey.getPublic.getEncoded, "root_pub.der")
  write(rootCert.getEncoded, "root.der")

  val intKey = genRSA(RSA2048)
  val intCert = createCACert(intKey.getPublic, 1L, rootKey.getPrivate, rootCN, intCN, 365*2)
  write(intKey.getPrivate.getEncoded, "int_rsa.der")
  write(intKey.getPublic.getEncoded, "int_pub.der")
  write(intCert.getEncoded, "int.der")

  val clientKey = genRSA(RSA2048)
  val clientCert = createClientCert(clientCN, 1L, clientKey.getPublic, intCert, intKey.getPrivate, intKey.getPublic, "*.compute-1.amazonaws.com", Vector("localhost"), Vector("127.0.0.1"), 365*2)
  write(clientKey.getPrivate.getEncoded, "localhost_rsa.der")
  write(clientKey.getPublic.getEncoded, "localhost_pub.der")
  write(clientCert.getEncoded, "localhost.der")


} 
开发者ID:jasonmar,项目名称:CertificateAuthorityInABox,代码行数:55,代码来源:Main.scala

示例5: NanoboardCrypto

//设置package包名称以及导入依赖的类
package com.karasiq.nanoboard.encoding

import akka.util.ByteString
import org.bouncycastle.crypto.digests.{SHA256Digest, SHA512Digest}
import org.bouncycastle.crypto.{Digest, StreamCipher}
import org.bouncycastle.jce.provider.BouncyCastleProvider


private[nanoboard] object NanoboardCrypto {
  val provider = new BouncyCastleProvider

  @inline
  def sha256 = new SHA256Digest()

  @inline
  def sha512 = new SHA512Digest()

  implicit class BCDigestOps[T <: Digest](md: T) {
    def digest(data: ByteString): ByteString = {
      md.update(data.toArray, 0, data.length)
      val hash = Array.ofDim[Byte](md.getDigestSize)
      md.doFinal(hash, 0)
      ByteString(hash)
    }

    def updated(data: ByteString): T = {
      md.update(data.toArray, 0, data.length)
      md
    }
  }

  implicit class BCStreamCipherOps[T <: StreamCipher](cipher: T) {
    def process(data: ByteString): ByteString = {
      val buffer = Array.ofDim[Byte](data.length)
      val length = cipher.processBytes(data.toArray, 0, data.length, buffer, 0)
      ByteString(buffer).take(length)
    }
  }
} 
开发者ID:Karasiq,项目名称:nanoboard,代码行数:40,代码来源:NanoboardCrypto.scala

示例6: RSAKeyGenerator

//设置package包名称以及导入依赖的类
package allawala.chassis.util

import java.io.{File, FileOutputStream, OutputStreamWriter}
import java.nio.file.Paths
import java.security.{Key, KeyPair, KeyPairGenerator, Security}

import com.typesafe.scalalogging.StrictLogging
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.io.pem.{PemObject, PemWriter}


object RSAKeyGenerator extends StrictLogging {
  private val KeySize = 2048
  private val homeDir = System.getProperty("user.home")

  private class PemFile(val key: Key, val description: String) {
    private val pemObject = new PemObject(description, key.getEncoded)

    def write(file: File): Unit = {
      val pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(file)))
      try {
        pemWriter.writeObject(this.pemObject)
      }
      finally {
        pemWriter.close()
      }
    }
  }

  private def generateKeyPair(): KeyPair = {
    val generator = KeyPairGenerator.getInstance("RSA", "BC")
    generator.initialize(KeySize)
    val keyPair = generator.generateKeyPair
    keyPair
  }

  private def writePem(key: Key, description: String, filename: String): Unit = {
    val path = Paths.get(homeDir, filename)
    val file = path.toFile
    val pemFile = new PemFile(key, description)
    pemFile.write(file)
    logger.debug(s"Writing $description to $path/$filename")
  }

  def generate(): Unit = {
    Security.addProvider(new BouncyCastleProvider)

    val keyPair = generateKeyPair()
    val privateKey = keyPair.getPrivate
    val publicKey = keyPair.getPublic

    writePem(privateKey, "RSA PRIVATE KEY","id_rsa")
    writePem(publicKey, "RSA PUBLIC KEY", "id_rsa.pub")
  }
} 
开发者ID:allawala,项目名称:service-chassis,代码行数:56,代码来源:RSAKeyGenerator.scala


注:本文中的org.bouncycastle.jce.provider.BouncyCastleProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。