https://github.com/t3hnar/scala-bcrypt
Scala wrapper for jBcrypt + pom.xml inside
https://github.com/t3hnar/scala-bcrypt
bcrypt scala
Last synced: 4 days ago
JSON representation
Scala wrapper for jBcrypt + pom.xml inside
- Host: GitHub
- URL: https://github.com/t3hnar/scala-bcrypt
- Owner: t3hnar
- License: other
- Created: 2011-11-30T08:16:11.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T10:41:59.000Z (about 2 years ago)
- Last Synced: 2025-06-07T23:53:07.714Z (7 months ago)
- Topics: bcrypt, scala
- Language: Scala
- Homepage:
- Size: 103 KB
- Stars: 152
- Watchers: 13
- Forks: 29
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scala Bcrypt [](http://travis-ci.org/t3hnar/scala-bcrypt) [](https://coveralls.io/r/t3hnar/scala-bcrypt) [](https://www.codacy.com/app/evolution-gaming/scala-bcrypt?utm_source=github.com&utm_medium=referral&utm_content=t3hnar/scala-bcrypt&utm_campaign=Badge_Grade) [](http://search.maven.org/#search%7Cga%7C1%7Cg%3Acom.github.t3hnar%20AND%20scala-bcrypt)
Scala Bcrypt is a scala friendly wrapper of [jBCRYPT](http://www.mindrot.org/projects/jBCrypt/)
## Examples
### Safe APIs
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).
#### Encrypt password
```scala
scala> import com.github.t3hnar.bcrypt._
import com.github.t3hnar.bcrypt._
scala> "password".bcryptSafeBounded
res1: Try[String] = Success($2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG)
```
#### Validate password
```scala
scala> "password".isBcryptedSafeBounded("$2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG")
res2: Try[Boolean] = Success(true)
```
#### Composition
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
scala> val bcryptAndVerify = for {
bcrypted <- "hello".bcryptBounded(12)
result <- "hello".isBcryptedSafeBounded(bcrypted)
} yield result
res: Try[Boolean] = Success(true)
```
#### Advanced usage
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
scala> val salt = generateSalt
salt: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwO
scala> "password".bcryptBounded(salt)
res3: Try[String] = Success($2a$10$8K1p/a0dL1LXMIgoEDFrwOfMQbLgtnOoKsWc.6U6H0llP3puzeeEu)
```
### Unsafe APIs
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 or password longer than 71 bytes). These Unsafe APIs are present for
backwards compatibility reasons and should be avoided if possible.
#### Encrypt password
```scala
scala> import com.github.t3hnar.bcrypt._
import com.github.t3hnar.bcrypt._
scala> "password".bcryptBounded
res1: String = $2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG
```
#### Validate password
```scala
scala> "password".isBcryptedBounded("$2a$10$iXIfki6AefgcUsPqR.niQ.FvIK8vdcfup09YmUxmzS/sQeuI3QOFG")
res2: Boolean = true
```
#### Advanced usage
```scala
scala> val salt = generateSalt
salt: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwO
scala> "password".bcryptBounded(salt)
res3: String = $2a$10$8K1p/a0dL1LXMIgoEDFrwOfMQbLgtnOoKsWc.6U6H0llP3puzeeEu
```
## Setup
#### SBT
```scala
libraryDependencies += "com.github.t3hnar" %% "scala-bcrypt" % "4.3.1"
```
#### Maven
```xml
com.github.t3hnar
scala-bcrypt_2.13
4.3.1
```