1.0.2 • Published 5 years ago
bs-password v1.0.2
bs-password
A password hashing libarary for ReasonML
How do I install it?
Inside of a BuckleScript project:
yarn add bs-password
Then add bs-password
to your bs-dependencies
in bsconfig.json
{
"bs-dependencies": [ "bs-password" ]
}
Usage
Basic Callback based interface.
- Derive a hash
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)
};
});
- Verify a hash against a password.
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)
}
});
};
});
- Generate a random token.
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 based interface.
- Derive a hash.
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);
- Verify a hash against a password.
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)
- Generate a random token.
let algo = Password.Algorithm.Bcrypt;
Password.Future.token(algo, 16)
|. Future.mapOk(token => Js.log2("Token: ", token))
|. Future.mapError(raise)
Promise based interface.
- Derive a hash.
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
)
- Verify a hash against a password.
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
)
- Generate a random token.
let algo = Password.Algorithm.Bcrypt;
Password.Promise.token(algo, 31)
|> Js.Promise.then_(token => Js.log2("Token: ", token) |> Js.Promise.resolve)