摘要使用不同的数据返回相同的值

时间:2016-06-16 21:58:32

标签: javascript hash sha256 webcryptoapi

我使用Javascript Forge获取某些数据的摘要,现在实验使用不同的文档,以下代码行总是返回相同的哈希:

function obtainData() {
    getDocument(getHash);
}

function getDocument(callback) {
    var file = dInput.files[0];
    var reader = new FileReader();
    reader.onload = function(e){
        var contents = e.target.result;
        var array = callback(contents)
        console.log(array.digest().toHex());
    }
    reader.readAsArrayBuffer(file);
}

function getHash(buffer) {
    digestHash = forge.md.sha256.create();
    digestHash.update(buffer);
    //always the same with different bytes everytime
    console.log(digestHash.digest().toHex());
    return digestHash;
}

为什么会这样?我错过了什么吗?

尝试使用不同的浏览器并且仍然使用相同的digestHash值

3 个答案:

答案 0 :(得分:2)

Forge库通常适用于Uint8。将内容转换为Uint8Array应该可以正常工作

reader.onload = function(e){
    var contents = e.target.result;
    var  binary = arrayBufferToString(contents);
    var array = callback(binary)
    console.log(array.digest().toHex());
}


function arrayBufferToString( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return binary;
}

使用WebCryptographiApi也是一个很好的解决方案

&#13;
&#13;
function obtainData() {
  getDocument(getHash);
}

function arrayBufferToString( buffer ) {
  var binary = '';
  var bytes = new Uint8Array( buffer );
  var len = bytes.byteLength;
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode( bytes[ i ] );
  }
  return binary;
}

function getDocument(callback) {
  var file = dInput.files[0];
  var reader = new FileReader();
  reader.onload = function(e){
    var contents = e.target.result;
    var array = callback(contents)
    console.log(array.digest().toHex());
  }
  reader.readAsArrayBuffer(file);
}

function getHash(buffer) {
  digestHash = forge.md.sha256.create();
  digestHash.update(arrayBufferToString(buffer));
  return digestHash;
}
&#13;
<script src="https://cdn.rawgit.com/artjomb/96b970358e20410fa64daa2e844aeb0f/raw/5375e7171ef297d436d65b962149dcc0e1960b2b/forge_v0.6.39.min.js"></script>
<input id="dInput" type="file">
<button onclick="obtainData()">hash</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

不同文档或数据的所有哈希值相同的原因是,尝试摘要的方法始终引用包含数据的对象([object Object]),因此它将始终获得相同的结果。所以,为了避免这种情况,我决定使用webcrypto。

function getHash(buffer) {
    console.log(buffer);
    var crypto = window.crypto || window.msCrypto;
    var digestHash;

    var promise = crypto.subtle.digest({name:"SHA-256"},
    convertStringToArrayBufferView(buffer));

    promise.then(function (result) {
        digestHash = convertArrayBufferToHexadecimal(result);
        console.log(digestHash);
    });

    return digestHash;
}

function convertStringToArrayBufferView(str) {
    var bytes = new Uint8Array(str.length);
    for(var i = 0; i < str.length; i++){
        bytes[i] = str.charCodeAt(i);
    }
    return bytes;
}

function convertArrayBufferToHexadecimal(buffer) {
    var data_view = new DataView(buffer);
    var i, len, hex = '', c;
    for(i = 0, len = data_view.byteLength; i<len; i+=1){
        c = data_view.getUint8(i).toString(16);
        if(c.length < 2){
            c = '0' + c;
        }
        hex += c;
    }

    return hex;
}

我在http://qnimate.com/找到了这个解决方案。有关于使用WebCrypto进行哈希处理的部分。

答案 2 :(得分:0)

尝试使用范围扩展digestHash:

function getHash(buffer) {
    // in your post the below line is missing 'var'
    // without the below 'var' digestHash will be global
    var digestHash = forge.md.sha256.create(); 
    digestHash.update(buffer);
    //always the same with different bytes everytime
    console.log(digestHash.digest().toHex());
    return digestHash;
}