PHP SHA1 给出的结果与预期不同

发布于 2024-12-01 10:03:22 字数 823 浏览 0 评论 0原文

这真的让我感到惊讶 - 这应该相当简单,但我不明白有什么区别。

我有这个函数来生成盐:

private function _generateSalt($max = 128)
{
    $characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()~";
    $i = 0;
    $salt = "";
    do {
        $salt .= $characterList{mt_rand(0,strlen($characterList)-1)};
         $i++;
    } while ($i < $max);
    return $salt;
}

非常基本(?)

并尝试从中创建 SHA1 哈希,给了我一个与我期望的不同的结果:

$salt = $this->_generateSalt();
$password = $salt.$password;
echo sha1($password);

$password 是由用户输入生成的字符串。 回显的哈希字符串是错误的。我不知道为什么。

var_dump($password); 在添加盐后给出了预期的字符串大小 - 将结果复制并粘贴到在线 SHA1 服务或通过 MySQL CLI 对字符串进行哈希处理给出了正确的结果。就像 $password 变量中有一些看不见的东西我不想散列。但我怎样才能找出为什么会发生这种情况呢? var_dump()、trim() 和比较结果没有让我有任何收获?

This really surprises me - this should be rather simple, but I can't figure out what the difference is.

I have this function to generate a salt:

private function _generateSalt($max = 128)
{
    $characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()~";
    $i = 0;
    $salt = "";
    do {
        $salt .= $characterList{mt_rand(0,strlen($characterList)-1)};
         $i++;
    } while ($i < $max);
    return $salt;
}

Pretty basic(?)

And trying to create a SHA1 hash from this, gives me a different result what I would expect:

$salt = $this->_generateSalt();
$password = $salt.$password;
echo sha1($password);

$password is a string generated by user input.
The echoed hashed string is wrong. And I don't know why.

var_dump($password); after prepending the salt gives me the expected string size - copy and paste the result to an online SHA1 service or hashing the string through MySQL CLI gives the correct result. It's like there's something invisible in the $password variable I wan't to hash. But how can I find out why this is happening? var_dump(), trim() and comparing results haven't gotten me anywhere?

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

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

发布评论

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

评论(3

逐鹿 2024-12-08 10:03:22

去掉所有非字母数字特殊字符 - 据我所知,这个字符甚至不是 ASCII:¤。因此,如果您在以不同编码编码的字符串下运行 sha1,可能会弄乱事情。

(这个答案是从评论中复制粘贴的,并由于提问者的要求而添加为答案,因为它似乎解决了问题)

Get rid of all the non alpha-numeric special characters - this one is even not ASCII as far as I can tell: ¤. So it might mess things up if you run sha1 under string encoded in different encodings.

(This answer is copy pasted from the comments and added as an answer because of the asker's request as it seem to fix the problem)

你怎么这么可爱啊 2024-12-08 10:03:22

生成盐的更好方法是:

// True for cryptographically strong, FALSE otherwise
$salt_strong = TRUE;

// The length
$salt_length = 32;

// Create the salt and load the results into a local var
$salt = bin2hex( openssl_random_pseudo_bytes( $salt_length , $salt_strong ) );

A better way to generate a salt would be:

// True for cryptographically strong, FALSE otherwise
$salt_strong = TRUE;

// The length
$salt_length = 32;

// Create the salt and load the results into a local var
$salt = bin2hex( openssl_random_pseudo_bytes( $salt_length , $salt_strong ) );
请持续率性 2024-12-08 10:03:22

这行不应该是:
$salt .= $characterList{mt_rand(0,strlen($characterList)-1)};

如下所示: $salt .= $characterList[mt_rand(0,strlen($characterList) )-1)];

Shouldn't this line:
$salt .= $characterList{mt_rand(0,strlen($characterList)-1)};

Look like this: $salt .= $characterList[mt_rand(0,strlen($characterList)-1)];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文