Il problema
In un sistema di conferma presa visione documenti, serve dimostrare che il PDF firmato sia esattamente
lo stesso mostrato all'utente — senza un backend dedicato per il calcolo dell'hash. La Web Crypto API
lo permette interamente lato client.
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.
Calcolo hash da Blob
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);
Quando usarlo: audit trail di documenti (presa visione, firme elettroniche semplici), verifica che un file scaricato non sia stato alterato, fingerprint univoco di file caricati dall'utente prima del salvataggio.
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.
Il problema CORS di Firebase Storage
Il primo tentativo di scaricare il PDF con fetch(downloadURL) spesso fallisce silenziosamente —
nessun errore visibile in console, ma il blob resta vuoto. Il motivo: Firebase Storage non configura
automaticamente le policy CORS per richieste fetch() da origini esterne, e il browser blocca
la risposta prima che JavaScript possa vederla.
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
Attenzione: la configurazione CORS è lato bucket, non richiede modifiche al codice — ma va applicata una sola volta tramite gsutil, non esiste un pannello equivalente nella Firebase Console.
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.
Articolo correlato
Firma elettronica con OTP su Firebase — audit trail, hash PDF e certificato con pdf-lib
Related article
Electronic signature with OTP on Firebase — audit trail, PDF hash and pdf-lib certificate
→