bs-password

1.0.2 • Public • Published

Build Status Coverage Status

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.

  1. 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)
  };
});
  1. 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)
    }
    });
  };
});
  1. 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.

  1. 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);
  1. 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)
  1. 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.

  1. 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
)
  1. 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
  1. 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)

Package Sidebar

Install

npm i bs-password

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

690 kB

Total Files

47

Last publish

Collaborators

  • scull7