PHP有密码_verify错误吗?

发布于 2025-01-31 04:03:07 字数 469 浏览 2 评论 0原文

以下代码段不应发出“匹配”,因为密码'testtest'与“ TestTestest”不匹配,而是对我来说是php 7.4.3。我做错了吗?

<?php
$sPass = 'testtesttest';
$sSalt = hash('sha256','this is my salt');
$sShadow = password_hash($sSalt . $sPass,PASSWORD_BCRYPT);
echo (password_verify($sSalt . 'testtest',$sShadow) ? 'MATCHED' : 'nomatch');

请注意,如果删除上面的盐引用,则代码正常。就像PHP的password_hash和password_verify函数具有大小限制,如果字符串比许多字符更长,它们将不再准确。

因此,我认为这是一个错误。

The following code snippet should not emit 'MATCHED' because the password 'testtest' does not match 'testtesttest', but does on PHP 7.4.3 for me. Am I doing something wrong?

<?php
$sPass = 'testtesttest';
$sSalt = hash('sha256','this is my salt');
$sShadow = password_hash($sSalt . $sPass,PASSWORD_BCRYPT);
echo (password_verify($sSalt . 'testtest',$sShadow) ? 'MATCHED' : 'nomatch');

Note, if you remove the salt references above, the code works fine. It's like the password_hash and password_verify functions of PHP have a size limitation where they no longer become accurate if the string is longer than so many characters.

So, I'm thinking this is a bug.

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

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

发布评论

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

评论(1

猛虎独行 2025-02-07 04:03:07

bcrypt只能处理72个字符,您的盐占64个字符,因此只有8个字符,因此只有8个字符考虑密码。

bcrypt函数的输入是密码字符串(最多72个字节),数字成本和16字节(128位)盐值。

使用盐的二进制形式不“浪费”,因为password_hash无论如何都会生成一个。

BCrypt can only handle 72 characters, your salt takes up 64 characters, so only 8 characters of your password are considered.

The input to the bcrypt function is the password string (up to 72 bytes), a numeric cost, and a 16-byte (128-bit) salt value.

Use the binary form of your salt to not "waste" as many characters or just don't use one at all, as password_hash will generate one anyway.

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