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