新旧版本为什么 md5值不一样
发布于 7 年前 作者 yakczh 3762 次浏览 来自 分享

测试代码





String.prototype.hexEncode = function(){
    var hex, i;

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += (" "+hex).slice(-4);
    }

    return result
}



var crypto=require('crypto');
var fs=require('fs');

var arr=[0x0,0x0,0x4d,0x67,0x0,0x0,0xa,0x28,0x5a,0x73,0xde,0x69,0x57,0xdc,0x4a,0x85];
const srcdata = new Buffer(arr, 'binary');
var md5= crypto.createHash('md5').update(srcdata).digest('hex');
console.log(md5)


fs.writeFileSync('md5',srcdata, 'binary');

var data=fs.readFileSync('md5', 'binary');
console.log(data.hexEncode());

 md5= crypto.createHash('md5').update(data).digest('hex');
 console.log(md5)

直接用字节初始化,md5值是对的,但是经过写入文件,再读出来 node 4.2 是一致的, 最新7.0算出的md5值不一样

d74fb898905fdc30d20f75456feb9c1f 0 0 4d 67 0 0 a 28 5a 73 de 69 57 dc 4a 85 a0becbcc2e7e48bb0585dc7d9ff16b76

1 回复

应该是这里的问题 4.x 的binary是 https://github.com/nodejs/node/blob/v4.x/lib/buffer.js#L426 但是到了7.x就输出成字符串 https://github.com/nodejs/node/blob/3d353c749cdb64b2d018766d05ba0e9b9b0f7ec8/lib/buffer.js#L478

一个ascii字符和一个二进制数据的区别不用再说了吧。

回到顶部