Files
mtusi2026lab2-fork/test/passwords.mjs

64 lines
2.0 KiB
JavaScript

import assert from 'node:assert/strict';
import { argon2 } from 'node:crypto';
import { createRequire } from 'node:module';
import test from 'node:test';
const require = createRequire(import.meta.url);
const { PasswordService } = require('../dist/Auth/password.service');
const password = '🔐'.repeat(40) + ':Синтетический пароль\0';
test('Argon2id only: PHC format, independent derivation, unique salts, Unicode and invalid hashes', async () => {
const service = new PasswordService();
const first = await service.hash(password);
const second = await service.hash(password);
assert.notEqual(
first,
second,
'same password must have independently salted hashes',
);
assert.match(
first,
/^\$argon2id\$v=19\$m=19456,t=2,p=1\$[A-Za-z0-9+/]{22}\$[A-Za-z0-9+/]{43}$/,
);
assert.ok(!first.includes(password));
const parts = first.split('$');
const independent = await new Promise((resolve, reject) =>
argon2(
'argon2id',
{
message: password,
nonce: Buffer.from(parts[4], 'base64'),
memory: 19456,
passes: 2,
parallelism: 1,
tagLength: 32,
},
(error, key) => (error ? reject(error) : resolve(key)),
),
);
assert.deepEqual(Buffer.from(parts[5], 'base64'), independent);
assert.equal(await service.verify(password, first), true);
assert.equal(
await service.verify(password.replace('🔐', '🔑'), first),
false,
);
assert.equal(
await service.verify(password.slice(0, -1), first),
false,
'NUL must not be silently truncated',
);
for (const invalid of [
null,
'',
'scrypt$131072$8$1$unsupported$unsupported',
'$argon2id$v=19$m=999999999,t=999999999,p=1$x$x',
first.replace('v=19', 'v=16'),
first.replace('argon2id', 'argon2i'),
first + '$extra',
first.replace(parts[5], 'a'),
first.replace(parts[4], parts[4] + '='),
]) {
assert.equal(await service.verify(password, invalid), false);
}
});