[英]nodejs: how to cut md5 digest into 4 long number by javascript
我的代码在下面,但有时它显示负数,我认为我做错了。 因此,我添加了最大数以使其大于零,但最好使用二进制运算。
var buff2hash = function(buff, part) {
var hash = buff[part * 4 + 3] << 24 |
buff[part * 4 + 2] << 16 |
buff[part * 4 + 1] << 8 |
buff[part * 4];
//return hash // this number will be negative sometimes
return hash < 0 ? hash + 4294967296 : hash;
};
var md5hashcode = exports.md5hashcode = function(key) {
return buff2hash(new Buffer(crypto.createHash('md5').update(key).digest()), 0);
};
哈希变量的最高有效位是符号。 要将哈希“黑客转换”为无符号的32位整数,请执行以下操作:
return hash>>>0;
(忘掉您在“返回”行中拥有的所有hoopla,只需执行上述操作即可。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.