yescrypt-wasm
Version:
WebAssembly module for Yescrypt
69 lines (57 loc) • 2.26 kB
JavaScript
// Reproduction / regression check for the "20x a password" bug (WASM build).
//
// Mirrors node/example.js: hashes a 19-byte and a 20-byte all-"a" password with
// fixed salts and compares the encoded "$y$..." output against the known-good
// values. Before the fix, the 20-byte case mismatched because the password was
// copied to the WASM heap without a NUL terminator and the C side used strlen().
//
// Run: node wasm/example.js (from the repo root, after `cd wasm && yarn && yarn build`)
import { Yescrypt } from './lib/index.js';
const cases = [
{
label: '19x "a"',
passwd: 'a'.repeat(19),
saltHex: '26295eb2e1cdfc39dbf3214f97d00d8b',
expected: '$y$j9T$aYWLm4Snwbnqn5mHL0R190$5MqtyafwmqbTiv/BCpiU9XWsB850JFOmy8BW7quc1iA',
},
{
label: '20x "a"',
passwd: 'a'.repeat(20),
saltHex: '91270e9036831730a9d82fc0937c4ceb',
expected: '$y$j9T$FSW1EOnUL.HeMz0kHm5Hf1$cq6E0/qPVBPCNCQErax5j/.1nE7dRDKNZEAHbdwYtq7',
},
];
const toBytes = (str) => new TextEncoder().encode(str);
const hexToBytes = (hex) => Uint8Array.from(hex.match(/.{1,2}/g).map((b) => parseInt(b, 16)));
const bytesToHex = (bytes) =>
Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
async function main() {
const yescrypt = await Yescrypt.init();
let failures = 0;
for (const { label, passwd, saltHex, expected } of cases) {
const passwdBytes = toBytes(passwd);
const saltBytes = hexToBytes(saltHex);
const result = yescrypt.yescrypt_hash(passwdBytes, saltBytes);
const ok = result === expected;
console.log(`[wasm] passwd with ${label}`);
console.log(` salt hex: ${saltHex}`);
console.log(` password hex: ${bytesToHex(passwdBytes)}`);
console.log(` result: ${result}`);
console.log(` expected: ${expected}`);
console.log(` ${ok ? 'OK' : 'MISMATCH'}\n`);
if (!ok) {
failures++;
}
}
if (failures) {
console.error(`[wasm] ${failures} case(s) FAILED`);
process.exit(1);
}
console.log('[wasm] all cases passed');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});