【问题标题】:Javascript: shasum of SHELL and crypto of JS library are giving different resultsJavascript:SHELL 的 shasum 和 JS 库的 crypto 给出了不同的结果
【发布时间】:2016-10-12 13:08:33
【问题描述】:

这是 Unix 给我的,它是正确的:

shasum -a 256 test.jpg
df94ac3fd72415827f345b5fa22761f57d87e99c25d5345d2e8e9f6c91ef34a3  test.jpg

在 Javascript 中,我无法使用 crypto-browserify 获取此信息。请看我的结果:

img.onload = function(e) {
    console.log(crypto.createHash('sha256').update(e.path[0]).digest('hex'));
    console.log(crypto.createHash('sha256').update(e.path).digest('hex'));
    console.log(crypto.createHash('sha256').update(e.path[0].src).digest('hex'));
    console.log(crypto.createHash('sha256').update(e).digest('hex'));
}

结果是:

da5698be17b9b46962335799779fbeca8ce5d491c0e26243bafef9ea1837a9d8
6e340b9cffb37a989ca54ee6bb780a2c78901d3fb33738768511a30617afa01d
7ce85f64d69c7a8865413deaff3d65ca0272dfbe74ad9bc07s5e28679243cb69
da5698be17b9b46962335799779fbeca8ce5d491csd26243bafef9ea1837a9d8

无法像在shasum 命令行中一样获取df94ac3fd72415827f345b5fa22761f57d87e99c25d5345d2e8e9f6c91ef34a3。你能告诉我应该怎么做才能得到sha吗?

【问题讨论】:

标签: javascript hash browserify sha256 cryptojs


【解决方案1】:

这就是我最终实现的方式:

    var oReq = new XMLHttpRequest();
    oReq.open("GET", "../test.jpg", true);
    oReq.responseType = "arraybuffer";
    oReq.onload = function (oEvent) {
        var arrayBuffer = oReq.response;
        if (arrayBuffer) {
            var byteArray = new Uint8Array(arrayBuffer);
            console.log(crypto.createHash('sha256').update(byteArray).digest('hex'));
        }
    };
    oReq.send(null);

谢谢, 基思。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多