argon2

中文文档
0.45.2发布于 2026/11/03MITTypeScriptCommonJS构建来源已验证

An Argon2 library for Node

原始使用文档
文档目录12 节
  1. Usage
  2. Comparison with the node:crypto native implementation
  3. Migrating from another hash function
  4. TypeScript usage
  5. Prebuilt binaries
  6. Before installing
  7. OSX
  8. FAQ
  9. Contributors
  10. Code contributors
  11. Financial contributors
  12. License

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 verify a password:

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

[!NOTE] By default, argon2.hash will generate secure hashes according to the security recommendations by the team that develops Argon2. For password hashing, there is no need to modify them.

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

Comparison with the node:crypto native implementation

The native API is focused towards generic usage of the Argon2 hash function, while this package historically focused on password hashing.

Once all supported Node releases include native Argon2 and older versions are officially EOL, node-argon2 will transition into a minimal wrapper. It will offer the most developer-friendly interface, bridging users to Node's native crypto methods. This avoids duplication and ensures seamless upgrades once native support is universal.

node-argon2node:crypto argon2
Generate hashes✅✅
PHC string formatting✅❌
Verify PHC string✅❌
Check if re-hash is needed✅❌
Provide sensible default parameters✅❌

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 >=22.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.

版本与发布记录

argon2 共发布 86 个版本,首次发布于 2015/12/19;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.45.2latest2026/11/03今天
0.45.12026/07/223 个月前
0.45.02026/07/193 个月前
1.0.0-alpha.1next2026/07/183 个月前
0.44.02025/08/111.2 年前
0.43.12025/07/151.3 年前

其他发布通道:true → 0.40.2,next → 1.0.0-alpha.1。

关于 argon2 的常见问题

argon2 是什么?

用于Node的Argon2库

如何安装 argon2?

在项目目录运行 npm install argon2,或使用 pnpm add / yarn add / bun add。

argon2 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

argon2 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

argon2 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.17.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

argon2 可以商用吗?

argon2 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

argon2 还在维护吗?

最新版本 0.45.2 发布于 2026/11/03(今天);至今共发布 86 个版本,其中过去 12 个月发布了 4 个;首次发布于 2015/12/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。