Php 使用SHA512进行用户身份验证

Php 使用SHA512进行用户身份验证,php,mysql,hash,openvpn,sha512,Php,Mysql,Hash,Openvpn,Sha512,在将密码输入数据库之前,我使用以下代码对密码进行哈希运算: $user_password = hash( 'sha512', $_POST['user_pass'] ); 我按照本教程的步骤,通过mySQL数据库对OpenVPN用户进行身份验证。在其中,我们设置了以下文件,我相信它可以根据mySQL数据库对请求进行身份验证。剧本是: /etc/openvpn/script/login.sh,它包括以下内容: #!/bin/bash . /etc/openvpn/script/config.sh

在将密码输入数据库之前,我使用以下代码对密码进行哈希运算:

$user_password = hash( 'sha512', $_POST['user_pass'] );
我按照本教程的步骤,通过mySQL数据库对OpenVPN用户进行身份验证。在其中,我们设置了以下文件,我相信它可以根据mySQL数据库对请求进行身份验证。剧本是: /etc/openvpn/script/login.sh,它包括以下内容:

#!/bin/bash
. /etc/openvpn/script/config.sh
##Authentication
user_id=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select user_id from user where user_id = '$username' AND user_pass = '$password' AND user_enable=1 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
##Check user
[ "$user_id" != '' ] && [ "$user_id" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1

只要密码没有被散列,这就可以工作。如何使用相同的方法,但对拥有哈希密码的用户进行身份验证?

SHA*哈希函数不适用于密码,因为它们太快了。看看PHP函数,它将生成一个BCrypt哈希,并负责生成一个安全的salt。对于较旧的PHP版本,也存在一个新的解决方案

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
这也意味着您不能直接在SQL语句中验证密码,而是在第一步中通过用户名从数据库中读取哈希,然后使用此哈希调用password\u verify