I have this two function in php.
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted_password = base64_encode(sha1($password . $salt, true).$salt);
$hash = array("salt"=>$salt, "encrypted"=>$encrypted_password);
return $hash;
}
//Password Decryption
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true).$salt);
return $hash;
}
I am trying to write this two functions in node js.
Here is something I tried.
const hash = crypto.createHash('sha1', 'my different salt from DB');
hash.update(password);
console.log(hash.digest('base64'));
But they both produced different results.
These Node.js functions should be equivalent to your PHP code:
const crypto = require("crypto");
function hashSSHA(password){
let salt = crypto.createHash('sha1').update(crypto.randomBytes(8)).digest('base64');
salt = salt.substring(0,10);
const hash = crypto.createHash('sha1');
hash.update(password + salt);
return {
salt: salt,
encrypted: Buffer.concat([hash.digest(), Buffer.from(salt)]).toString('base64')
};
};
function checkhashSSHA(salt, password) {
const hash = crypto.createHash('sha1');
hash.update(password + salt);
return Buffer.concat([hash.digest(), Buffer.from(salt)]).toString('base64');
}
const password = "some password";
const hashResult = hashSSHA(password);
console.log("Hash result: ", hashResult);
console.log("Check hash result: ", checkhashSSHA(hashResult.salt, password));