在 Linux 终端中获取不同于 sha256sum 的 Subtlecrypto SHA-256 摘要并尝试获取文件摘要

问题描述 投票:0回答:1

我在 linux 终端中使用 SHA256 算法并使用 crypto.subtle 为同一字符串获取不同的 hexdigest。这是我得到的:

$ echo "foobar" | sha256sum
aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f  -

并使用 crypto.subtle:

crypto.subtle.digest("SHA-256", new TextEncoder().encode("foobar")).then((hash) => {
    console.log("rohdas hash " + hash);
    let arr = [...new Uint8Array(hash)].map(c => c.toString(16).padStart(2, '0')).join('');     
    console.log(arr); }).catch(err => console.log(err));
}
//c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2

我也在尝试生成 zip 文件的 SHA256 摘要,但遇到了一些 XRayWrapper 错误。

这是文件摘要的代码:

let reader = new FileReader();
reader.readAsArrayBuffer(new File('/data/myfile.zip'));
reader.onloadend = () => {
console.log(reader.result + " " + reader.result.byteLength);//[object ArrayBuffer] 336
window.crypto.subtle.digest('SHA-256', reader.result).then((calculatedHash) => {// warning1 here
    console.log("calculatedHash " + calculatedHash);//calculatedHash [object Opaque]
    let calculatedHashHex = [...new Uint8Array(calculatedHash)];// error2 here
}).catch((err) => {console.log("digest error " + err);});
*warning1*
JavaScript Warning: "XrayWrapper denied access to property Symbol.toPrimitive (reason: object is not safely Xrayable). See https://developer.mozilla.org/en-US/docs/Xray_vision for more information. Note that only the first denied property access from a given global object will be reported."
*error2*
JavaScript Error: "Error: Accessing TypedArray data over Xrays is slow, and forbidden in order to encourage performant code. To copy TypedArrays across origin boundaries, consider using Components.utils.cloneInto()."
hash filereader sha256 subtlecrypto
1个回答
0
投票

我能够生成文件的哈希值。即使对于字符串它不匹配(可能是编码等问题),文件哈希与 sha256sum 值匹配。我使用的“窗口”对象实例存在一些问题。结束这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.