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


Scala PBEKeySpec类代码示例

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


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

示例1: PasswordHasher

//设置package包名称以及导入依赖的类
package org.packtpublishing.security

import javax.crypto.spec.PBEKeySpec
import javax.crypto.SecretKeyFactory
import java.security.SecureRandom

object PasswordHasher { 
  lazy val random = SecureRandom.getInstance("SHA1PRNG")
  
  def hash(password: String): (Array[Byte], Array[Byte]) = {
    val salt = random.generateSeed(32)
    (hash(password, salt), salt)
  }
  
  def hash(password: String, salt: Array[Byte]) = {
    val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512")
    val keySpec = new PBEKeySpec(password.toCharArray(), salt, 1000, 256)
    val secretKey = secretKeyFactory.generateSecret(keySpec)
    secretKey.getEncoded()
  }    
} 
开发者ID:allansene,项目名称:spray-scala-akka-book-catalog,代码行数:22,代码来源:PasswordHasher.scala

示例2: SecretKeyHashUtils

//设置package包名称以及导入依赖的类
package core.authentication

import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

import com.typesafe.scalalogging.StrictLogging
import core.entities.CredentialSet
import utils.StringUtils.{ByteArrayFromBase64StringExtensions, ByteArrayToBase64StringExtensions}

import scala.util.{Failure, Success, Try}


object SecretKeyHashUtils extends StrictLogging {

  private val HASH_ALGORITHM: String = "PBKDF2WithHmacSHA512"
  private val ITERATIONS: Int        = 64
  private val SECRET_KEY_LENGTH: Int = 512

  def validate(storedCredentials: CredentialSet, secret: String): Boolean = {
    val given = calculateHash(secret.toCharArray, storedCredentials.salt.fromBase64()).map(_.asBase64())
    given match {
      case Some(givenPassword) => storedCredentials.password.equals(givenPassword)
      case None                => false
    }
  }

  def generate(password: Array[Char], salt: Array[Byte]): Option[CredentialSet] = {
    val result = calculateHash(password, salt)
    result.map(pw => CredentialSet(pw.asBase64(), salt.asBase64(), HASH_ALGORITHM))
  }

  private def calculateHash(password: Array[Char],
                            salt: Array[Byte],
                            iterations: Int = ITERATIONS,
                            keyLength: Int = SECRET_KEY_LENGTH): Option[Array[Byte]] = {

    val hashCalculation = Try[Array[Byte]] {
      val keyFactory = SecretKeyFactory.getInstance(HASH_ALGORITHM)
      val keySpec    = new PBEKeySpec(password, salt, iterations, keyLength)
      val secretKey  = keyFactory.generateSecret(keySpec)
      secretKey.getEncoded()
    }

    hashCalculation match {
      case Success(value) => Some(value)
      case Failure(err)   => logger.error("Operation failed!", err); None
    }
  }
} 
开发者ID:lymr,项目名称:fun-chat,代码行数:50,代码来源:SecretKeyHashUtils.scala

示例3: User

//设置package包名称以及导入依赖的类
package com.acromancer.acromancer.user.domain

import java.time.OffsetDateTime
import java.util.UUID
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

import com.acromancer.acromancer.common.Utils
import com.acromancer.acromancer.user._

case class User(
  id: UserId,
  login: String,
  loginLowerCased: String,
  email: String,
  password: String,
  salt: String,
  createdOn: OffsetDateTime
)

object User {

  def withRandomUUID(
    login: String,
    email: String,
    plainPassword: String,
    salt: String,
    createdOn: OffsetDateTime
  ) = User(UUID.randomUUID(), login, login.toLowerCase, email, encryptPassword(plainPassword, salt), salt, createdOn)

  def encryptPassword(password: String, salt: String): String = {
    // 10k iterations takes about 10ms to encrypt a password on a 2013 MacBook
    val keySpec = new PBEKeySpec(password.toCharArray, salt.getBytes, 10000, 128)
    val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    val bytes = secretKeyFactory.generateSecret(keySpec).getEncoded
    Utils.toHex(bytes)
  }

  def passwordsMatch(plainPassword: String, user: User): Boolean = {
    Utils.constantTimeEquals(
      user.password,
      encryptPassword(plainPassword, user.salt)
    )
  }
}

case class BasicUserData(id: UserId, login: String, email: String, createdOn: OffsetDateTime)

object BasicUserData {
  def fromUser(user: User) = new BasicUserData(user.id, user.login, user.email, user.createdOn)
} 
开发者ID:GitsAndGlamour,项目名称:acromancer-game,代码行数:52,代码来源:User.scala

示例4: sha1

//设置package包名称以及导入依赖的类
package com.github.jeroenr.tepkin.util

import java.security.MessageDigest
import javax.crypto.spec.{PBEKeySpec, SecretKeySpec}
import javax.crypto.{Mac, SecretKeyFactory}

import com.github.jeroenr.bson.util.Codec

trait Crypto extends Codec {

  def sha1(value: String): String = {
    val digest = sha1(value.getBytes("UTF-8"))
    encodeBase64(digest)
  }

  def sha1(value: Array[Byte]): Array[Byte] = {
    MessageDigest.getInstance("SHA-1").digest(value)
  }

  def hmac(value: Array[Byte], key: String): Array[Byte] = {
    val signingKey = new SecretKeySpec(value, "HmacSHA1")
    val mac = Mac.getInstance("HmacSHA1")
    mac.init(signingKey)
    mac.doFinal(decodeUtf8(key))
  }

  def keyDerive(password: String, salt: Array[Byte], iterations: Int): Array[Byte] = {
    val spec = new PBEKeySpec(password.toCharArray, salt, iterations, 20 * 8 )
    val keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    keyFactory.generateSecret(spec).getEncoded
  }

  def xor(as: Array[Byte], bs: Array[Byte]): Array[Byte] = {
    as.zip(bs).map { case (a, b) => (a ^ b).toByte }
  }
}

object Crypto extends Crypto 
开发者ID:dgouyette,项目名称:tepkin,代码行数:39,代码来源:Crypto.scala

示例5: MAC_ALGORITHM

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

import java.security.MessageDigest
import javax.crypto.spec.{PBEKeySpec, SecretKeySpec}
import javax.crypto.{Mac, SecretKeyFactory}

import rere.sasl.util.{BinaryString, UTF8}

import scala.util.control.NonFatal

trait ScramShaAuthMechanism extends ScramAuthMechanism {
  def MAC_ALGORITHM: String
  def PBKDF2_KEY_LENGTH: Int
  def PBKDF2_ALGORITHM: String
  def HASH_FUNCTION_NAME: String

  def factory: SecretKeyFactory
  def log(throwable: Throwable): Unit

  final def hi(password: String, salt: BinaryString, iterations: Int): BinaryString = {
    try {
      val keySpec = new PBEKeySpec(password.toCharArray, salt, iterations, PBKDF2_KEY_LENGTH)
      factory.generateSecret(keySpec).getEncoded
    } catch {
      case NonFatal(ex) =>
        log(ex)
        throw ex
    }
  }

  final def hmac(key: BinaryString, data: String): BinaryString = {
    try {
      val mac = Mac.getInstance(MAC_ALGORITHM)
      val secretKey = new SecretKeySpec(key, MAC_ALGORITHM)
      mac.init(secretKey)
      mac.doFinal(UTF8.to(data))
    } catch {
      case NonFatal(ex) =>
        log(ex)
        throw ex
    }
  }

  final def h(data: BinaryString): BinaryString = {
    try {
      MessageDigest.getInstance(HASH_FUNCTION_NAME).digest(data)
    } catch {
      case NonFatal(ex) =>
        log(ex)
        throw ex
    }
  }
} 
开发者ID:pbaun,项目名称:rere,代码行数:54,代码来源:ScramShaAuthMechanism.scala

示例6: sanitize

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

import java.security.SecureRandom
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.{PBEKeySpec}
import javax.xml.bind.DatatypeConverter


  def sanitize(s:Array[Byte]): String = {
    DatatypeConverter.printBase64Binary(s)
  }

  def hash(password: Array[Char], salt: Array[Byte], iterations: Int): String = {
    val spec = new PBEKeySpec(password, salt, iterations, Math.min(160, salt.length*8))
    val skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    sanitize(skf.generateSecret(spec).getEncoded)
  }

  def generateSalt(length: Int): String = {
    val saltBuf = new Array[Byte] (length)
    val rng = new SecureRandom()
    rng.nextBytes(saltBuf)
    sanitize(saltBuf)
  }

  def generateKey(password: String, iterations: Int, saltLength: Int): (String, String) = {
    val salt = generateSalt(saltLength)
    val hash = this.hash(password.toCharArray, salt.getBytes("UTF-8"), iterations)
    (hash, salt)
  }
} 
开发者ID:bbohrer,项目名称:callforpartners,代码行数:32,代码来源:Password.scala

示例7: User

//设置package包名称以及导入依赖的类
package com.softwaremill.bootzooka.user.domain

import java.time.OffsetDateTime
import java.util.UUID
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

import com.softwaremill.bootzooka.common.Utils
import com.softwaremill.bootzooka.user._

case class User(
    id: UserId,
    login: String,
    loginLowerCased: String,
    email: String,
    password: String,
    salt: String,
    createdOn: OffsetDateTime
)

object User {

  def withRandomUUID(
      login: String,
      email: String,
      plainPassword: String,
      salt: String,
      createdOn: OffsetDateTime
  ) = User(UUID.randomUUID(), login, login.toLowerCase, email, encryptPassword(plainPassword, salt), salt, createdOn)

  def encryptPassword(password: String, salt: String): String = {
    // 10k iterations takes about 10ms to encrypt a password on a 2013 MacBook
    val keySpec          = new PBEKeySpec(password.toCharArray, salt.getBytes, 10000, 128)
    val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    val bytes            = secretKeyFactory.generateSecret(keySpec).getEncoded
    Utils.toHex(bytes)
  }

  def passwordsMatch(plainPassword: String, user: User): Boolean =
    Utils.constantTimeEquals(
      user.password,
      encryptPassword(plainPassword, user.salt)
    )
}

case class BasicUserData(id: UserId, login: String, email: String, createdOn: OffsetDateTime)

object BasicUserData {
  def fromUser(user: User) = new BasicUserData(user.id, user.login, user.email, user.createdOn)
} 
开发者ID:Bii03,项目名称:students-clustering,代码行数:51,代码来源:User.scala

示例8: PasswordHasher

//设置package包名称以及导入依赖的类
package me.lsbengine.api.admin.security

import java.security.SecureRandom
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

import me.lsbengine.server.BlogConfiguration

object PasswordHasher {
  private val keyLength = 512
  // Needs to be tested on the final hardware
  private val defaultIterations = BlogConfiguration.hashIterations

  def hashPassword(password: Array[Char], salt: Array[Byte], iterations: Int): Array[Byte] = {
    val skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512")
    val spec = new PBEKeySpec(password, salt, iterations, keyLength)
    val key = skf.generateSecret(spec)
    key.getEncoded
  }

  def generateSalt(): Array[Byte] = {
    val random = SecureRandom.getInstance("SHA1PRNG", "SUN")
    random.nextBytes(new Array[Byte](32))
    val salt = new Array[Byte](64)
    random.nextBytes(salt)
    salt
  }

  def hashPassword(password: String, salt: Array[Byte], iterations: Int = defaultIterations): Array[Byte] = {
    hashPassword(password.toCharArray, salt, iterations)
  }
} 
开发者ID:jrabasco,项目名称:LSBEngine,代码行数:33,代码来源:PasswordHasher.scala

示例9: User

//设置package包名称以及导入依赖的类
package northendlink.user.domain

import java.time.OffsetDateTime
import java.util.UUID
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

import northendlink.common.Utils
import northendlink.user._

case class User(
  id: UserId,
  login: String,
  loginLowerCased: String,
  email: String,
  password: String,
  salt: String,
  createdOn: OffsetDateTime
)

object User {

  def withRandomUUID(
    login: String,
    email: String,
    plainPassword: String,
    salt: String,
    createdOn: OffsetDateTime
  ) = User(UUID.randomUUID(), login, login.toLowerCase, email, encryptPassword(plainPassword, salt), salt, createdOn)

  def encryptPassword(password: String, salt: String): String = {
    // 10k iterations takes about 10ms to encrypt a password on a 2013 MacBook
    val keySpec = new PBEKeySpec(password.toCharArray, salt.getBytes, 10000, 128)
    val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    val bytes = secretKeyFactory.generateSecret(keySpec).getEncoded
    Utils.toHex(bytes)
  }

  def passwordsMatch(plainPassword: String, user: User): Boolean = {
    user.password.equals(encryptPassword(plainPassword, user.salt))
  }
}

case class BasicUserData(id: UserId, login: String, email: String, createdOn: OffsetDateTime)

object BasicUserData {
  def fromUser(user: User) = new BasicUserData(user.id, user.login, user.email, user.createdOn)
} 
开发者ID:OOSINT,项目名称:NorthendLink-Site,代码行数:49,代码来源:User.scala


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