[英]sha1 + salt -> migrate from php to node.js
我有一個使用php和mysql的現有登錄系統。 我想將其遷移到node.js。 以下代碼是否有相同產品的有用解決方案? 還是重寫包括生成新密碼的所有內容更好?
提前致謝
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
和功能的使用:
public function getUserByNameAndPassword($name, $password) {
$result = mysql_query("SELECT * FROM users WHERE name = '$name'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
和
public function storeUser($name, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
//more code ...
}
看一下本機節點庫:
https://nodejs.org/api/crypto.html
這將允許您正確地進行哈希處理,並且可以從多個引擎中進行哈希選擇。 此外,還有許多加密模塊,例如https://www.npmjs.com/package/bcrypt 。
無論使用哪種方法,您都可以輕松地將相同的php復制為javascript。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.