https://github.com/scull7/bs-password
A password hashing library for ReasonML
https://github.com/scull7/bs-password
Last synced: over 1 year ago
JSON representation
A password hashing library for ReasonML
- Host: GitHub
- URL: https://github.com/scull7/bs-password
- Owner: scull7
- License: mit
- Created: 2018-05-30T09:52:06.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-17T05:40:18.000Z (over 7 years ago)
- Last Synced: 2025-03-02T10:16:49.357Z (over 1 year ago)
- Language: OCaml
- Homepage: https://scull7.github.io/bs-password/
- Size: 398 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/scull7/bs-password)
[](https://coveralls.io/github/scull7/bs-password?branch=master)
# bs-password
A password hashing libarary for ReasonML
## How do I install it?
Inside of a BuckleScript project:
```sh
yarn add bs-password
```
Then add `bs-password` to your `bs-dependencies` in `bsconfig.json`
```json
{
"bs-dependencies": [ "bs-password" ]
}
```
## Usage
### Basic Callback based interface.
1. Derive a hash
```reason
let password = "This is my password";
let algorithm = Password.Algorithm.Bcrypt;
Password.deriveKey(algorithm, password, (result) => {
switch (result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok((salt, hash)) => Js.log3("Salt and Key", salt, hash)
};
});
```
2. Verify a hash against a password.
```reason
let password = "This is my passsword";
let algorithm = Password.Algorithm.Bcrypt;
Password.deriveKey(algorithm, password, (result) => {
switch (result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok((_, hash)) =>
Password.verify(algorithm, hash, password, (result2) => {
switch (result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok(isValid) => Js.log2("isValid: ", isValid)
}
});
};
});
```
3. Generate a random token.
```reason
let algorithm = Password.Algorithm.Bcrypt;
let length = 8;
Password.token(algorithm, length, (result) => {
switch(result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok(token) => Js.log2("Token: ", token)
};
});
```
### [Future][reason-future] based interface.
1. Derive a hash.
```reason
let password = "This is my password";
let algorithm = Password.Algorithm.Bcrypt;
Password.Future.deriveKey(algorithm, password)
|. Future.mapOk(((salt, hash)) => Js.log3("Salt and Key: ", salt, hash))
|. Future.mapError(raise);
```
2. Verify a hash against a password.
```reason
let password = "This is my password";
let algo = Password.Algorithm.Bcrypt;
Password.Future.deriveKey(algo, password)
|. Future.flatMapOk(((_, hash)) => Password.Future.verify(algo, hash, password))
|. Future.mapOk(isValid => Js.log2("Is Valid: ", isValid))
|. Future.mapError(raise)
```
3. Generate a random token.
```reason
let algo = Password.Algorithm.Bcrypt;
Password.Future.token(algo, 16)
|. Future.mapOk(token => Js.log2("Token: ", token))
|. Future.mapError(raise)
```
### Promise based interface.
1. Derive a hash.
```reason
let password = "This is my password";
let algorithm = Password.Algorithm.Bcrypt;
Password.Promise.deriveKey(algorithm, password)
|> Js.Promise.then_(result =>
switch (result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok((salt, hash)) => Js.log3("Salt and key: ", salt, hash)
}
|> Js.Promise.resolve
)
```
2. Verify a hash against a password.
```reason
let password = "This is my password";
let algo = Password.Algorithm.Bcrypt;
Password.Promise.deriveKey(algo, password)
|> Js.Promise.then_(result =>
switch (result) {
| Belt.Result.Error(e) => raise(e)
| Belt.Result.Ok((_, hash)) => Js.Promise.resolve(hash)
}
)
|> Js.Promise.then_(hash => Password.Promise.verify(algo, hash, password))
|> Js.Promise.then_(isValid =>
Js.log2("Does Match: ", isValid) |> Js.Promise.resolve
)
```
3. Generate a random token.
```reason
let algo = Password.Algorithm.Bcrypt;
Password.Promise.token(algo, 31)
|> Js.Promise.then_(token => Js.log2("Token: ", token) |> Js.Promise.resolve)
```
[reason-future]: https://github.com/RationalJS/future
[Js.Promise]: https://bucklescript.github.io/bucklescript/api/Js.Promise.html