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

passlib

v0.3.0

Published

Your friendly password storage and verification library

Downloads

24

Readme

passlib

CI

Storing passwords securely, simplified

Usage

const passlib = require("passlib");

async function createUser(name, password) {
  const wrapped = await passlib.create(password);
  await db.store(name, wrapped); // or something like that
}

async function checkCredentials(name, password) {
  const expected = await db.get(name); // or something like that

  /* The actual check!
   * Make sure to always call this function (with a dummy password if needed),
   * even if you didn't find an account in your db, unless you're OK with an
   * attacker finding out all valid user names in your system.
   * If you do not call this function, the attacker may try user names and see
   * when something responds real quick, meaning no password check performed,
   * ergo not a valid user, or somewhat slower, account valid as it tried to
   * verify the password.
   * This is not a concern if you user directory is public anyway.
   */
  if (!(await passlib.verify(expected, password))) {
    throw new Error("Incorrect credentials");
  }

  // Upgrade when needed, so we always use the most current version
  if (passlib.needsUpgrade(expected)) {
    const wrapped = await passlib.create(password);
    await db.store(name, wrapped); // or something like that
  }
}

Word of advise

Do not mess with the wrapped values, ever. Just consider them opaque.

API

/**
 * Checks whether a stored value needs updating to a new version.
 * Important: this does *NOT* check the integrity of a stored value.
 * @param {string|Buffer} value Value to check
 * @returns {boolean} True if update is needed
 */
export function needsUpgrade(value: string | Buffer): boolean;
/**
 * Creates a value from a password that is suitable for storing
 * in a peristent store. If the store ever gets compromised,
 * the returned value is supposed to be secure enough so that
 * the password cannot be computed from it.
 *
 * @async
 * @param {string} password Password to wrap
 * @param {object} [options]
 * @param {integer} [options.version] Create with this specific version
 *   instead of the most current one
 * @param {boolean} [options.asBuffer] Return a node buffer instead of a string
 * @returns {Promise<string|Buffer>} Wrapped password value
 */
export function create(password: string, options?: {
    version?: any;
    asBuffer?: boolean;
}): Promise<string | Buffer>;
/**
 * Verifies a stored value created by this library matches
 * a user provided password.
 *
 * @async
 * @param {string|Buffer} value Previously stored value
 * @param {string} password Password to wrap
 * @returns {Promise<boolean>} True if password matches
 *   (e.g. login can proceed)
 */
export function verify(value: string | Buffer, password: string): Promise<boolean>;

Algorithms

v2 (current)

This algorithm just uses argon2 (via node-argon2), type argoon2id in version 1.3, with a memory cost of 20 MiB and a time cost of 5.

v1

This algorithm derives a key k using crypto.pbkdf2(password, salt, iterations, salt, 64, "sha512") where salt is a per-call generated crypto-random buffer of 32-bytes, and iterations is a value of minimum 20000 but adjusted to take approximately at least 100 milliseconds on the runtime computer.

A check = SHA224(VERSION(1) | iterations | salt) is computed, acting as a checksum around the parameters.

Finally, using mac = HMAC-SHA512(k, VERSION(1) | iterations | salt | check) (the important bit), the concatenation of VERSION(1) | iterations | salt | check | mac is returned, acting as the wrapped password value that can be securely stored.

This construction is analog to what scrypt (and node-scrypt) are using, except for the different KDF of course. Using PBKDF2 has the drawback that it is better "crackable" than e.g. scrypt. The decision to use it instead of scrypt in this version stems from PBKDF2 being part of the node standard library. It still is believed to be secure enough (given enough iterations)[citation needed] today.

v0

Do NOT Use: The password is just put plain text into a buffer, aka zero security. This version exists mainly for testing purposes. Did I mention you should NOT EVER use this?