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


Scala PKCS8EncodedKeySpec类代码示例

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


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

示例1: Cipher

//设置package包名称以及导入依赖的类
package com.github.wildprairie.common.utils

import java.nio.file.Paths
import java.security.{PrivateKey, PublicKey}


object Cipher {
  val RSA = new Cipher("RSA")
}

class Cipher(algorithm: String) {
  import java.nio.file.Files
  import java.security.KeyFactory
  import java.security.spec.X509EncodedKeySpec
  import java.security.spec.PKCS8EncodedKeySpec

  def generatePrivate: PrivateKey = {
    val keyBytes = Files.readAllBytes(Paths.get(getClass.getResource("/private_key.der").toURI))
    val spec = new PKCS8EncodedKeySpec(keyBytes)
    val kf = KeyFactory.getInstance(algorithm)
    kf.generatePrivate(spec)
  }

  def generatePublic: PublicKey = {
    val keyBytes = Files.readAllBytes(Paths.get(getClass.getResource("/public_key.der").toURI))
    val spec = new X509EncodedKeySpec(keyBytes)
    val kf = KeyFactory.getInstance(algorithm)
    kf.generatePublic(spec)
  }

  def decrypt(privateKey: PrivateKey, input: Array[Byte]): Array[Byte] = {
    val decrypt = javax.crypto.Cipher.getInstance(algorithm)
    decrypt.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey)
    decrypt.doFinal(input)
  }
} 
开发者ID:OpenWakfu,项目名称:wildprairie,代码行数:37,代码来源:Cipher.scala

示例2: PaymentProtocolSpec

//设置package包名称以及导入依赖的类
package fr.acinq.syscoin

import java.io._
import java.security._
import java.security.cert.{CertificateFactory, X509Certificate}
import java.security.spec.PKCS8EncodedKeySpec

import com.google.protobuf.ByteString
import org.syscoin.protocols.payments.Protos.{Output, PaymentDetails, PaymentRequest}
import org.bouncycastle.util.io.pem.PemReader
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner

import scala.compat.Platform

@RunWith(classOf[JUnitRunner])
class PaymentProtocolSpec extends FlatSpec {
  val keystore = KeyStore.getInstance("JKS")
  keystore.load(classOf[PaymentProtocolSpec].getResourceAsStream("/cacerts"), null)
  val aliases = keystore.aliases()

  "Payment protocol" should "verify payment requests" in {
    val stream = classOf[PaymentProtocolSpec].getResourceAsStream("/r1411736682.syscoinpaymentrequest")
    val request = PaymentRequest.parseFrom(stream)
    val (name, publicKey, trustAnchor) = PaymentProtocol.verifySignature(request, keystore)
    assert(name === "www.syscoincore.org")

    // check that we get an exception if we attempt to modify payment details
    val details = PaymentDetails.parseFrom(request.getSerializedPaymentDetails)
    val request1 = request.toBuilder.setSerializedPaymentDetails(details.toBuilder.setPaymentUrl("foo").build().toByteString).build()
    intercept[RuntimeException] {
      PaymentProtocol.verifySignature(request1, keystore)
    }
  }
  it should "sign payment requests" in {
    val factory = CertificateFactory.getInstance("X.509")
    val cacert = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/cacert.pem")).asInstanceOf[X509Certificate]
    val servercert = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/servercert.pem")).asInstanceOf[X509Certificate]
    //val cert3 = factory.generateCertificate(classOf[PaymentProtocolSpec].getResourceAsStream("/ca-int2.crt")).asInstanceOf[X509Certificate]
    val keyPair = new PemReader(new InputStreamReader(classOf[PaymentProtocolSpec].getResourceAsStream("/serverkey.pem"))).readPemObject()
    val keyFactory = KeyFactory.getInstance("RSA")
    val key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getContent))
    keystore.setCertificateEntry("foo", cacert)
    val details = PaymentDetails.newBuilder()
      .addOutputs(Output.newBuilder().setAmount(100).setScript(ByteString.EMPTY))
      .setMemo("foo")
      .setPaymentUrl("")
      .setTime(Platform.currentTime)

    val request = PaymentRequest.newBuilder()
      .setPaymentDetailsVersion(1)
      .setSerializedPaymentDetails(details.build().toByteString)
      .build

    val request1 = PaymentProtocol.sign(request, Seq(servercert), key)
    val (name, publicKey, trustAnchor) = PaymentProtocol.verifySignature(request1, keystore)
    assert(name === "Foobar")
  }
} 
开发者ID:sidhujag,项目名称:syscoin-lib,代码行数:61,代码来源:PaymentProtocolSpec.scala

示例3: loadPkcs8PrivateKey

//设置package包名称以及导入依赖的类
package mu.node.echod.util

import java.nio.file.{Files, Paths}
import java.security.{KeyFactory, PrivateKey, PublicKey}
import java.security.spec.{PKCS8EncodedKeySpec, X509EncodedKeySpec}

import pdi.jwt.JwtAlgorithm.RS256

trait KeyUtils {

  val jwtDsa = RS256

  def loadPkcs8PrivateKey(path: String): PrivateKey = {
    val keyBytes = Files.readAllBytes(Paths.get(path))
    val spec = new PKCS8EncodedKeySpec(keyBytes)
    KeyFactory.getInstance("RSA").generatePrivate(spec)
  }

  def loadX509PublicKey(path: String): PublicKey = {
    val keyBytes = Files.readAllBytes(Paths.get(path))
    val spec = new X509EncodedKeySpec(keyBytes)
    KeyFactory.getInstance("RSA").generatePublic(spec)
  }
} 
开发者ID:vyshane,项目名称:grpc-scala-microservice-kit,代码行数:25,代码来源:KeyUtils.scala

示例4: readPkcs8EncodedKeySpec

//设置package包名称以及导入依赖的类
package com.twitter.util.security

import com.twitter.logging.Logger
import com.twitter.util.Try
import com.twitter.util.security.Pkcs8EncodedKeySpecFile._
import java.io.File
import java.security.spec.PKCS8EncodedKeySpec


  def readPkcs8EncodedKeySpec(): Try[PKCS8EncodedKeySpec] = {
    val pemFile = new PemFile(file)
    pemFile
      .readMessage(MessageType)
      .map(new PKCS8EncodedKeySpec(_))
      .onFailure(logException)
  }
}

private object Pkcs8EncodedKeySpecFile {
  private val MessageType: String = "PRIVATE KEY"

  private val log = Logger.get("com.twitter.util.security")
} 
开发者ID:lanshuijuntuan,项目名称:Java.util,代码行数:24,代码来源:Pkcs8EncodedKeySpecFile.scala

示例5: JiraApi

//设置package包名称以及导入依赖的类
package de.codecentric.ccdashboard.service.timesheet.oauth

import java.security.spec.PKCS8EncodedKeySpec
import java.security.{KeyFactory, PrivateKey}
import java.util.Base64

import com.github.scribejava.core.builder.api.DefaultApi10a
import com.github.scribejava.core.model.OAuth1RequestToken
import com.github.scribejava.core.services.{RSASha1SignatureService, SignatureService}


class JiraApi(val privateKey: String, val authorizeUrl: String, val requestTokenResource: String, val accessTokenResource: String) extends DefaultApi10a {
  def getAccessTokenEndpoint: String = accessTokenResource

  def getAuthorizationUrl(requestToken: OAuth1RequestToken): String = String.format(authorizeUrl, requestToken.getToken)

  def getRequestTokenEndpoint: String = requestTokenResource

  override def getSignatureService: SignatureService = new RSASha1SignatureService(getPrivateKey)

  private def getPrivateKey: PrivateKey = try {

    val key: Array[Byte] = Base64.getDecoder.decode(privateKey) // this is the PEM encoded PKCS#8 private key
    val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(key)
    val kf: KeyFactory = KeyFactory.getInstance("RSA")
    kf.generatePrivate(keySpec)
  }
  catch {
    case e: Exception => {
      throw new RuntimeException(e)
    }
  }
} 
开发者ID:codecentric,项目名称:ccd-timesheet-service,代码行数:34,代码来源:JiraApi.scala

示例6: PemKeyUtil

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

import java.security.{PrivateKey, PublicKey}
import java.security.spec.PKCS8EncodedKeySpec
import java.util.Base64
import java.security.spec.X509EncodedKeySpec
import java.security.KeyFactory

object PemKeyUtil {

  def decodePublicKey(pem: String): PublicKey = {
    val bytes = pemToDer(pem)
    decodePublicKey(bytes)
  }

  def decodePrivateKey(pem: String): PrivateKey = {
    val bytes = pemToDer(pem)
    decodePrivateKey(bytes)
  }

  def pemToDer(pem: String): Array[Byte] = {
    Base64.getDecoder.decode(trimBeginEnd(pem))
  }

  def trimBeginEnd(pem: String) = {
    pem.replaceAll("-----BEGIN (.*)-----", "")
      .replaceAll("-----END (.*)----", "")
      .replaceAll("\r\n", "")
      .replaceAll("\n", "")
      .trim()
  }

  def decodePublicKey(der: Array[Byte]): PublicKey = {
    val spec = new X509EncodedKeySpec(der)
    val rsa = KeyFactory.getInstance("RSA")
    rsa.generatePublic(spec)
  }

  def decodePrivateKey(der: Array[Byte]): PrivateKey = {
    val spec = new PKCS8EncodedKeySpec(der)
    val rsa = KeyFactory.getInstance("RSA")
    rsa.generatePrivate(spec)
  }

} 
开发者ID:etaty,项目名称:jwtyped,代码行数:46,代码来源:PemKeyUtil.scala


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