bcryptjs

  • Version 3.0.2
  • Published
  • 112 kB
  • No dependencies
  • BSD-3-Clause license

Install

npm i bcryptjs
yarn add bcryptjs
pnpm add bcryptjs

Overview

Optimized bcrypt in plain JavaScript with zero dependencies, with TypeScript support. Compatible to 'bcrypt'.

Index

Functions

function compare

compare: {
(password: string, hash: string): Promise<boolean>;
(
password: string,
hash: string,
callback?: Callback<boolean>,
progressCallback?: ProgressCallback
): void;
};
  • Asynchronously tests a password against a hash.

    Parameter password

    Password to test

    Parameter hash

    Hash to test against Promise, if callback has been omitted

  • Asynchronously tests a password against a hash.

    Parameter password

    Password to test

    Parameter hash

    Hash to test against

    Parameter callback

    Callback receiving the error, if any, otherwise the result

    Parameter progressCallback

    Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.

function compareSync

compareSync: (password: string, hash: string) => boolean;
  • Synchronously tests a password against a hash.

    Parameter password

    Password to test

    Parameter hash

    Hash to test against true if matching, otherwise false

function decodeBase64

decodeBase64: (s: string, len: number) => number[];
  • Decodes a base64 encoded string to up to len bytes of output, using the custom bcrypt alphabet.

    Parameter s

    String to decode

    Parameter len

    Maximum output length

function encodeBase64

encodeBase64: (b: Readonly<ArrayLike<number>>, len: number) => string;
  • Encodes a byte array to base64 with up to len bytes of input, using the custom bcrypt alphabet.

    Parameter b

    Byte array

    Parameter len

    Maximum input length

function genSalt

genSalt: {
(rounds?: number): Promise<string>;
(callback: Callback<string>): void;
(rounds: number, callback: Callback<string>): void;
};
  • Asynchronously generates a salt.

    Parameter rounds

    Number of rounds to use, defaults to 10 if omitted Promise with resulting salt, if callback has been omitted

  • Asynchronously generates a salt.

    Parameter callback

    Callback receiving the error, if any, and the resulting salt

  • Asynchronously generates a salt.

    Parameter rounds

    Number of rounds to use, defaults to 10 if omitted

    Parameter callback

    Callback receiving the error, if any, and the resulting salt

function genSaltSync

genSaltSync: (rounds?: number) => string;
  • Synchronously generates a salt.

    Parameter rounds

    Number of rounds to use, defaults to 10 if omitted Resulting salt

    Throws

    If a random fallback is required but not set

function getRounds

getRounds: (hash: string) => number;
  • Gets the number of rounds used to encrypt the specified hash.

    Parameter hash

    Hash to extract the used number of rounds from Number of rounds used

function getSalt

getSalt: (hash: string) => string;
  • Gets the salt portion from a hash. Does not validate the hash.

    Parameter hash

    Hash to extract the salt from Extracted salt part

function hash

hash: {
(password: string, salt: number | string): Promise<string>;
(
password: string,
salt: string | number,
callback?: Callback<string>,
progressCallback?: ProgressCallback
): void;
};
  • Asynchronously generates a hash for the given password.

    Parameter password

    Password to hash

    Parameter salt

    Salt length to generate or salt to use Promise with resulting hash, if callback has been omitted

  • Asynchronously generates a hash for the given password.

    Parameter password

    Password to hash

    Parameter salt

    Salt length to generate or salt to use

    Parameter callback

    Callback receiving the error, if any, and the resulting hash

    Parameter progressCallback

    Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.

function hashSync

hashSync: (password: string, salt?: number | string) => string;
  • Synchronously generates a hash for the given password.

    Parameter password

    Password to hash

    Parameter salt

    Salt length to generate or salt to use, default to 10 Resulting hash

function setRandomFallback

setRandomFallback: (random: RandomFallback) => void;
  • Sets the pseudo random number generator to use as a fallback if neither node's crypto module nor the Web Crypto API is available. Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!

    Parameter random

    Function taking the number of bytes to generate as its sole argument, returning the corresponding array of cryptographically secure random byte values.

function truncates

truncates: (password: string) => boolean;
  • Tests if a password will be truncated when hashed, that is its length is greater than 72 bytes when converted to UTF-8.

    Parameter password

    The password to test

    Returns

    true if truncated, otherwise false

Type Aliases

type Callback

type Callback<T> = (err: Error | null, result?: T) => void;
  • Called with an error on failure or a value of type T upon success.

type ProgressCallback

type ProgressCallback = (percentage: number) => void;
  • Called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.

type RandomFallback

type RandomFallback = (length: number) => number[];
  • Called to obtain random bytes when both Web Crypto API and Node.js crypto are not available.

Package Files (2)

Dependencies (0)

No dependencies.

Dev Dependencies (4)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/bcryptjs.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/bcryptjs)
  • HTML
    <a href="https://www.jsdocs.io/package/bcryptjs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>