如何生成用于散列的随机长盐?

发布于 2024-08-20 12:32:48 字数 64 浏览 2 评论 0原文

PHP 中有什么方法可以生成随机的、可变长度的盐以用于散列?假设我想制作一个 16 个字符的长盐 - 我该怎么做?

What is a way in PHP to make a random, variable length salt for use in hashing? Let's say I want to make a 16-character long salt - how would I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯狂的代价 2024-08-27 12:32:49

好的盐有两个先决条件:一定要长,而且一定要随机。有很多方法可以实现这一点。例如,您可以使用 microtimerand 的组合,但您可能会采取更大的措施来确保您的盐是唯一的。

虽然冲突的可能性可以忽略不计,但请记住,使用 MD5 或其他容易发生冲突的算法对盐进行哈希处理将减少盐无缘无故唯一的可能性。

编辑:用 rand() 替换 mt_rand()。正如 Michael 所说,它比 rand 更好。

There are two prerequisites for a good salt: It must be long, and it must be random. There are many ways to accomplish this. You could use a combination of microtime and rand, for example, but you might go to even greater lengths to ensure that your salt is unique.

While the chance of a collision is neglible, keep in mind that hashing your salt with MD5 or other collision-prone algorithms will reduce the chance that your salt is unique for no reason.

EDIT: Substitute rand() for mt_rand(). As Michael noted, it's better than rand.

断舍离 2024-08-27 12:32:48

编辑: mcrypt 扩展已被弃用。
对于新项目,请查看
random_bytes ( int $length )
sodium (从 php 7.2 core- 开始)扩展。


如果 mcrypt 扩展 可用,您只需使用 mcrypt_create_iv(size, source) 创建盐。

$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
var_dump($iv);

由于“字符串”的每个字节可以在 0-255 之间的范围内,因此您需要一个二进制安全函数来保存/检索它。

edit: the mcrypt extension has been deprecated.
For new projects take a look at
random_bytes ( int $length )
and the sodium (as of php 7.2 core-)extension.


If the mcrypt extension is available you could simply use mcrypt_create_iv(size, source) to create a salt.

$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
var_dump($iv);

Since each byte of the "string" can be in the range between 0-255 you need a binary-safe function to save/retrieve it.

泪意 2024-08-27 12:32:48

根据您的操作系统,例如:

$fh=fopen('/dev/urandom','rb');
$salt=fgets($fh,16);
fclose($fh);

请阅读 random 和 urandom 的行为。

虽然其他人正确地指出 md5 和重复散列存在一些问题,但对于密码(即相对较短的字符串),无论散列算法多么复杂,暴力攻击都需要相同的时间。

C.

depending on your OS, something like:

$fh=fopen('/dev/urandom','rb');
$salt=fgets($fh,16);
fclose($fh);

Do read up on the behaviour of random and urandom.

While others have correctly pointed out that there some issues with md5 and repeated hashing, for passwords (i.e. relatively short strings) brute force attacks take the same amount of time regardless of how sophisticated the hashing algorithm is.

C.

江城子 2024-08-27 12:32:48

这是我找到一个生成随机字符串的函数:

/* random string */
function rand_string( $length ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
    $size = strlen( $chars );
    for( $i = 0; $i < $length; $i++ ) {
        $str .= $chars[ rand( 0, $size - 1 ) ];
    }
    return $str;
}

Here is I found a function to generate random string:

/* random string */
function rand_string( $length ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
    $size = strlen( $chars );
    for( $i = 0; $i < $length; $i++ ) {
        $str .= $chars[ rand( 0, $size - 1 ) ];
    }
    return $str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文