Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/572e52bb621f28e20401 to your computer and use it in GitHub Desktop.
Save rightfold/572e52bb621f28e20401 to your computer and use it in GitHub Desktop.
package hexapoda.core.interaction
import hexapoda.core.entity.User
import hexapoda.core.gateway.UserFetcher
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
class UserAuthenticator(userFetcher: UserFetcher) {
def apply(emailAddress: String, password: String) =
userFetcher.findUserByEmailAddress(emailAddress)
.filter(UserAuthenticator.checkPassword(password, _))
.map(_.id.value)
}
object UserAuthenticator {
val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
private def checkPassword(password: String, user: User) =
checkPassword(password,
user.passwordHash,
user.passwordSalt,
user.passwordIterationCount)
private def checkPassword(password: String,
passwordHash: Array[Byte],
passwordSalt: Array[Byte],
passwordIterationCount: Int) = {
val keySpec = new PBEKeySpec(password.toCharArray,
passwordSalt,
passwordIterationCount,
passwordHash.length * 8)
val hash = secretKeyFactory.generateSecret(keySpec).getEncoded
hash.toSeq == passwordHash.toSeq
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment