@fgv/ts-extras-argon2 5.1.0-27

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.
@@ -0,0 +1,112 @@
1
+ // Copyright (c) 2026 Erik Fortune
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ // SOFTWARE.
20
+
21
+ import '@fgv/ts-utils-jest';
22
+ import { NodeArgon2Provider } from '../../../packlets/argon2';
23
+ import { BrowserArgon2Provider } from '@fgv/ts-web-extras-argon2';
24
+ import { CryptoUtils } from '@fgv/ts-extras';
25
+
26
+ // RFC 9106 §B.3 parameter set (no secret/AD — our IArgon2idParams interface does not expose
27
+ // those optional Argon2 fields). Both implementations must agree on this exact output.
28
+ const RFC_PARAMS: CryptoUtils.IArgon2idParams = {
29
+ memoryKiB: 32,
30
+ iterations: 3,
31
+ parallelism: 4,
32
+ outputBytes: 32
33
+ };
34
+ const RFC_PASSWORD = new Uint8Array(32).fill(0x01);
35
+ const RFC_SALT = new Uint8Array(16).fill(0x02);
36
+
37
+ // Verified independently against both argon2 (kelektiv) and hash-wasm.
38
+ const RFC_EXPECTED_HEX = '03aab965c12001c9d7d0d2de33192c0494b684bb148196d73c1df1acaf6d0c2e';
39
+
40
+ // Parameter sweep — covers variation in each dimension independently.
41
+ interface ITestCase {
42
+ label: string;
43
+ password: Uint8Array | string;
44
+ salt: Uint8Array;
45
+ params: CryptoUtils.IArgon2idParams;
46
+ }
47
+
48
+ const SWEEP: ITestCase[] = [
49
+ {
50
+ label: 'RFC §B.3 base case',
51
+ password: RFC_PASSWORD,
52
+ salt: RFC_SALT,
53
+ params: RFC_PARAMS
54
+ },
55
+ {
56
+ label: 'string password',
57
+ password: 'correct horse battery staple',
58
+ salt: RFC_SALT,
59
+ params: RFC_PARAMS
60
+ },
61
+ {
62
+ label: 'higher memoryKiB',
63
+ password: RFC_PASSWORD,
64
+ salt: RFC_SALT,
65
+ params: { ...RFC_PARAMS, memoryKiB: 64 }
66
+ },
67
+ {
68
+ label: 'more iterations',
69
+ password: RFC_PASSWORD,
70
+ salt: RFC_SALT,
71
+ params: { ...RFC_PARAMS, iterations: 5 }
72
+ },
73
+ {
74
+ label: 'parallelism=1',
75
+ password: RFC_PASSWORD,
76
+ salt: RFC_SALT,
77
+ params: { ...RFC_PARAMS, parallelism: 1 }
78
+ },
79
+ {
80
+ label: 'longer output (64 bytes)',
81
+ password: RFC_PASSWORD,
82
+ salt: RFC_SALT,
83
+ params: { ...RFC_PARAMS, outputBytes: 64 }
84
+ },
85
+ {
86
+ label: 'different salt',
87
+ password: RFC_PASSWORD,
88
+ salt: new Uint8Array(16).fill(0x07),
89
+ params: RFC_PARAMS
90
+ }
91
+ ];
92
+
93
+ describe('cross-runtime equivalence: NodeArgon2Provider vs BrowserArgon2Provider', () => {
94
+ let node: NodeArgon2Provider;
95
+ let browser: BrowserArgon2Provider;
96
+
97
+ beforeEach(() => {
98
+ node = NodeArgon2Provider.create().orThrow();
99
+ browser = BrowserArgon2Provider.create().orThrow();
100
+ });
101
+
102
+ test('RFC §B.3 parameter set matches known-good hex output', async () => {
103
+ const nodeBytes = (await node.argon2id(RFC_PASSWORD, RFC_SALT, RFC_PARAMS)).orThrow();
104
+ expect(Buffer.from(nodeBytes).toString('hex')).toBe(RFC_EXPECTED_HEX);
105
+ });
106
+
107
+ test.each(SWEEP)('$label: outputs are byte-identical', async ({ password, salt, params }) => {
108
+ const nodeBytes = (await node.argon2id(password, salt, params)).orThrow();
109
+ const browserBytes = (await browser.argon2id(password, salt, params)).orThrow();
110
+ expect(nodeBytes).toEqual(browserBytes);
111
+ });
112
+ });
@@ -0,0 +1,129 @@
1
+ // Copyright (c) 2026 Erik Fortune
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ // SOFTWARE.
20
+
21
+ import '@fgv/ts-utils-jest';
22
+ import { NodeArgon2Provider } from '../../../packlets/argon2';
23
+ import { CryptoUtils } from '@fgv/ts-extras';
24
+
25
+ // RFC 9106 §B.3 parameter set (no secret/AD — our interface does not expose those fields).
26
+ // t=3, m=32, p=4, tag=32. Used for determinism and cross-runtime agreement.
27
+ const RFC_PARAMS: CryptoUtils.IArgon2idParams = {
28
+ memoryKiB: 32,
29
+ iterations: 3,
30
+ parallelism: 4,
31
+ outputBytes: 32
32
+ };
33
+ const RFC_PASSWORD = new Uint8Array(32).fill(0x01);
34
+ const RFC_SALT = new Uint8Array(16).fill(0x02);
35
+
36
+ describe('NodeArgon2Provider', () => {
37
+ let provider: NodeArgon2Provider;
38
+
39
+ beforeEach(() => {
40
+ provider = NodeArgon2Provider.create().orThrow();
41
+ });
42
+
43
+ describe('create()', () => {
44
+ test('returns a provider instance', () => {
45
+ expect(NodeArgon2Provider.create()).toSucceedAndSatisfy((p) => {
46
+ expect(p).toBeInstanceOf(NodeArgon2Provider);
47
+ });
48
+ });
49
+ });
50
+
51
+ describe('argon2id()', () => {
52
+ test('derives a key with Uint8Array password', async () => {
53
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, RFC_PARAMS)).toSucceedAndSatisfy((bytes) => {
54
+ expect(bytes).toBeInstanceOf(Uint8Array);
55
+ expect(bytes.length).toBe(RFC_PARAMS.outputBytes);
56
+ });
57
+ });
58
+
59
+ test('derives a key with string password', async () => {
60
+ expect(await provider.argon2id('password', RFC_SALT, RFC_PARAMS)).toSucceedAndSatisfy((bytes) => {
61
+ expect(bytes).toBeInstanceOf(Uint8Array);
62
+ expect(bytes.length).toBe(RFC_PARAMS.outputBytes);
63
+ });
64
+ });
65
+
66
+ test('is deterministic — same inputs produce identical bytes', async () => {
67
+ const r1 = (await provider.argon2id(RFC_PASSWORD, RFC_SALT, RFC_PARAMS)).orThrow();
68
+ const r2 = (await provider.argon2id(RFC_PASSWORD, RFC_SALT, RFC_PARAMS)).orThrow();
69
+ expect(r1).toEqual(r2);
70
+ });
71
+
72
+ test('different passwords produce different bytes', async () => {
73
+ const a = (await provider.argon2id(new Uint8Array(32).fill(0x01), RFC_SALT, RFC_PARAMS)).orThrow();
74
+ const b = (await provider.argon2id(new Uint8Array(32).fill(0x02), RFC_SALT, RFC_PARAMS)).orThrow();
75
+ expect(a).not.toEqual(b);
76
+ });
77
+
78
+ test('different salts produce different bytes', async () => {
79
+ const a = (await provider.argon2id(RFC_PASSWORD, new Uint8Array(16).fill(0x02), RFC_PARAMS)).orThrow();
80
+ const b = (await provider.argon2id(RFC_PASSWORD, new Uint8Array(16).fill(0x03), RFC_PARAMS)).orThrow();
81
+ expect(a).not.toEqual(b);
82
+ });
83
+
84
+ test('respects outputBytes', async () => {
85
+ expect(
86
+ await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, outputBytes: 64 })
87
+ ).toSucceedAndSatisfy((bytes) => {
88
+ expect(bytes.length).toBe(64);
89
+ });
90
+ });
91
+
92
+ test('returns failure when underlying library rejects (salt too short)', async () => {
93
+ const tinySalt = new Uint8Array(1).fill(0x02); // argon2 requires >= 8 bytes
94
+ expect(await provider.argon2id(RFC_PASSWORD, tinySalt, RFC_PARAMS)).toFail();
95
+ });
96
+ });
97
+
98
+ describe('parameter validation', () => {
99
+ test('fails when memoryKiB < 8', async () => {
100
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, memoryKiB: 7 })).toFailWith(
101
+ /memoryKiB must be >= 8/i
102
+ );
103
+ });
104
+
105
+ test('fails when iterations < 1', async () => {
106
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, iterations: 0 })).toFailWith(
107
+ /iterations must be >= 1/i
108
+ );
109
+ });
110
+
111
+ test('fails when parallelism < 1', async () => {
112
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, parallelism: 0 })).toFailWith(
113
+ /parallelism must be 1\.\.255/i
114
+ );
115
+ });
116
+
117
+ test('fails when parallelism > 255', async () => {
118
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, parallelism: 256 })).toFailWith(
119
+ /parallelism must be 1\.\.255/i
120
+ );
121
+ });
122
+
123
+ test('fails when outputBytes < 4', async () => {
124
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, outputBytes: 3 })).toFailWith(
125
+ /outputBytes must be >= 4/i
126
+ );
127
+ });
128
+ });
129
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./node_modules/@rushstack/heft-node-rig/profiles/default/tsconfig-base.json",
3
+ "compilerOptions": {
4
+ "resolveJsonModule": true,
5
+ "types": ["heft-jest", "node"],
6
+ "lib": ["es2020", "DOM"]
7
+ }
8
+ }