You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
|
2 weeks ago
|
import { IncrementalSha256, hashBlob } from
|
||
|
|
'../public/cloud/template-upload-hash.js';
|
||
|
|
|
||
|
|
const ascii = value => Uint8Array.from(
|
||
|
|
Array.from(value, character => character.charCodeAt(0))
|
||
|
|
);
|
||
|
|
const vectors = [
|
||
|
|
['', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'],
|
||
|
|
['abc', 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'],
|
||
|
|
['The quick brown fox jumps over the lazy dog',
|
||
|
|
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592'],
|
||
|
|
];
|
||
|
|
for (const [input, expected] of vectors) {
|
||
|
|
const hasher = new IncrementalSha256();
|
||
|
|
const bytes = ascii(input);
|
||
|
|
for (let offset = 0; offset < bytes.length; offset += 2) {
|
||
|
|
hasher.update(bytes.subarray(offset, offset + 2));
|
||
|
|
}
|
||
|
|
if (hasher.hex() !== expected) {
|
||
|
|
throw new Error(`Incremental SHA-256 vector failed: ${JSON.stringify(input)}`);
|
||
|
|
}
|
||
|
|
if (typeof Blob === 'function') {
|
||
|
|
const progress = [];
|
||
|
|
const digest = await hashBlob(new Blob([bytes]),
|
||
|
|
(loaded, total) => progress.push([loaded, total]), 1);
|
||
|
|
if (digest !== expected || (bytes.length > 0
|
||
|
|
&& progress.at(-1)?.[0] !== bytes.length)) {
|
||
|
|
throw new Error(`Blob SHA-256 vector failed: ${JSON.stringify(input)}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
(globalThis.print || console.log)(
|
||
|
|
'Template-upload incremental SHA-256 behavior passed.'
|
||
|
|
);
|