Scala Bcrypt is a scala friendly wrapper of jBCRYPT
The safe APIs will result in scala.util.Failure
s and scala.util.Success
s when executing operations to explicitly
indicate the possibility that certain bcrypt operations can fail due to providing incorrect salt versions or number of
rounds (eg. > 30 rounds).
scala> import com.github.t3hnar.bcrypt._
import com.github.t3hnar.bcrypt._
scala> "password".bcrypt
res1: String = $2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG
scala> "password".isBcrypted("$2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG")
res2: Try[Boolean] = Success(true)
Since Try
is monadic, you can use a for-comprehension to compose operations that return Success
or Failure
with
fail-fast semantics. You can also use the desugared notation (flatMap
s and map
s) if you prefer
scala> val bcryptAndVerify = for {
bcrypted <- "hello".bcrypt(12)
result <- "hello".isBcrypted(bcrypted)
} yield result
res: Try[Boolean] = Success(true)
By default, the salt
generated internally, and developer does not need to generate and store salt.
But if you decide that you need to manage salt, you can use bcrypt
in the following way:
scala> val salt = generateSalt
salt: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwO
scala> "password".bcrypt(salt)
res3: Try[String] = Success($2a$10$8K1p/a0dL1LXMIgoEDFrwOfMQbLgtnOoKsWc.6U6H0llP3puzeeEu)
The Unsafe APIs will result in Exceptions being thrown when executing operations as certain bcrypt operations can fail due to providing incorrect salt versions or number of rounds (eg. > 30 rounds). These Unsafe APIs are present for backwards compatibility reasons and should be avoided if possible
scala> import com.github.t3hnar.bcrypt._
import com.github.t3hnar.bcrypt._
scala> "password".bcrypt
res1: String = $2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG
scala> "password".isBcrypted("$2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG")
res2: Boolean = true
scala> val salt = generateSalt
salt: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwO
scala> "password".bcrypt(salt)
res3: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwOfMQbLgtnOoKsWc.6U6H0llP3puzeeEu
libraryDependencies += "com.github.t3hnar" %% "scala-bcrypt" % "3.0"
<dependency>
<groupId>com.github.t3hnar</groupId>
<artifactId>scala-bcrypt_2.12</artifactId>
<version>3.1</version>
</dependency>