This commit is contained in:
Tutur33
2023-11-24 23:58:26 +01:00
parent 25395c0ee1
commit 938ad9d309
4191 changed files with 41 additions and 518781 deletions
-22
View File
@@ -1,22 +0,0 @@
The MIT License (MIT)
Copyright (c) 2015 Ranieri Althoff
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-218
View File
@@ -1,218 +0,0 @@
# node-argon2
[![Financial contributors on Open Collective][opencollective-image]][opencollective-url]
[![Build status][actions-image]][actions-url]
[![NPM package][npm-image]][npm-url]
[![Coverage status][coverage-image]][coverage-url]
[![Code Quality][codequality-image]][codequality-url]
Bindings to the reference [Argon2](https://github.com/P-H-C/phc-winner-argon2)
implementation.
**Want to use it on the command line? Instead check
[node-argon2-cli](https://github.com/ranisalt/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:
```js
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](https://github.com/ranisalt/node-argon2/wiki/Options)
To verify a password:
```js
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](https://github.com/ranisalt/node-argon2/wiki/Migrating-from-another-hash-function) 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.
```ts
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](#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](#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](http://brew.sh/):
```console
$ 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:
```console
$ 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:
```console
$ 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
<details>
<summary>How do I manually rebuild the binaries?</summary>
```bash
$ 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`
</details>
<details>
<summary>
How do I skip installing prebuilt binaries and manually compile from source?
</summary>
You can do either of the two methods below:
1. Force build from source on install.
```bash
$ npm install argon2 --build-from-source
```
2. Ignore `node-argon2` install script and build manually.
```bash
$ npm install argon2 --ignore-scripts
$ npx @mapbox/node-pre-gyp rebuild -C ./node_modules/argon2
```
</details>
<details>
<summary>
I installed Node as a <a href="https://snapcraft.io/node">snap</a>, and I can't install node-argon2.
</summary>
This seems to be an issue related to snap (see [#345 (comment)](https://github.com/ranisalt/node-argon2/issues/345#issuecomment-1164178674)). Installing Node with another package manager, such as [asdf](https://asdf-vm.com/) or [nvm](https://github.com/nvm-sh/nvm), is a possible workaround.
</details>
### Differences from [node-argon2-ffi](https://github.com/cjlarose/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
argon2@0.29.1 against 2.56 MB for argon2-ffi@1.2.0. 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](CONTRIBUTING.md)].
<a href="https://github.com/ranisalt/node-argon2/graphs/contributors"><img src="https://opencollective.com/node-argon2/contributors.svg?width=890&button=false" /></a>
### Financial contributors
Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/node-argon2/contribute)]
#### Individuals
<a href="https://opencollective.com/node-argon2"><img src="https://opencollective.com/node-argon2/individuals.svg?width=890"></a>
#### Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/node-argon2/contribute)]
<a href="https://opencollective.com/node-argon2/organization/0/website"><img src="https://opencollective.com/node-argon2/organization/0/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/1/website"><img src="https://opencollective.com/node-argon2/organization/1/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/2/website"><img src="https://opencollective.com/node-argon2/organization/2/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/3/website"><img src="https://opencollective.com/node-argon2/organization/3/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/4/website"><img src="https://opencollective.com/node-argon2/organization/4/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/5/website"><img src="https://opencollective.com/node-argon2/organization/5/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/6/website"><img src="https://opencollective.com/node-argon2/organization/6/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/7/website"><img src="https://opencollective.com/node-argon2/organization/7/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/8/website"><img src="https://opencollective.com/node-argon2/organization/8/avatar.svg"></a>
<a href="https://opencollective.com/node-argon2/organization/9/website"><img src="https://opencollective.com/node-argon2/organization/9/avatar.svg"></a>
## License
Work licensed under the [MIT License](LICENSE). Please check
[P-H-C/phc-winner-argon2](https://github.com/P-H-C/phc-winner-argon2) for
license over Argon2 and the reference implementation.
[opencollective-image]: https://img.shields.io/opencollective/all/node-argon2.svg?style=flat-square
[opencollective-url]: https://opencollective.com/node-argon2
[npm-image]: https://img.shields.io/npm/v/argon2.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/argon2
[actions-image]: https://img.shields.io/github/workflow/status/ranisalt/node-argon2/CI/master?style=flat-square
[actions-url]: https://github.com/ranisalt/node-argon2/actions
[coverage-image]: https://img.shields.io/codacy/coverage/3aa6daee00154e1492660ecb2f788f73/master.svg?style=flat-square
[coverage-url]: https://app.codacy.com/gh/ranisalt/node-argon2
[codequality-image]: https://img.shields.io/codacy/grade/3aa6daee00154e1492660ecb2f788f73/master.svg?style=flat-square
[codequality-url]: https://app.codacy.com/gh/ranisalt/node-argon2
-127
View File
@@ -1,127 +0,0 @@
#include "argon2/include/argon2.h"
#include <cassert>
#include <cstdint>
#include <napi.h>
#include <vector>
using namespace Napi;
using ustring = std::vector<uint8_t>;
static ustring from_buffer(const Value &value) {
const auto &buf = value.As<Buffer<uint8_t>>();
const auto &data = buf.Data();
return {data, data + buf.Length()};
}
static Buffer<uint8_t> to_buffer(const Env &env, const ustring &str) {
return Buffer<uint8_t>::Copy(env, str.data(), str.size());
}
struct Options {
ustring secret;
ustring ad;
uint32_t hash_length;
uint32_t time_cost;
uint32_t memory_cost;
uint32_t parallelism;
uint32_t version;
argon2_type type;
};
static argon2_context make_context(uint8_t *buf, ustring &plain, ustring &salt,
Options &opts) {
argon2_context ctx;
ctx.out = buf;
ctx.outlen = opts.hash_length;
ctx.pwd = plain.data();
ctx.pwdlen = plain.size();
ctx.salt = salt.data();
ctx.saltlen = salt.size();
ctx.secret = opts.secret.empty() ? nullptr : opts.secret.data();
ctx.secretlen = opts.secret.size();
ctx.ad = opts.ad.empty() ? nullptr : opts.ad.data();
ctx.adlen = opts.ad.size();
ctx.t_cost = opts.time_cost;
ctx.m_cost = opts.memory_cost;
ctx.lanes = opts.parallelism;
ctx.threads = opts.parallelism;
ctx.allocate_cbk = nullptr;
ctx.free_cbk = nullptr;
ctx.flags = ARGON2_FLAG_CLEAR_PASSWORD | ARGON2_FLAG_CLEAR_SECRET;
ctx.version = opts.version;
return ctx;
}
class HashWorker final : public AsyncWorker {
public:
HashWorker(const Function &callback, ustring &&plain, ustring &&salt,
Options &&opts)
: AsyncWorker{callback, "argon2:HashWorker"}, plain{std::move(plain)},
salt{std::move(salt)}, opts{std::move(opts)} {}
void Execute() override {
auto buf = std::make_unique<uint8_t[]>(opts.hash_length);
auto ctx = make_context(buf.get(), plain, salt, opts);
int result = argon2_ctx(&ctx, opts.type);
if (result != ARGON2_OK) {
/* LCOV_EXCL_START */
SetError(argon2_error_message(result));
/* LCOV_EXCL_STOP */
} else {
hash.assign(buf.get(), buf.get() + opts.hash_length);
}
}
void OnOK() override {
const auto &env = Env();
HandleScope scope{env};
Callback()({env.Undefined(), to_buffer(env, hash)});
}
private:
ustring plain;
ustring salt;
Options opts;
ustring hash;
};
static Options extract_opts(const Object &opts) {
return {
opts.Has("secret") ? from_buffer(opts["secret"]) : ustring{},
opts.Has("associatedData") ? from_buffer(opts["associatedData"])
: ustring{},
opts["hashLength"].ToNumber(),
opts["timeCost"].ToNumber(),
opts["memoryCost"].ToNumber(),
opts["parallelism"].ToNumber(),
opts["version"].ToNumber(),
argon2_type(int(opts["type"].ToNumber())),
};
}
static Value Hash(const CallbackInfo &info) {
assert(info.Length() == 4 && info[0].IsBuffer() && info[1].IsBuffer() &&
info[2].IsObject() && info[3].IsFunction());
auto worker = new HashWorker{info[3].As<Function>(), from_buffer(info[0]),
from_buffer(info[1]),
extract_opts(info[2].As<Object>())};
worker->Queue();
return info.Env().Undefined();
}
static Object init(Env env, Object exports) {
exports["hash"] = Function::New(env, Hash);
return exports;
}
NODE_API_MODULE(argon2_lib, init);
-50
View File
@@ -1,50 +0,0 @@
// Type definitions for argon2 v0.19.2
/// <reference types="node" />
export interface Options {
hashLength?: number;
timeCost?: number;
memoryCost?: number;
parallelism?: number;
type?: 0 | 1 | 2;
version?: number;
salt?: Buffer;
saltLength?: number;
raw?: boolean;
secret?: Buffer;
associatedData?: Buffer;
}
export interface NumericLimit {
max: number;
min: number;
}
export interface OptionLimits {
hashLength: NumericLimit;
memoryCost: NumericLimit;
timeCost: NumericLimit;
parallelism: NumericLimit;
}
export const argon2d: 0;
export const argon2i: 1;
export const argon2id: 2;
export const defaults: Options;
export const limits: OptionLimits;
export function hash(
plain: Buffer | string,
options: Options & { raw: true }
): Promise<Buffer>;
export function hash(
plain: Buffer | string,
options?: Options & { raw?: false }
): Promise<string>;
export function verify(
hash: string,
plain: Buffer | string,
options?: Options
): Promise<boolean>;
export function needsRehash(hash: string, options?: Options): boolean;
-121
View File
@@ -1,121 +0,0 @@
"use strict";
const assert = require("assert");
const { randomBytes, timingSafeEqual } = require("crypto");
const path = require("path");
const { promisify } = require("util");
const binary = require("@mapbox/node-pre-gyp");
const bindingPath = binary.find(path.resolve(__dirname, "./package.json"));
const { hash: _hash } = require(bindingPath);
const { deserialize, serialize } = require("@phc/format");
const types = Object.freeze({ argon2d: 0, argon2i: 1, argon2id: 2 });
const defaults = Object.freeze({
hashLength: 32,
saltLength: 16,
timeCost: 3,
memoryCost: 1 << 16,
parallelism: 4,
type: types.argon2id,
version: 0x13,
});
const limits = Object.freeze({
hashLength: { min: 4, max: 2 ** 32 - 1 },
memoryCost: { min: 1 << 10, max: 2 ** 32 - 1 },
timeCost: { min: 2, max: 2 ** 32 - 1 },
parallelism: { min: 1, max: 2 ** 24 - 1 },
});
const names = Object.freeze({
[types.argon2d]: "argon2d",
[types.argon2i]: "argon2i",
[types.argon2id]: "argon2id",
});
const bindingsHash = promisify(_hash);
const generateSalt = promisify(randomBytes);
const assertLimits =
(options) =>
([key, { max, min }]) => {
const value = options[key];
assert(
min <= value && value <= max,
`Invalid ${key}, must be between ${min} and ${max}.`
);
};
const hash = async (plain, { raw, salt, ...options } = {}) => {
options = { ...defaults, ...options };
Object.entries(limits).forEach(assertLimits(options));
salt = salt || (await generateSalt(options.saltLength));
const hash = await bindingsHash(Buffer.from(plain), salt, options);
if (raw) {
return hash;
}
const {
type,
version,
memoryCost: m,
timeCost: t,
parallelism: p,
associatedData: data,
} = options;
return serialize({
id: names[type],
version,
params: { m, t, p, ...(data ? { data } : {}) },
salt,
hash,
});
};
const needsRehash = (digest, options) => {
const { memoryCost, timeCost, version } = { ...defaults, ...options };
const {
version: v,
params: { m, t },
} = deserialize(digest);
return +v !== +version || +m !== +memoryCost || +t !== +timeCost;
};
const verify = async (digest, plain, options) => {
const obj = deserialize(digest);
// Only these have the "params" key, so if the password was encoded
// using any other method, the destructuring throws an error
if (!(obj.id in types)) {
return false;
}
const {
id,
version = 0x10,
params: { m, t, p, data },
salt,
hash,
} = obj;
return timingSafeEqual(
await bindingsHash(Buffer.from(plain), salt, {
...options,
type: types[id],
version: +version,
hashLength: hash.length,
memoryCost: +m,
timeCost: +t,
parallelism: +p,
...(data ? { associatedData: Buffer.from(data, "base64") } : {}),
}),
hash
);
};
module.exports = { defaults, limits, hash, needsRehash, verify, ...types };
-81
View File
@@ -1,81 +0,0 @@
{
"target_defaults": {
"include_dirs": ["argon2/include"],
"target_conditions": [
["OS == 'mac'", {
"xcode_settings": {
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
}
}],
],
"configurations": {
"Release": {
"target_conditions": [
["OS != 'win'", {
"cflags+": ["-fdata-sections", "-ffunction-sections", "-fvisibility=hidden"],
"ldflags+": ["-Wl,--gc-sections"]
}]
],
"defines+": ["_FORTIFY_SOURCE=2", "NDEBUG"]
}
}
},
"targets": [
{
"target_name": "libargon2",
"sources": [
"argon2/src/argon2.c",
"argon2/src/core.c",
"argon2/src/blake2/blake2b.c",
"argon2/src/thread.c",
"argon2/src/encoding.c",
],
"cflags+": ["-Wno-type-limits"],
"conditions": [
["target_arch == 'ia32' or target_arch == 'x64'", {
"cflags+": ["-msse", "-msse2"],
"sources+": ["argon2/src/opt.c"]
}, {
"sources+": ["argon2/src/ref.c"]
}]
],
"type": "static_library"
}, {
"target_name": "<(module_name)",
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
},
"defines": [
"NAPI_VERSION=<(napi_build_version)",
],
"sources": [
"argon2.cpp"
],
"cflags_cc!": ["-fno-exceptions"],
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"dependencies": ["libargon2"],
"configurations": {
"Debug": {
"conditions": [
["OS == 'linux'", {
"cflags": ["--coverage"],
"ldflags": ["-fprofile-arcs", "-ftest-coverage"],
}]
]
}
}
}, {
"target_name": "action_after_build",
"type": "none",
"dependencies": ["<(module_name)"],
"copies": [{
"files": ["<(PRODUCT_DIR)/<(module_name).node"],
"destination": "<(module_path)"
}]
}
]
}
-84
View File
@@ -1,84 +0,0 @@
{
"name": "argon2",
"version": "0.30.3",
"description": "An Argon2 library for Node",
"main": "argon2.js",
"files": [
"argon2.cpp",
"argon2.d.ts",
"binding.gyp",
"argon2/CHANGELOG.md",
"argon2/LICENSE",
"argon2/include/",
"argon2/src/blake2/",
"argon2/src/argon2.c",
"argon2/src/core.c",
"argon2/src/core.h",
"argon2/src/encoding.c",
"argon2/src/encoding.h",
"argon2/src/opt.c",
"argon2/src/ref.c",
"argon2/src/thread.c",
"argon2/src/thread.h"
],
"types": "argon2.d.ts",
"scripts": {
"install": "node-pre-gyp install --fallback-to-build",
"format": "prettier --write \"**/*.{js,json,ts}\"",
"test": "c8 mocha test/test.js",
"test:ts": "tsc -p . && node test/test-d.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ranisalt/node-argon2.git"
},
"keywords": [
"argon2",
"crypto",
"encryption",
"hashing",
"password"
],
"author": "Ranieri Althoff <ranisalt+argon2@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ranisalt/node-argon2/issues"
},
"homepage": "https://github.com/ranisalt/node-argon2#readme",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.10",
"@phc/format": "^1.0.0",
"node-addon-api": "^5.0.0"
},
"devDependencies": {
"@types/node": "^18.11.18",
"c8": "^7.12.0",
"mocha": "^10.2.0",
"node-gyp": "^9.3.1",
"prettier": "^2.8.1",
"typescript": "^4.9.4"
},
"binary": {
"module_name": "argon2",
"module_path": "./lib/binding/napi-v{napi_build_version}",
"host": "https://github.com",
"remote_path": "./ranisalt/node-argon2/releases/download/v{version}",
"package_name": "{module_name}-v{version}-napi-v{napi_build_version}-{platform}-{arch}-{libc}.tar.gz",
"napi_versions": [
3
]
},
"engines": {
"node": ">=14.0.0"
},
"standard": {
"env": "mocha",
"ignore": [
"test/test-d.js"
]
},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/node-argon2"
}
}