SHA-256 hash of a PDF with Web Crypto API, client-side
How to compute the SHA-256 hash of a PDF file directly in the browser with
crypto.subtle.digest — for audit trails and document integrity verification, no external libraries.
The problem
In a document acknowledgement system, you need to prove the signed PDF is exactly the one shown to the user — without a dedicated backend for hash computation. The Web Crypto API allows this entirely client-side.
Hash computation from Blob
async function calcolaHashPdf(pdfBlob) { const arrayBuffer = await pdfBlob.arrayBuffer(); const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer); const hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); } // Scarica blob da Firebase Storage e calcola hash prima di mostrare il documento const pdfRef = storageRef(storage, item.pdfPath); const url = await getDownloadURL(pdfRef); const res = await fetch(url); const blob = await res.blob(); const hash = await calcolaHashPdf(blob);
When to use it: document audit trails (acknowledgements, simple electronic signatures), verifying a downloaded file hasn't been altered, unique fingerprinting of user-uploaded files before saving.
The Firebase Storage CORS problem
The first attempt to download the PDF with fetch(downloadURL) often fails silently — no
visible error in console, but the blob stays empty. The reason: Firebase Storage doesn't automatically
configure CORS policies for fetch() requests from external origins, and the browser blocks
the response before JavaScript can see it.
[
{
"origin": ["https://tuodominio.it"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
# Richiede Google Cloud SDK installato gsutil cors set cors.json gs://TUO-BUCKET.appspot.com # Verifica configurazione applicata gsutil cors get gs://TUO-BUCKET.appspot.com
Warning: CORS configuration is bucket-level and requires no code changes — but it must be applied once via gsutil; there's no equivalent panel in the Firebase Console.