@fgv/ts-web-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,124 @@
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 { BrowserArgon2Provider } 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('BrowserArgon2Provider', () => {
37
+ let provider: BrowserArgon2Provider;
38
+
39
+ beforeEach(() => {
40
+ provider = BrowserArgon2Provider.create().orThrow();
41
+ });
42
+
43
+ describe('create()', () => {
44
+ test('returns a provider instance', () => {
45
+ expect(BrowserArgon2Provider.create()).toSucceedAndSatisfy((p) => {
46
+ expect(p).toBeInstanceOf(BrowserArgon2Provider);
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
+
93
+ describe('parameter validation', () => {
94
+ test('fails when memoryKiB < 8', async () => {
95
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, memoryKiB: 7 })).toFailWith(
96
+ /memoryKiB must be >= 8/i
97
+ );
98
+ });
99
+
100
+ test('fails when iterations < 1', async () => {
101
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, iterations: 0 })).toFailWith(
102
+ /iterations must be >= 1/i
103
+ );
104
+ });
105
+
106
+ test('fails when parallelism < 1', async () => {
107
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, parallelism: 0 })).toFailWith(
108
+ /parallelism must be 1\.\.255/i
109
+ );
110
+ });
111
+
112
+ test('fails when parallelism > 255', async () => {
113
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, parallelism: 256 })).toFailWith(
114
+ /parallelism must be 1\.\.255/i
115
+ );
116
+ });
117
+
118
+ test('fails when outputBytes < 4', async () => {
119
+ expect(await provider.argon2id(RFC_PASSWORD, RFC_SALT, { ...RFC_PARAMS, outputBytes: 3 })).toFailWith(
120
+ /outputBytes must be >= 4/i
121
+ );
122
+ });
123
+ });
124
+ });
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
+ }