-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathargon2_tests.ts
More file actions
138 lines (128 loc) · 3.56 KB
/
argon2_tests.ts
File metadata and controls
138 lines (128 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { test } from '../util';
import { argon2Sync, argon2, Buffer } from 'react-native-quick-crypto';
import { assert } from 'chai';
const SUITE = 'argon2';
// RFC 9106 test vector for argon2id
const RFC_PARAMS = {
message: Buffer.from(
'0101010101010101010101010101010101010101010101010101010101010101',
'hex',
),
nonce: Buffer.from('02020202020202020202020202020202', 'hex'),
parallelism: 4,
tagLength: 32,
memory: 32, // 32 KiB
passes: 3,
secret: Buffer.from('0303030303030303', 'hex'),
associatedData: Buffer.from('040404040404040404040404', 'hex'),
version: 0x13,
};
test(SUITE, 'argon2Sync: argon2id produces expected output', () => {
const result = argon2Sync('argon2id', RFC_PARAMS);
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: argon2i produces output', () => {
const result = argon2Sync('argon2i', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: argon2d produces output', () => {
const result = argon2Sync('argon2d', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: different algorithms produce different output', () => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const d = argon2Sync('argon2d', params);
const i = argon2Sync('argon2i', params);
const id = argon2Sync('argon2id', params);
assert.notDeepEqual(d, i);
assert.notDeepEqual(i, id);
assert.notDeepEqual(d, id);
});
test(SUITE, 'argon2Sync: respects tagLength', () => {
const result = argon2Sync('argon2id', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 64,
memory: 64,
passes: 3,
});
assert.strictEqual(result.length, 64);
});
test(SUITE, 'argon2Sync: throws on invalid algorithm', () => {
assert.throws(() => {
argon2Sync('argon2x', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
}, /Unknown argon2 algorithm/);
});
test(SUITE, 'argon2: async produces same result as sync', () => {
return new Promise<void>((resolve, reject) => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const syncResult = argon2Sync('argon2id', params);
argon2('argon2id', params, (err, asyncResult) => {
try {
assert.isNull(err);
assert.deepEqual(
Buffer.from(asyncResult).toString('hex'),
Buffer.from(syncResult).toString('hex'),
);
resolve();
} catch (e) {
reject(e);
}
});
});
});
test(SUITE, 'argon2Sync: deterministic with same inputs', () => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const r1 = argon2Sync('argon2id', params);
const r2 = argon2Sync('argon2id', params);
assert.deepEqual(
Buffer.from(r1).toString('hex'),
Buffer.from(r2).toString('hex'),
);
});