我正在使用以下步骤创建一个存储在我的数据库中的哈希:
$ret=randStrGen(100).time();
$gtch = chunk_split($a,1,'.;l89b_-');
$Thm = hash_hmac('sha512', $gtch, file_get_contents('serial.txt'));
$hash =crypt($Thm,sprintf('$2y$%02d$',4).randStrGen(22).$ret);
主要问题是变量$hash
并不总是返回散列值,而是返回*0
。所以它不可靠,我不知道为什么会这样?
编辑:问题已解决
来自crypt()文档的:
CRYPT_BLOWFISH - 河豚用盐调和如下:“$ 2a $”, “$ 2x $”或“$ 2y $”,两位数的成本参数“$”和22个字符 从字母“./0-9A-Za-z”。使用此外的字符 salt中的range将导致crypt()返回零长度字符串。
我用作salt的randStrGen()实际上是使用非字母数字字符,这就是问题的原因。谢谢你们。
答案 0 :(得分:0)
正如您已经发现的那样,salt必须采用特定格式,并不是BCrypt允许所有字符,这就是错误的原因。
您的代码的另一个问题是成本因素,这太低而无法有效保护密码。目前应该至少大约10个。
尽管如此,你绝对应该使用内置函数password_hash()和password_verify(),它们使用最佳实践生成安全盐,以及其他困难部分,例如向后兼容未来算法。
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);