[英]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_hash
或password_verify
功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.