本文整理汇总了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
}
}
示例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)
}
示例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()
}
}
示例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")
}
示例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)
}
}
}
示例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")
}
}