argon2 vs bcrypt vs pbkdf2
Password Hashing Libraries
argon2bcryptpbkdf2Similar Packages:

Password Hashing Libraries

Password hashing libraries are essential tools in web development for securely storing user passwords. They provide mechanisms to transform plain-text passwords into a hashed format, making it difficult for attackers to retrieve the original passwords even if they gain access to the database. These libraries implement various algorithms and techniques to enhance security, including salting, stretching, and key derivation functions. Choosing the right password hashing library is crucial for ensuring the integrity and security of user credentials in applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
argon202,1271.03 MB98 months agoMIT
bcrypt07,7831.11 MB32a year agoMIT
pbkdf2020342.5 kB36 months agoMIT

Feature Comparison: argon2 vs bcrypt vs pbkdf2

Security Strength

  • argon2:

    Argon2 is designed to resist both brute-force and side-channel attacks. It incorporates memory-hard functions, making it difficult for attackers to use specialized hardware to crack passwords. Its configurable parameters allow developers to adjust the memory and time cost, enhancing security based on current hardware capabilities.

  • bcrypt:

    Bcrypt is known for its adaptive nature, meaning it can be configured to increase the computational cost over time as hardware improves. This makes it a reliable choice for long-term security, although it is not as resistant to GPU attacks compared to Argon2.

  • pbkdf2:

    PBKDF2 is considered secure, but its resistance to modern attacks is not as strong as Argon2. It relies on iterations to increase the time required to compute the hash, but it does not have the memory-hard properties that make Argon2 more resilient against certain attack vectors.

Performance

  • argon2:

    Argon2 can be slower than other algorithms due to its memory-hard design, which is beneficial for security but may impact performance in high-load scenarios. However, its performance can be tuned by adjusting parameters to balance security and speed based on application needs.

  • bcrypt:

    Bcrypt is generally slower than traditional hashing algorithms like SHA-256, which is intentional to deter brute-force attacks. Its performance is consistent, but it may become a bottleneck in applications with extremely high user registration or login rates if not configured properly.

  • pbkdf2:

    PBKDF2's performance can vary significantly based on the number of iterations specified. While it can be tuned for performance, it may not be as efficient as Argon2 or Bcrypt in terms of computational cost versus security.

Ease of Use

  • argon2:

    Argon2 has a straightforward API, but its advanced configuration options may require a deeper understanding of security principles to use effectively. Developers may need to invest time in learning how to best configure its parameters for their specific use case.

  • bcrypt:

    Bcrypt is known for its simplicity and ease of use, making it a popular choice among developers. Its API is intuitive, and it handles salting automatically, which reduces the likelihood of implementation errors.

  • pbkdf2:

    PBKDF2 is also relatively easy to use, with many libraries providing straightforward implementations. However, developers must be careful to choose appropriate iteration counts to ensure adequate security.

Community Adoption

  • argon2:

    Argon2 is gaining traction as the recommended hashing algorithm due to its modern design and security features. While it is not as widely adopted as Bcrypt yet, its recognition as the winner of the Password Hashing Competition is driving its popularity.

  • bcrypt:

    Bcrypt has been widely adopted for many years and is considered a standard in password hashing. Its long-standing presence in the community means there is a wealth of resources, libraries, and examples available for developers.

  • pbkdf2:

    PBKDF2 has been around for a long time and is supported by many platforms and libraries. While it is still a valid choice, its popularity has waned in favor of newer algorithms like Argon2 and Bcrypt.

Configurability

  • argon2:

    Argon2 offers extensive configurability, allowing developers to adjust memory usage, time cost, and parallelism. This flexibility enables developers to tailor the hashing process to their specific security requirements and hardware capabilities.

  • bcrypt:

    Bcrypt allows developers to set the cost factor, which determines the computational complexity of the hashing process. While it is less configurable than Argon2, it still provides a reasonable level of adaptability for most applications.

  • pbkdf2:

    PBKDF2 allows for the specification of the number of iterations, which can be adjusted to increase security over time. However, it lacks the memory-hard properties of Argon2, making it less adaptable to modern attack vectors.

How to Choose: argon2 vs bcrypt vs pbkdf2

  • argon2:

    Choose Argon2 if you need a modern and highly secure hashing algorithm that is resistant to GPU-based attacks. It is the winner of the Password Hashing Competition and offers configurable memory usage, time complexity, and parallelism, making it suitable for applications requiring high security.

  • bcrypt:

    Choose Bcrypt if you are looking for a widely adopted and battle-tested hashing algorithm that is simple to implement and provides good security for most applications. It automatically handles salting and is designed to be slow, which helps mitigate brute-force attacks.

  • pbkdf2:

    Choose PBKDF2 if you need a flexible and widely supported key derivation function that can be used for various cryptographic purposes, including password hashing. It allows you to specify the number of iterations, making it adaptable to different security needs, though it may not be as resistant to modern attacks as Argon2.

README for argon2

node-argon2

Financial contributors on Open Collective Build status NPM package

Bindings to the reference Argon2 implementation.

Usage

It's possible to hash using either Argon2i, Argon2d or Argon2id (default), and verify if a password matches a hash.

To hash a password:

const argon2 = require('argon2');

try {
  const hash = await argon2.hash("password");
} catch (err) {
  //...
}

To see how you can modify the output (hash length, encoding) and parameters (time cost, memory cost and parallelism), read the wiki

To verify a password:

try {
  if (await argon2.verify("<big long hash>", "password")) {
    // password match
  } else {
    // password did not match
  }
} catch (err) {
  // internal failure
}

Migrating from another hash function

See this article on the wiki for steps on how to migrate your existing code to Argon2. It's easy!

TypeScript usage

A TypeScript type declaration file is published with this module. If you are using TypeScript 2.0.0 or later, that means you do not need to install any additional typings in order to get access to the strongly typed interface. Simply use the library as mentioned above.

import * as argon2 from "argon2";

const hash = await argon2.hash(..);

Prebuilt binaries

node-argon2 provides prebuilt binaries from v0.26.0 onwards. They are built every release using GitHub Actions.

The current prebuilt binaries are built and tested with the following systems:

  • Ubuntu 22.04 (x86-64; ARM64 from v0.28.2; ARMv7 from v0.43.0)
  • MacOS 13 (x86-64)
  • MacOS 14 (ARM64 from v0.29.0)
  • Windows Server 2022 (x86-64)
  • Alpine Linux 3.18 (x86-64 from v0.28.1; ARM64 from v0.28.2; ARMv7 from v0.43.0)
  • FreeBSD 14 (x86-64 from v0.29.1; ARM64 from v0.44.0)

Binaries should also work for any version more recent than the ones listed above. For example, the binary for Ubuntu 20.04 also works on Ubuntu 22.04, or any other Linux system that ships a newer version of glibc; the binary for MacOS 11 also works on MacOS 12. If your platform is below the above requirements, you can follow the Before installing section below to manually compile from source. It is also always recommended to build from source to ensure consistency of the compiled module.

Before installing

You can skip this section if the prebuilt binaries work for you.

You MUST have a node-gyp global install before proceeding with the install, along with GCC >= 5 / Clang >= 3.3. On Windows, you must compile under Visual Studio 2015 or newer.

node-argon2 works only and is tested against Node >=18.0.0.

OSX

To install GCC >= 5 on OSX, use homebrew:

$ brew install gcc

Once you've got GCC installed and ready to run, you then need to install node-gyp, you must do this globally:

$ npm install -g node-gyp

Finally, once node-gyp is installed and ready to go, you can install this library, specifying the GCC or Clang binary to use:

$ CXX=g++-12 npm install argon2

NOTE: If your GCC or Clang binary is named something different than g++-12, you'll need to specify that in the command.

FAQ

How do I manually rebuild the binaries?
$ npx @mapbox/node-pre-gyp rebuild -C ./node_modules/argon2

Run @mapbox/node-pre-gyp instead of node-gyp because node-argon2's binding.gyp file relies on variables from @mapbox/node-pre-gyp.

You can omit npx @mapbox and use just node-pre-gyp if you have a global installation of @mapbox/node-pre-gyp, otherwise prefixing npx will use the local one in ./node_modules/.bin

How do I skip installing prebuilt binaries and manually compile from source?

You can do either of the two methods below:

  1. Force build from source on install.
$ npm install argon2 --build-from-source
  1. Ignore node-argon2 install script and build manually.
$ npm install argon2 --ignore-scripts
$ npx node-gyp rebuild -C ./node_modules/argon2
I installed Node as a snap, and I can't install node-argon2.

This seems to be an issue related to snap (see #345 (comment)). Installing Node with another package manager, such as asdf or nvm, is a possible workaround.

Contributors

Code contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

Work licensed under the MIT License. Please check P-H-C/phc-winner-argon2 for license over Argon2 and the reference implementation.