'use strict'; const { test } = require('node:test'); const assert = require('node:assert'); const crypto = require('node:crypto'); const Sha256 = require('./sha256'); const ref = (buf) => crypto.createHash('sha256').update(buf).digest('hex'); test('known vectors', () => { assert.strictEqual(Sha256.hex(new Uint8Array(0)), 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'); assert.strictEqual(Sha256.hex(new TextEncoder().encode('abc')), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'); assert.strictEqual( Sha256.hex(new TextEncoder().encode('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'); }); test('every length around block boundaries matches node:crypto', () => { for (let n = 0; n <= 200; n++) { const buf = crypto.randomBytes(n); assert.strictEqual(Sha256.hex(new Uint8Array(buf)), ref(buf), 'length ' + n); } }); test('random chunk boundaries give the same digest as one shot', () => { const buf = crypto.randomBytes(3 * 1024 * 1024 + 17); for (let round = 0; round < 5; round++) { const h = Sha256.create(); let pos = 0; while (pos < buf.length) { const n = Math.min(buf.length - pos, 1 + Math.floor(Math.random() * 200000)); h.update(new Uint8Array(buf.buffer, buf.byteOffset + pos, n)); pos += n; } assert.strictEqual(h.hex(), ref(buf)); assert.strictEqual(h.bytes, buf.length); } }); test('update after digest throws; isHex', () => { const h = Sha256.create(); h.hex(); assert.throws(() => h.update(new Uint8Array(1))); assert.ok(Sha256.isHex(ref(Buffer.from('x')))); assert.ok(!Sha256.isHex('ABC')); });