node.js 和 PHP 之间的 SHA1 哈希差异
SHA1 hash differences between node.js and PHP
我想将这部分 node.js 代码转换为 PHP 代码。 (工作中)
function generateHashedPass (password, salt) {
var byteSalt = new Buffer(salt, 'base64');
var bytePass = new Buffer(password, 'ucs2');
var byteResult = Buffer.concat([byteSalt, bytePass]);
return sha1.update(byteResult).digest('base64');
}
console.log(generateHashedPass('111111', 'UY68RQZT14QPgSsfaw/F+w==') === 'L0xc787MxCwJJaZjFX6MqxkVcFE=' ? "Algo correct" : "Algo wrong" );
现在我在 php 中有这样的东西:(不工作)
public function getHashedPass($pass, $salt) {
$base_salt = unpack('H*', base64_decode($salt));
$base_pass = unpack('H*', mb_convert_encoding($pass, 'UCS-2', 'auto'));
$base_result = $base_salt[1] . $base_pass[1];
return base64_encode(sha1($base_result));
}
但是结果和node.js函数不一样
结果应该是这样的: L0xc787MxCwJJaZjFX6MqxkVcFE=
当密码为:111111
盐是: UY68RQZT14QPgSsfaw/F+w==
试试这个:
//----------------------------------------------------
function getCharHex($aString) {
$bytes = str_split($aString, 2);
$result = "";
foreach ($bytes as $byte) {
$result .= chr(hexdec($byte));
}
return $result;
}
//----------------------------------------------------
function getHashedPass($pass, $salt) {
$base_salt = unpack('H*', base64_decode($salt));
$base_pass = unpack('H*', mb_convert_encoding($pass, 'UCS-2LE', 'auto'));
$base_result = getCharHex($base_salt[1].$base_pass[1]);
return base64_encode(sha1($base_result, true));
}
echo getHashedPass('111111', 'UY68RQZT14QPgSsfaw/F+w==');
//L0xc787MxCwJJaZjFX6MqxkVcFE=
我想将这部分 node.js 代码转换为 PHP 代码。 (工作中)
function generateHashedPass (password, salt) {
var byteSalt = new Buffer(salt, 'base64');
var bytePass = new Buffer(password, 'ucs2');
var byteResult = Buffer.concat([byteSalt, bytePass]);
return sha1.update(byteResult).digest('base64');
}
console.log(generateHashedPass('111111', 'UY68RQZT14QPgSsfaw/F+w==') === 'L0xc787MxCwJJaZjFX6MqxkVcFE=' ? "Algo correct" : "Algo wrong" );
现在我在 php 中有这样的东西:(不工作)
public function getHashedPass($pass, $salt) {
$base_salt = unpack('H*', base64_decode($salt));
$base_pass = unpack('H*', mb_convert_encoding($pass, 'UCS-2', 'auto'));
$base_result = $base_salt[1] . $base_pass[1];
return base64_encode(sha1($base_result));
}
但是结果和node.js函数不一样
结果应该是这样的: L0xc787MxCwJJaZjFX6MqxkVcFE=
当密码为:111111
盐是: UY68RQZT14QPgSsfaw/F+w==
试试这个:
//----------------------------------------------------
function getCharHex($aString) {
$bytes = str_split($aString, 2);
$result = "";
foreach ($bytes as $byte) {
$result .= chr(hexdec($byte));
}
return $result;
}
//----------------------------------------------------
function getHashedPass($pass, $salt) {
$base_salt = unpack('H*', base64_decode($salt));
$base_pass = unpack('H*', mb_convert_encoding($pass, 'UCS-2LE', 'auto'));
$base_result = getCharHex($base_salt[1].$base_pass[1]);
return base64_encode(sha1($base_result, true));
}
echo getHashedPass('111111', 'UY68RQZT14QPgSsfaw/F+w==');
//L0xc787MxCwJJaZjFX6MqxkVcFE=