我一直在研究生成盐的最好方法。一般的建议似乎是使用mcrype_create_iv或openssl_random_pseudo_bytes来生成。T
这样做的问题是,由于涉及的字符,我在使用检索到的值时遇到了问题。我使用RedBean作为对象关系管理,使用Silex作为框架。我在使用RedBean设置和检索生成的值时遇到了问题,我知道Silex也有局限性,因为我曾经收到一个错误,即salt不能包含分隔符"{}“。
生成使用标准字符集的salt的最佳方式是什么?我认为我也许能够对结果执行md5操作,但这样得到的字符集要小得多。
发布于 2013-03-08 16:45:36
要生成有效的盐,必须知道哪个算法将使用该盐。通常,您可以使用函数base64_encode()从二进制盐中检索标准字符,它将生成一个具有以下字母表的字符串:
base64 encoding alphabeth: +/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
如果Silex将盐用于BCrypt散列算法,它将使用以下字母表的盐,请注意“。代替"+":
BCrypt hash alphabet: ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
我认为最好测试哪些字符是允许的,或者找出将使用哪种算法,否则迟早会生成无效的salt。使用函数mcrypt_create_iv()生成BCrypt盐的示例如下所示:
/**
* Generates a random salt for using with the BCrypt algorithm.
* @param int $length Number of characters the string should have.
* @return string A random salt.
*/
function sto_generateRandomSalt($length = 22)
{
if (!defined('MCRYPT_DEV_URANDOM')) die('The MCRYPT_DEV_URANDOM source is required (PHP 5.3).');
// Generate random bytes, using the operating system's random source.
// Since PHP 5.3 this also uses the random source on a Windows server.
// Unlike /dev/random, the /dev/urandom does not block the server, if
// there is not enough entropy available.
$randomBinaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
// BCrypt expects nearly the same alphabet as base64_encode returns,
// but instead of the '+' characters it accepts '.' characters.
// BCrypt alphabet: ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
$randomEncodedString = str_replace('+', '.', base64_encode($randomBinaryString));
return substr($randomEncodedString, 0, $length);
}https://stackoverflow.com/questions/15195684
复制相似问题