@pirxpilot/argon2 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/argon2.d.ts +76 -0
- package/argon2.js +7 -13
- package/package.json +4 -2
package/argon2.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
declare module 'argon2' {
|
|
2
|
+
/**
|
|
3
|
+
* Hashes a password with Argon2, producing a raw hash
|
|
4
|
+
*
|
|
5
|
+
* @overload
|
|
6
|
+
* @param {Buffer | string} password The plaintext password to be hashed
|
|
7
|
+
* @param {Options & { raw: true }} options The parameters for Argon2
|
|
8
|
+
* @returns {Promise<Buffer>} The raw hash generated from `password`
|
|
9
|
+
*/
|
|
10
|
+
export function hash(
|
|
11
|
+
password: Buffer | string,
|
|
12
|
+
options: Options & {
|
|
13
|
+
raw: true;
|
|
14
|
+
}
|
|
15
|
+
): Promise<Buffer>;
|
|
16
|
+
/**
|
|
17
|
+
* Hashes a password with Argon2, producing an encoded hash
|
|
18
|
+
*
|
|
19
|
+
* @overload
|
|
20
|
+
* @param {Buffer | string} password The plaintext password to be hashed
|
|
21
|
+
* @param {Options & { raw?: boolean }} [options] The parameters for Argon2
|
|
22
|
+
* @returns {Promise<string>} The encoded hash generated from `password`
|
|
23
|
+
*/
|
|
24
|
+
export function hash(
|
|
25
|
+
password: Buffer | string,
|
|
26
|
+
options?: Options & {
|
|
27
|
+
raw?: boolean;
|
|
28
|
+
}
|
|
29
|
+
): Promise<string>;
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} digest The digest to be checked
|
|
32
|
+
* @param {Object} [options] The current parameters for Argon2
|
|
33
|
+
* @param {number} [options.timeCost=3]
|
|
34
|
+
* @param {number} [options.memoryCost=65536]
|
|
35
|
+
* @param {number} [options.parallelism=4]
|
|
36
|
+
* @param {number} [options.version=0x13]
|
|
37
|
+
* @returns {boolean} `true` if the digest parameters do not match the parameters in `options`, otherwise `false`
|
|
38
|
+
*/
|
|
39
|
+
export function needsRehash(
|
|
40
|
+
digest: string,
|
|
41
|
+
options?: {
|
|
42
|
+
timeCost?: number;
|
|
43
|
+
memoryCost?: number;
|
|
44
|
+
parallelism?: number;
|
|
45
|
+
version?: number;
|
|
46
|
+
}
|
|
47
|
+
): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} digest The digest to be checked
|
|
50
|
+
* @param {Buffer | string} password The plaintext password to be verified
|
|
51
|
+
* @param {Object} [options] The current parameters for Argon2
|
|
52
|
+
* @param {Buffer} [options.secret]
|
|
53
|
+
* @returns {Promise<boolean>} `true` if the digest parameters matches the hash generated from `password`, otherwise `false`
|
|
54
|
+
*/
|
|
55
|
+
export function verify(
|
|
56
|
+
digest: string,
|
|
57
|
+
password: Buffer | string,
|
|
58
|
+
options?: {
|
|
59
|
+
secret?: Buffer;
|
|
60
|
+
}
|
|
61
|
+
): Promise<boolean>;
|
|
62
|
+
export const argon2d: 0;
|
|
63
|
+
export const argon2i: 1;
|
|
64
|
+
export const argon2id: 2;
|
|
65
|
+
export type Options = {
|
|
66
|
+
hashLength?: number;
|
|
67
|
+
timeCost?: number;
|
|
68
|
+
memoryCost?: number;
|
|
69
|
+
parallelism?: number;
|
|
70
|
+
type?: 0 | 1 | 2;
|
|
71
|
+
version?: number;
|
|
72
|
+
salt?: Buffer;
|
|
73
|
+
associatedData?: Buffer;
|
|
74
|
+
secret?: Buffer;
|
|
75
|
+
};
|
|
76
|
+
}
|
package/argon2.js
CHANGED
|
@@ -2,6 +2,7 @@ import { argon2, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
import { deserialize, serialize } from '@phc/format';
|
|
4
4
|
|
|
5
|
+
/** @type {(id: string, options: object) => Promise<Buffer>} */
|
|
5
6
|
const asyncArgon2 = promisify(argon2);
|
|
6
7
|
|
|
7
8
|
/** @type {(size: number) => Promise<Buffer>} */
|
|
@@ -11,15 +12,7 @@ export const argon2d = 0;
|
|
|
11
12
|
export const argon2i = 1;
|
|
12
13
|
export const argon2id = 2;
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
const types = Object.freeze({ argon2d, argon2i, argon2id });
|
|
16
|
-
|
|
17
|
-
/** @enum {'argon2d' | 'argon2i' | 'argon2id'} */
|
|
18
|
-
const names = Object.freeze({
|
|
19
|
-
[types.argon2d]: 'argon2d',
|
|
20
|
-
[types.argon2i]: 'argon2i',
|
|
21
|
-
[types.argon2id]: 'argon2id'
|
|
22
|
-
});
|
|
15
|
+
const names = ['argon2d', 'argon2i', 'argon2id'];
|
|
23
16
|
|
|
24
17
|
const defaults = {
|
|
25
18
|
hashLength: 32,
|
|
@@ -36,8 +29,8 @@ const defaults = {
|
|
|
36
29
|
* @property {number} [timeCost=3]
|
|
37
30
|
* @property {number} [memoryCost=65536]
|
|
38
31
|
* @property {number} [parallelism=4]
|
|
39
|
-
* @property {
|
|
40
|
-
* @property {number} [version=
|
|
32
|
+
* @property {argon2d | argon2i | argon2id} [type=argon2id]
|
|
33
|
+
* @property {number} [version=0x13]
|
|
41
34
|
* @property {Buffer} [salt]
|
|
42
35
|
* @property {Buffer} [associatedData]
|
|
43
36
|
* @property {Buffer} [secret]
|
|
@@ -150,7 +143,8 @@ export function needsRehash(digest, options = {}) {
|
|
|
150
143
|
*/
|
|
151
144
|
export async function verify(digest, password, options = {}) {
|
|
152
145
|
const { id, ...rest } = deserialize(digest);
|
|
153
|
-
if (!(id
|
|
146
|
+
if (!names.includes(id)) {
|
|
147
|
+
console.error(`Unknown Argon2 id: ${id}`);
|
|
154
148
|
return false;
|
|
155
149
|
}
|
|
156
150
|
|
|
@@ -163,7 +157,7 @@ export async function verify(digest, password, options = {}) {
|
|
|
163
157
|
const { secret = Buffer.alloc(0) } = options;
|
|
164
158
|
|
|
165
159
|
return timingSafeEqual(
|
|
166
|
-
await asyncArgon2(
|
|
160
|
+
await asyncArgon2(id, {
|
|
167
161
|
message: Buffer.from(password),
|
|
168
162
|
nonce: salt,
|
|
169
163
|
tagLength: hash.byteLength,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pirxpilot/argon2",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "An Argon2 library for Node",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"argon2",
|
|
@@ -17,8 +17,10 @@
|
|
|
17
17
|
"author": "Ranieri Althoff <ranisalt+argon2@gmail.com>",
|
|
18
18
|
"type": "module",
|
|
19
19
|
"exports": "./argon2.js",
|
|
20
|
+
"types": "./argon2.d.ts",
|
|
20
21
|
"files": [
|
|
21
|
-
"argon2.js"
|
|
22
|
+
"argon2.js",
|
|
23
|
+
"argon2.d.ts"
|
|
22
24
|
],
|
|
23
25
|
"scripts": {
|
|
24
26
|
"test": "make check"
|