Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这种密码散列和匹配方法有多安全?_Php_Security_Hash_Cryptography_Passwords - Fatal编程技术网

Php 这种密码散列和匹配方法有多安全?

Php 这种密码散列和匹配方法有多安全?,php,security,hash,cryptography,passwords,Php,Security,Hash,Cryptography,Passwords,我从一系列帖子和一些先验知识中获取信息,以实现以下哈希算法。然而,有很多关于什么实现是安全的和不安全的讨论。我的方法怎么样?安全吗 public static function sha512($token,$cost = 50000,$salt = null) { $salt = ($salt == null) ? (generateToken(32)) : ($salt); $salt = '$6$rounds=' . $cost . '$' . $salt .

我从一系列帖子和一些先验知识中获取信息,以实现以下哈希算法。然而,有很多关于什么实现是安全的和不安全的讨论。我的方法怎么样?安全吗

public static function sha512($token,$cost = 50000,$salt = null) {
        $salt = ($salt == null) ? (generateToken(32)) : ($salt);
        $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';
        return crypt($token, $salt);
}

public static function sha512Equals($token,$hash) {
    return (crypt($token,$hash) == $hash);
}


public static function generateToken($length,$characterPool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    $token = '';
    $max = mb_strlen($characterPool);

    for ($i = 0;$i < $length;$i++){
        $token .= $characterPool[cryptorand(0,$max)];
    }

    return $token;
}

public static function cryptorand($min, $max) {
    $range = $max - $min;

    if ($range < 0) 
        return $min;

    $log = log($range, 2);
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $bits) - 1; // set all lower bits to 1

    do {
        $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd = $rnd & $filter; // discard irrelevant bits
    } while ($rnd >= $range);

    return $min + $rnd;
}
公共静态函数sha512($token,$cost=50000,$salt=null){
$salt=($salt==null)?(generateToken(32)):($salt);
$salt='$6$rounds='.$cost'.$'.$salt'.$';
返回密码($token,$salt);
}
公共静态函数sha512Equals($token,$hash){
返回(crypt($token,$hash)=$hash);
}
公共静态函数generateToken($length,$characterPool='0123456789ABCDEFGHIjklmnopqrStuvxyzABCDEFGHIjklmnopqrStuvxyz'){
$token='';
$max=mb_strlen($characterPool);
对于($i=0;$i<$length;$i++){
$token.=$characterPool[cryptorand(0,$max)];
}
返回$token;
}
公共静态函数cryptorand($min,$max){
$range=$max-$min;
如果($range<0)
返回$min;
$log=log($range,2);
$bytes=(int)($log/8)+1;//字节长度
$bits=(int)$log+1;//以位为单位的长度
$filter=(int)(1=$range);
返回$min+$rnd;
}

那么这种方法安全吗?PHP中是否有更安全的方法用于散列令牌和稍后与令牌匹配?任何批评都是非常感谢的。

不,因为您最终信任
crypt
,并且您没有在
sha512Equals
中使用时间常数比较

也可能存在特定于平台的问题:
openssl\u random\u pseudo\u bytes
不需要加密安全。我不知道你怎么知道
crypt
也使用SHA-512

您在
cryptorand
中的计算稍有偏差(例如,
$log
的值正好位于字节边界上),但幸运的是do/while循环一直在检查这一点



请改用
密码\u散列
密码\u验证
功能。

最好把这篇文章发到这是给瑞士银行的,lol。这就足够了。@MubinKhalid可能……没有,但很多信息都存储在用户帐户中,现在有很多简单的方法来散列密码,我想我最好花一两个小时来确保我是以最好的方式完成的。在PHP中,你应该简单地使用and函数,因为它们实现了所有的最佳实践。顺便问一下,
cryptorand()
函数从何而来,可以将0..count作为参数传递,还是应该是0..count-1?@martinstoeckli使用内置功能的建议是可以的,但是
cryptorand()
似乎已经包含在问题的来源中。“考虑到PHP的安全状态,我不确定它们是否完全安全,但至少你可以责怪其他人”它们与
crypt(3)一样安全
实际上是。如果你好奇的话,可以向@ircmaxell询问详情:)嗨@HurricaneDevelopment,这足以回答这个问题吗?@HurricaneDevelopment我会接受Maarten的回答。这是切中要害,没有事实上的不准确之处。(我只是回应了他关于不确定他们是否完全安全的附加声明。)