PHP中的密码加密

时间:2017-09-07 05:07:51

标签: php codeigniter hash

在Php中加密密码的最佳方法是什么? Codeigniter的文档说密码应该使用php的密码哈希扩展进行哈希处理。到目前为止,我一直在使用codeigniter的加密密钥加密密码。任何建议。

2 个答案:

答案 0 :(得分:5)

将此用于密码哈希

<?php
/**
 * We just want to hash our password using the current DEFAULT algorithm.
 * This is presently BCRYPT, and will produce a 60 character result.
 *
 * Beware that DEFAULT may change over time, so you would want to prepare
 * By allowing your storage to expand past 60 characters (255 would be good)
 */
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";
?>

使用此密码哈希验证

<?php
// See the password_hash() example to see where this came from.
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

if (password_verify('rasmuslerdorf', $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}
?>

答案 1 :(得分:-3)

您还可以使用md5()函数进行密码加密和解密。

以下是示例:

$password = '123456789';
$encrypted_password = md5($password);
echo "Encrypted Password :".$encrypted_password;

-------------------
Output :

Encrypted Password : 25f9e794323b453885f5181f1b624d0b

现在检查输入的密码是否正确(例如登录)从数据库中获取存储的md5密码,你可以这样检查。

$entered_password = '123456789';
$encrypted_password = md5($entered_password);

if($encrypted_password == $password){
echo "Success";
}else{
echo "Fail";
}