fix bug with response body, add test for download hash

This commit is contained in:
ading2210 2024-11-10 17:40:14 -05:00
parent 8e5a23adf2
commit 82249fe707
6 changed files with 31 additions and 4 deletions

View file

@ -0,0 +1,20 @@
//https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#examples
async function digestMessage(msgUint8) {
const hashBuffer = await window.crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string
return hashHex;
}
async function test() {
let url = "https://curl.se/download/curl-8.11.0.tar.gz";
let expected_hash = "264537d90e58d2b09dddc50944baf3c38e7089151c8986715e2aaeaaf2b8118f";
let r = await libcurl.fetch(url);
let msg = new Uint8Array(await r.arrayBuffer());
let hash = await digestMessage(msg);
assert(hash === expected_hash, "unexpected hash of downloaded data");
}