• Stars
    star
    1,877
  • Rank 24,692 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js bindings for Argon2 hashing algorithm

node-argon2

Financial contributors on Open Collective Build status NPM package Coverage status Code Quality

Bindings to the reference Argon2 implementation.

Want to use it on the command line? Instead check node-argon2-cli.

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 20.04 (x86-64; ARM64 from v0.28.2)
  • MacOS 11 (x86-64)
  • MacOS 12 (ARM64 from v0.29.0)
  • Windows Server 2019 (x86-64)
  • Alpine Linux 3.13 (x86-64 from v0.28.1; ARM64 from v0.28.2)
  • FreeBSD 13.1 (x86-64 from v0.29.1)

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 >=14.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 @mapbox/node-pre-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.

Differences from node-argon2-ffi

The interface of both are very similar, notably, node-argon2-ffi splits the argon2i and argon2d function set, but this module also has the argon2id option, which node-argon2-ffi does not support. Also, while node-argon2-ffi suggests you promisify crypto.randomBytes, node-argon2 library does that internally.

node-argon2 is much lighter than node-argon2-ffi, at 184 KB for [email protected] against 2.56 MB for [email protected]. Performance-wise, the libraries are equal. You can run the same benchmark suite if you are curious, but both can perform around 130 hashes/second on an Intel Core i5-4460 @ 3.2GHz with default options.

This library is implemented natively, meaning it is an extension to the node engine. Thus, half of the code is C++ bindings, the other half is Javascript functions. node-argon2-ffi uses ffi, a mechanism to call functions from one language in another, and handles the type bindings (e.g. JS Number -> C++ int).

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.

More Repositories

1

faster-computer-guide

Python
103
star
2

node-argon2-cli

CLI for node-argon2
JavaScript
29
star
3

koa-from-scratch

Learn how to build a secure, RESTful API with Koa from the ground up.
JavaScript
22
star
4

voucomernoru

JavaScript
15
star
5

netmon

Daemon that monitors your internet speed and tweets 😠
Python
13
star
6

react-use-credential-management

React hook to leverage Credential Management API
TypeScript
10
star
7

tibia-ipchanger

Python
8
star
8

unified-remote-mpv

Lua
6
star
9

impostometro

JavaScript
6
star
10

moita-ufsc-crawler

Flexible, modular and powerful scraper for ramiropolla/capim and derivatives
Python
6
star
11

aindahatempo

JavaScript
5
star
12

dotfiles

Now unified and better than ever
Shell
5
star
13

Zenith-AAC

(UNMAINTAINED) Powerful automated account creator for Open Tibia servers
PHP
5
star
14

cagr-api

Python
5
star
15

lumelia

TypeScript
4
star
16

tibiatravel

TypeScript
3
star
17

curriculum-vitae

My curriculum vitae/résumé.
TeX
3
star
18

enigma

Python
3
star
19

ocelot

Python
2
star
20

sucuri

HTML
2
star
21

sandra

JavaScript
2
star
22

cirogenerator

JavaScript
2
star
23

i3-swipe

Python
2
star
24

jaguari

Python
2
star
25

flags-calculator

Open Tibia server player access and permissions flag calculator
CSS
2
star
26

imgsort

Magically sort images into resolution-grouped folders
Python
2
star
27

advent-of-code-2021

Elixir
2
star
28

tibiapak

1
star
29

tcc

TeX
1
star
30

otb

C++
1
star
31

ine5420

C++
1
star
32

manpagr

Trabalho de Implementação 2 - Estrutura de Dados
C++
1
star
33

pkgbuilder

Dockerfile
1
star
34

histar

JavaScript
1
star
35

rsa.sh

TypeScript
1
star
36

advent-of-code-2020

Elixir
1
star
37

flagr

Use whatever flag you want and stop whining on Facebook.
Python
1
star
38

robson

JavaScript
1
star
39

espanha-russia-eterno

E se Espanha x Rússia nunca tivesse acabado?
JavaScript
1
star