Skip to content

Commit 5f72eeb

Browse files
committed
fixup docs and make algorithm first argument
1 parent b067bcd commit 5f72eeb

4 files changed

Lines changed: 213 additions & 184 deletions

File tree

benchmark/crypto/argon2.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const bench = common.createBenchmark(main, {
1717
n: [50],
1818
});
1919

20-
function measureSync(n, message, nonce, options) {
20+
function measureSync(n, algorithm, message, nonce, options) {
2121
bench.start();
2222
for (let i = 0; i < n; ++i)
23-
argon2Sync({ ...options, message, nonce, tagLength: 64 });
23+
argon2Sync(algorithm, { ...options, message, nonce, tagLength: 64 });
2424
bench.end(n);
2525
}
2626

27-
function measureAsync(n, message, nonce, options) {
27+
function measureAsync(n, algorithm, message, nonce, options) {
2828
let remaining = n;
2929
function done(err) {
3030
assert.ifError(err);
@@ -33,15 +33,15 @@ function measureAsync(n, message, nonce, options) {
3333
}
3434
bench.start();
3535
for (let i = 0; i < n; ++i)
36-
argon2({ ...options, message, nonce, tagLength: 64 }, done);
36+
argon2(algorithm, { ...options, message, nonce, tagLength: 64 }, done);
3737
}
3838

39-
function main({ n, mode, ...options }) {
39+
function main({ n, mode, algorithm, ...options }) {
4040
// Message, nonce, secret, associated data & tag length do not affect performance
4141
const message = randomBytes(32);
4242
const nonce = randomBytes(16);
4343
if (mode === 'sync')
44-
measureSync(n, message, nonce, options);
44+
measureSync(n, algorithm, message, nonce, options);
4545
else
46-
measureAsync(n, message, nonce, options);
46+
measureAsync(n, algorithm, message, nonce, options);
4747
}

doc/api/crypto.md

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,39 +2949,40 @@ Does not perform any other validation checks on the certificate.
29492949

29502950
## `node:crypto` module methods and properties
29512951

2952-
### `crypto.argon2(options, callback)`
2952+
### `crypto.argon2(algorithm, parameters, callback)`
29532953

29542954
<!-- YAML
29552955
added: REPLACEME
29562956
-->
29572957

29582958
> Stability: 1.2 - Release candidate
29592959
2960-
* `options` {Object}
2961-
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView}
2962-
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
2963-
least 8 bytes long.
2964-
* `parallelism` {number} Parallelization parameter (number of lanes and threads).
2965-
Must be greater than 1 and less than `2**24-1`.
2966-
* `tagLength` {number} The length of the key to generate. Must be greater than 4 and
2960+
* `algorithm` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
2961+
* `parameters` {Object}
2962+
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView} REQUIRED, this is the password for password
2963+
hashing applications of Argon2.
2964+
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} REQUIRED, must be at
2965+
least 8 bytes long. This is the salt for password hashing applications of Argon2.
2966+
* `parallelism` {number} REQUIRED, degree of parallelism determines how many computational chains (lanes)
2967+
can be run. Must be greater than 1 and less than `2**24-1`.
2968+
* `tagLength` {number} REQUIRED, the length of the key to generate. Must be greater than 4 and
29672969
less than `2**32-1`.
2968-
* `memory` {number} Memory cost in 1KiB blocks. Must be greater than
2970+
* `memory` {number} REQUIRED, memory cost in 1KiB blocks. Must be greater than
29692971
`8 * parallelism` and less than `2**32-1`. The actual number of blocks is rounded
29702972
down to the nearest multiple of `4 * parallelism`.
2971-
* `passes` {number} Number of passes (iterations). Must be greater than 1 and less
2973+
* `passes` {number} REQUIRED, number of passes (iterations). Must be greater than 1 and less
29722974
than `2**32-1`.
2973-
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView} Random additional input,
2974-
similar to the salt, that should **NOT** be stored with the derived key. Also known
2975-
as a pepper. If used, must have a length not greater than `2**32-1` bytes.
2976-
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional data to
2975+
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView|undefined} OPTIONAL, Random additional input,
2976+
similar to the salt, that should **NOT** be stored with the derived key. This is known as pepper in
2977+
password hashing applications. If used, must have a length not greater than `2**32-1` bytes.
2978+
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView|undefined} OPTIONAL, Additional data to
29772979
be added to the hash, functionally equivalent to salt or secret, but meant for
29782980
non-random data. If used, must have a length not greater than `2**32-1` bytes.
2979-
* `type` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
29802981
* `callback` {Function}
29812982
* `err` {Error}
29822983
* `derivedKey` {Buffer}
29832984

2984-
Provides an asynchronous [argon2][] implementation. Argon2 is a password-based
2985+
Provides an asynchronous [Argon2][] implementation. Argon2 is a password-based
29852986
key derivation function that is designed to be expensive computationally and
29862987
memory-wise in order to make brute-force attacks unrewarding.
29872988

@@ -2999,64 +3000,73 @@ An exception is thrown when any of the input arguments specify invalid values
29993000
or types.
30003001

30013002
```mjs
3002-
const {
3003-
argon2,
3004-
randomBytes,
3005-
} = await import('node:crypto');
3006-
3007-
const nonce = randomBytes(16);
3008-
argon2({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 }, (err, derivedKey) => {
3003+
const { argon2, randomBytes } = await import('node:crypto');
3004+
3005+
const parameters = {
3006+
message: 'password',
3007+
nonce: randomBytes(16),
3008+
parallelism: 4,
3009+
tagLength: 64,
3010+
memory: 65536,
3011+
passes: 3,
3012+
};
3013+
3014+
argon2('argon2id', parameters, (err, derivedKey) => {
30093015
if (err) throw err;
3010-
console.log(derivedKey.toString('hex')); // '0de3036...22afcc5'
3016+
console.log(derivedKey.toString('hex')); // 'af91dad...9520f15'
30113017
});
30123018
```
30133019

30143020
```cjs
3015-
const {
3016-
argon2,
3017-
randomBytes,
3018-
} = require('node:crypto');
3019-
3020-
randomBytes(16, (err, nonce) => {
3021+
const { argon2, randomBytes } = require('node:crypto');
3022+
3023+
const parameters = {
3024+
message: 'password',
3025+
nonce: randomBytes(16),
3026+
parallelism: 4,
3027+
tagLength: 64,
3028+
memory: 65536,
3029+
passes: 3,
3030+
};
3031+
3032+
argon2('argon2id', parameters, (err, derivedKey) => {
30213033
if (err) throw err;
3022-
argon2({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 }, (err, derivedKey) => {
3023-
if (err) throw err;
3024-
console.log(derivedKey.toString('hex')); // '0de3036...22afcc5'
3025-
});
3034+
console.log(derivedKey.toString('hex')); // 'af91dad...9520f15'
30263035
});
30273036
```
30283037

3029-
### `crypto.argon2Sync(password, salt, keylen, options)`
3038+
### `crypto.argon2Sync(algorithm, parameters)`
30303039

30313040
<!-- YAML
30323041
added: REPLACEME
30333042
-->
30343043

30353044
> Stability: 1.2 - Release candidate
30363045
3037-
* `options` {Object}
3038-
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView}
3039-
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
3040-
least 8 bytes long.
3041-
* `parallelism` {number} Parallelization parameter (number of lanes and threads).
3042-
Must be greater than 1 and less than `2**24-1`.
3043-
* `tagLength` {number} The length of the key to generate. Must be greater than 4 and
3046+
* `algorithm` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
3047+
* `parameters` {Object}
3048+
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView} REQUIRED, this is the password for password
3049+
hashing applications of Argon2.
3050+
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} REQUIRED, must be at
3051+
least 8 bytes long. This is the salt for password hashing applications of Argon2.
3052+
* `parallelism` {number} REQUIRED, degree of parallelism determines how many computational chains (lanes)
3053+
can be run. Must be greater than 1 and less than `2**24-1`.
3054+
* `tagLength` {number} REQUIRED, the length of the key to generate. Must be greater than 4 and
30443055
less than `2**32-1`.
3045-
* `memory` {number} Memory cost in 1KiB blocks. Must be greater than
3056+
* `memory` {number} REQUIRED, memory cost in 1KiB blocks. Must be greater than
30463057
`8 * parallelism` and less than `2**32-1`. The actual number of blocks is rounded
30473058
down to the nearest multiple of `4 * parallelism`.
3048-
* `passes` {number} Number of passes (iterations). Must be greater than 1 and less
3059+
* `passes` {number} REQUIRED, number of passes (iterations). Must be greater than 1 and less
30493060
than `2**32-1`.
3050-
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView} Random additional input,
3051-
similar to the salt, that should **NOT** be stored with the derived key. Also known
3052-
as a pepper. If used, must have a length not greater than `2**32-1` bytes.
3053-
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional data to
3061+
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView|undefined} OPTIONAL, Random additional input,
3062+
similar to the salt, that should **NOT** be stored with the derived key. This is known as pepper in
3063+
password hashing applications. If used, must have a length not greater than `2**32-1` bytes.
3064+
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView|undefined} OPTIONAL, Additional data to
30543065
be added to the hash, functionally equivalent to salt or secret, but meant for
30553066
non-random data. If used, must have a length not greater than `2**32-1` bytes.
3056-
* `type` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
30573067
* Returns: {Buffer}
30583068

3059-
Provides a synchronous [argon2][] implementation. Argon2 is a password-based
3069+
Provides a synchronous [Argon2][] implementation. Argon2 is a password-based
30603070
key derivation function that is designed to be expensive computationally and
30613071
memory-wise in order to make brute-force attacks unrewarding.
30623072

@@ -3073,25 +3083,35 @@ An exception is thrown when any of the input arguments specify invalid values
30733083
or types.
30743084

30753085
```mjs
3076-
const {
3077-
argon2Sync,
3078-
randomBytes,
3079-
} = await import('node:crypto');
3080-
3081-
const nonce = randomBytes(16);
3082-
const key = argon2Sync({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 });
3083-
console.log(key.toString('hex')); // '3745e48...08d59ae'
3086+
const { argon2Sync, randomBytes } = await import('node:crypto');
3087+
3088+
const parameters = {
3089+
message: 'password',
3090+
nonce: randomBytes(16),
3091+
parallelism: 4,
3092+
tagLength: 64,
3093+
memory: 65536,
3094+
passes: 3,
3095+
};
3096+
3097+
const derivedKey = argon2Sync('argon2id', parameters);
3098+
console.log(derivedKey.toString('hex')); // 'af91dad...9520f15'
30843099
```
30853100

30863101
```cjs
3087-
const {
3088-
argon2Sync,
3089-
randomBytes,
3090-
} = require('node:crypto');
3091-
3092-
const nonce = randomBytes(16);
3093-
const key = argon2Sync({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 });
3094-
console.log(key.toString('hex')); // '3745e48...08d59ae'
3102+
const { argon2Sync, randomBytes } = require('node:crypto');
3103+
3104+
const parameters = {
3105+
message: 'password',
3106+
nonce: randomBytes(16),
3107+
parallelism: 4,
3108+
tagLength: 64,
3109+
memory: 65536,
3110+
passes: 3,
3111+
};
3112+
3113+
const derivedKey = argon2Sync('argon2id', parameters);
3114+
console.log(derivedKey.toString('hex')); // 'af91dad...9520f15'
30953115
```
30963116

30973117
### `crypto.checkPrime(candidate[, options], callback)`

0 commit comments

Comments
 (0)