npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-bcrypt

v1.0.1

Published

Simplify handling and hashing of passwords with bcrypt.

Downloads

1

Readme

simple-bcrypt

Hash passwords with bcrypt using secure settings. CommonJS and EcmaScript modules and syntax possible. Synchronous and asynchronous versions included.

Installation

npm i -S simple-brypt

Usage

Hashing a password

Before you can hash new password the library first has to be initialized. The libray will conduct a small dance to measure hashing performance and determine an ideal setting. During this time the server should be almost idle as to not influence measurements. Somewhere in your server initialization, before any password need to be hashed, include the following snippet:

const {BcryptSettings} = require("simple-bcrypt");

// ...

/**
 * Your init function.
 */
async function initialize(){
  // ...
  await BcryptSettings.init(500);
  //...
}

This has to be done in any context where you need to calculate bcrypt hashes, e.g. in worker threads or other processes.

Then, to create a hash:

const {hashSync, hashAsync} = require("simple-bcrypt");

console.log(
    hashSync("test"),
    // $2b$11$OdKlPM0Km45fev22th5lhOEGT5Bj1Wip14VmZto4P18zghV25t2VW
);
// or
hashAsync("test).then((hash) => {
    console.log(hash);
    // $2b$11$OdKlPM0Km45fev22th5lhOEGT5Bj1Wip14VmZto4P18zghV25t2VW
});

In addition to the string output that you might be used to from bcrypt, there is also an additional binary output (BMCF) available. You can specify the format string or binary as a second paramenter to the hash functions.

Comparing Hash against Password

To compare hashes, the BcryptSettings object does not need to be initialized. Simply call:

const {compareSync, compareAsnc} = require("simple-bcrypt");

const password = "test";
const hash = "$2b$11$OdKlPM0Km45fev22th5lhOEGT5Bj1Wip14VmZto4P18zghV25t2VW";

console.log(
    compareSync(password, hash),
    // true
    compareSync("p4ssw0rd", hash),
    // false
);

The async method works accordingly. Make sure to pass the two objects in the correct order. Instead of a hash in string format, you can also pass a BMCF formatted hash.

Encode/Decode to/from BMCF

The strings that bcrypt produces are in a format called Modular Crypt Format (MCF).

The Binary Modular Crypt Format (BMCF) takes slightly less space and might therefore be useful if you need to store many passwords. The drawback is, that bcrypt does not understand BMCF natively. A conversion is required to work with the data.

This library contains two utilities to convert between the two formats for bcrypt generated hashes. As a convenience, the function convert simply switches between the formats.

Call encode, decode, or convert with a hash in the respective format and the result will be the same hash in the other format.