繁体   English   中英

这种密码哈希和匹配方法的安全性如何?

[英]How secure is this method of password hashing and matching?

我从一系列文章中获取了信息,并获得了一些先验知识,以实现以下哈希算法。 但是,关于什么实现是安全的和不安全的讨论很多。 我的方法如何衡量? 安全吗?

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;
}

那么这种方法安全吗? PHP中是否存在用于对令牌进行哈希处理并稍后与令牌进行匹配的更安全的方法? 任何批评都将受到高度赞赏。

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

可能还存在特定于平台的问题: openssl_random_pseudo_bytes不必一定是加密安全的。 我不确定您怎么知道crypt使用SHA-512。

您在cryptorand中的计算略有偏离(例如,对于恰好在字节边界上的$log值),但是幸运的是,do / while循环可对其进行检查。


请改用password_hashpassword_verify功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM