I have a project that need migrate php code to node.js. But node.js hash result is not same as php. After investigation, the packing seems has different with php packing. Is any code that need to implement in node.js?
PHP:
$halfHashLen = Protector::$HASH_LEN >> 1;
//iterative hashing with split and insert back itself
for ($i = 100; --$i >=0;){
// same as node.js
print_r("pack(\"H*\", substr(\$data, 0, \$halfHashLen)) - base64 ===> ".base64_encode(pack("H*", substr($data, 0, $halfHashLen)))."<br>");
// same as node.js
print_r("pack(\"H*\", \$data) - base64 ===> ".base64_encode(pack("H*", $data))."<br>");
// same as node.js
print_r("pack(\"H*\", substr(\$data, \$halfHashLen, \$halfHashLen)) - base64 ===> ".base64_encode(pack("H*", substr($data, $halfHashLen, $halfHashLen)))."<br>");
// not same as node.js
print_r("pack(\"H*\", substr(\$data, \$halfHashLen, \$halfHashLen)) ".pack("H*", substr($data, $halfHashLen, $halfHashLen))."<br>");
// not as with node.js
$data = hash("sha256",pack("H*", substr($data, 0, $halfHashLen)) . pack("H*", $data) . pack("H*", substr($data, $halfHashLen, $halfHashLen)));
}
Node.js
var halfHashLen = this.HASH_LEN >> 1;
for(var i = 100; --i>=0; )
{
// same as PHP
console.log("new Buffer(data.substr(0,halfHashLen),\"hex\") - base64 ===> "+new Buffer(new Buffer(data.substr(0,halfHashLen),"hex")).toString("base64"));
// same as PHP
console.log("new Buffer(data,\"hex\") - base64 ===> "+new Buffer(new Buffer(data,"hex")).toString("base64"));
// same as PHP
console.log("new Buffer(data.substr(halfHashLen,halfHashLen),\"hex\") - base64 ===> "+new Buffer(new Buffer(data.substr(halfHashLen,halfHashLen),"hex")).toString("base64"));
// not same as PHP
console.log("new Buffer(data.substr(halfHashLen,halfHashLen),\"hex\") ===> "+new Buffer(data.substr(halfHashLen,halfHashLen),"hex")); // not same as PHP
// not same as PHP
data = crypto.createHash('sha256').update((new Buffer(data.substr(0,halfHashLen),"hex")+new Buffer(data,"hex")+new Buffer(data.substr(halfHashLen,halfHashLen),"hex"))).digest('hex');
}
via Jacky Shek
No comments:
Post a Comment