-3

我正在尝试使用 Mcrypt's 生成盐mcrypt_create_iv(),但它似乎不起作用并且我遇到了错误。

这是我的代码:

<?php
$salt= substr(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM))(mt_rand()),0,22);

echo $salt;
?>
4

2 回答 2

1
$salt = substr( mcrypt_create_iv(16, MCRYPT_DEV_URANDOM), mt_rand( 0, 22 ) );

你有一些语法错误

于 2013-05-16T22:22:18.810 回答
1

这是行不通的,你mcrypt_create_iv()用来获取随机字节,但这些不能用于 BCrypt 散列。问题是,mcrypt_create_iv 返回二进制数据,而 BCrypt 需要一个带有给定字母字符的盐。您必须将您的盐编码为这个字母表:./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. 该功能mt_rand()在这里没有用。

PHP 5.5 将有自己的函数 password_hash() 和 password_verify() 准备好,以简化生成 BCrypt 密码哈希。我强烈推荐使用这个优秀的 api,或者它是早期 PHP 版本的兼容包。用法非常简单:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);

// 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);
于 2013-05-17T10:45:43.900 回答