这也是你应该这样做的方式。请注意,这段代码是我的做法,您可能想要更改其中的一些内容。而且你必须定义自己独特的盐,无论是在配置文件中还是在其他地方。它必须 a) 在我发布的全局范围内,或者您可以更改它以使其在函数中定义。你也没有加密,你实际上是在散列。加密是两种方式,散列是一种方式的加密。这意味着您无法解密哈希。您只能暴力猜测原始纯文本。
/*
* Copyright (c) 2012, Macarthur Inbody
* The following code was posted on http://*.com/questions/8195689/encryption-using-crypt
* The license is simply CC-by https://creativecommons.org/licenses/by/3.0/
*
*
*
*/
/*
*
* This is used to hash their password.
*
* @param $password string the users supplied password
* @param $username string the users supplied username
* @param $rand_salt int the secondary salt -2^31-1 to 2^31-1 Must be defined previously.
* @return string the hashed password
*/
function hash_pass($username,$password,$rand_salt){
global $unique_salt;
$main_salt=base64_encode(hash('sha512',$username.$password.$config_salt);
$main_salt=str_replace('+', '.', $salt);
$main_salt=str_replace('=','/',$salt);
$main_salt='$2$06'.$main_salt; //change this here to the cost factor that you want
$hashed=crypt($unique_salt.$username.$password.$rand_salt,$main_salt);
return $hashed;
}
function gen_rand_salt(){
return rand();
}
function rand_str($length,$additional_entropy){
$max_length=ceil($length/28);
if(!is_defined($additional_entropy)){
$additional_entropy='';
}
$str='';
for($i=0;$i<=$max_length;++$i){
$str.=base64_encode(sha1($i.''.microtime().$additional_entropy,true));
}
$str=substr($str,0,$length);
return $str;
}
/*
*
* Generate A temp password/token
*
* This function generates a temporary password and also gives you
* the hashed password too. It is an array, arr[0]=password, arr[1]=
* hashed password. If it fails it'll return -1;
*
* @param $username the username
* @param $rand_salt the random salt value, must be given.
*
* @return array if it is successful array, if it fails it's a number of -1
*/
function generate_temp_password($username,$rand_salt){
global $unique_salt;
if(!is_defined($rand_salt)){
return -1;
}
$pass_len=12; // change this to what you want for password recovery
$pass_arr=Array();
$password=rand_str($pass_len,$unique_salt.rand().$rand_salt);
$password=substr(base64_encode(sha1($rand_str.$rand_salt,true)),0,$pass_len);
$hashed_password=hash_pass($username,$password,$rand_salt);
$pass_arr[0]=$password;
$pass_arr[1]=$hashed_password;
return $pass_arr;
}
正如代码中所述,许可证是 CC-By,因为我认为它对于大多数事情来说已经足够了。另外,请保持块与此页面的链接相同,因为这是我对所有自己的源代码所做的。我也意识到“随机”字符串并不是真的那么随机,但它的随机性足以让您可以使用它来达到预期目的。
编辑2:还要确保转义用户的用户名。我没有转义密码,因为我正在散列它,因此没有必要转义它,因为它已经得到缓解并且只会浪费周期。但是只有如果你正在做这样的事情。确保使用mysql_real_escape_string 转义用户名。如果您使用的是 php5+,则应该查看 mysqli(如果您使用的是 mysql)。如果您使用的是其他系统,那么您必须自己查找它,因为我只知道 mysql。我将离开几天,所以我真的希望这对你有用。我会不时检查它,但我可能会忘记......所以是的。我希望这对您有所帮助,因为它安全可靠,并且应该适合您。
编辑 3:更改了随机字符串函数以使其稍微更强,因为我忘记了这将用于生成临时密码。这应该使其足够随机以用于此目的,否则生成的密码可能会被知道确切时间(使用当前微时间)的人知道,尽管不太可能,但这仍然使它更强大并且应该使它免受此类攻击。它不应该完全准备好生产,并且应该对您的系统安全。只需确保在全局范围内的某个位置设置 $unique_salt 变量,或者在每个函数中使用它时都设置它。