本文整理汇总了Scala中javax.crypto.KeyGenerator类的典型用法代码示例。如果您正苦于以下问题:Scala KeyGenerator类的具体用法?Scala KeyGenerator怎么用?Scala KeyGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了KeyGenerator类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: Player
//设置package包名称以及导入依赖的类
package proton.game
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.time.{Clock, LocalDateTime}
import java.util.{Base64, UUID}
import javax.crypto.spec.SecretKeySpec
import javax.crypto.{KeyGenerator, Mac}
@SerialVersionUID(1L)
class Player(val id: UUID, val name: String, val secret: String) extends Serializable {
override def hashCode = id.hashCode()
override def equals(other: Any) = other match {
case that: Player => this.id == that.id
case _ => false
}
override def toString = s"$name ($id)"
def identity = PlayerIdentity(id, name)
def isAuthorized(time: LocalDateTime, signature: String): Boolean = {
val seconds = ChronoUnit.SECONDS.between(time, LocalDateTime.now(Clock.systemUTC()))
if (seconds < -300 || seconds > 300) {
false
} else {
val secretKeySpec = new SecretKeySpec(secret.getBytes, "HmacSHA256")
val mac = Mac.getInstance("HmacSHA256")
mac.init(secretKeySpec)
val message = id.toString.toLowerCase + "|" + time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val hmac = mac.doFinal(message.getBytes("UTF-8"))
val encoded = Base64.getEncoder.encodeToString(hmac)
encoded.equalsIgnoreCase(signature)
}
}
}
object Player {
def apply(name: String) = new Player(UUID.randomUUID(), name, generateKey)
def apply(id: UUID, name: String) = new Player(id, name, generateKey)
private def generateKey: String = {
val keyGenerator: KeyGenerator = KeyGenerator.getInstance("HmacSHA256")
Base64.getEncoder.encodeToString(keyGenerator.generateKey().getEncoded)
}
def apply(id: UUID, name: String, secret: String) = new Player(id, name, secret)
def apply(identity: PlayerIdentity) = new Player(identity.id, identity.name, generateKey)
def apply(identity: PlayerIdentity, secret: String) = new Player(identity.id, identity.name, secret)
}
case class PlayerIdentity(id: UUID, name: String)
开发者ID:Morgan-Stanley,项目名称:proton,代码行数:61,代码来源:Player.scala
示例2: EncryptionContext
//设置package包名称以及导入依赖的类
package client
import java.security.{Key, PublicKey}
import javax.crypto.{KeyGenerator, Cipher}
import com.cryptoutility.protocol.crypto.{Base64Encode, Encrypt}
import play.api.Configuration
class EncryptionContext (publicKey: PublicKey, config: Configuration) {
val symmetricAlgorithm = config.getString("cipher.symmetric.algorithm").get
val asymmetricAlgorithm = config.getString("cipher.asymmetric.algorithm").get
val secretKeyAlgorithm = config.getString("cipher.symmetric.key.algorithm").get
def encrypt = Encrypt(symmetricAlgorithm, identity)(_, _)
def cipher = {
val key = secret
val c = Cipher.getInstance(symmetricAlgorithm)
c.init(Cipher.ENCRYPT_MODE, key)
(key, c)
}
def secret = KeyGenerator.getInstance(secretKeyAlgorithm).generateKey()
def encryptEncoded = Encrypt(symmetricAlgorithm, Base64Encode(_))(_, _)
def wrap = Encrypt.wrap(asymmetricAlgorithm, publicKey, Base64Encode(_))(_)
}
开发者ID:ejosiah,项目名称:crypto-utility,代码行数:29,代码来源:EncryptionContext.scala
注:本文中的javax.crypto.KeyGenerator类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论