demo · v138

SRI fingerprint generator

A real, end-to-end use case: fetch a script’s bytes, hash with SubtleCrypto, and emit both the SRI integrity="sha384-…" attribute and the cdn-cgi/sri hex digest a CDN console wants. Side-by-side: the new toBase64() / toHex() calls versus the legacy btoa(String.fromCharCode) dance.

checking Uint8Array.prototype.toBase64 support…

138: Uint8Array.prototype.toBase64 / toHex

const bytes = new Uint8Array(await (await fetch(url)).arrayBuffer());
const hash  = new Uint8Array(await crypto.subtle.digest("SHA-384", bytes));
const b64   = hash.toBase64();                  // NEW
const hex   = hash.toHex();                     // NEW
return { integrity: "sha384-" + b64, hex };

legacy: btoa(String.fromCharCode.apply(...))

const bytes = new Uint8Array(await (await fetch(url)).arrayBuffer());
const hash  = new Uint8Array(await crypto.subtle.digest("SHA-384", bytes));
let s = "";
for (let i = 0; i < hash.length; i++) s += String.fromCharCode(hash[i]);
const b64 = btoa(s);                            // legacy
const hex = [...hash].map(b => b.toString(16).padStart(2,"0")).join("");
return { integrity: "sha384-" + b64, hex };
bytes fetched SHA-384 (hex) SRI integrity match (138 vs legacy)
no timing yet

see also