BCrypt 似乎验证密码不正确

问题描述 投票:0回答:1

我需要一些帮助来了解这里发生的事情。这个测试证明了我遇到的问题。我有一个很长的密码,我使用 BCrypt 对其进行哈希处理。然后我尝试验证实际值和哈希值。我面临的问题是,库似乎对我修改后的密码也返回 true。最后一个断言就是发生这种情况的地方。我从原始密码中删除了最后 10 个字符,为什么 BCrypt 仍会匹配哈希值?

org.mindrot:jbcrypt:0.4

@Test
fun thisTestFails() {
    val password = "YtWSXeKO3J_qhI7jArzsM5eSETrEfVpAutbCrNwx0yHOK9axNDvPQyUOLi-MapE4uUI2YVgOQjI-SCsAg0FmIJ8Ei1jBxpSrFEvDnZDEPc7ClLhfdr1RA5ruSr9qZvebLFXka-1Gl8TMscGbVZKHsYqveq5p7hnMYqrT"
    val passwordHash = BCrypt.hashpw(password, BCrypt.gensalt())
    val modified = password.dropLast(10)
    assertNotEquals(password, modified)
    assertTrue(BCrypt.checkpw(password, passwordHash))
    assertFalse(BCrypt.checkpw(modified, passwordHash)) // Fails here
}
java kotlin bcrypt
1个回答
0
投票

评论很到位,该库最多仅支持 54 字节。这个测试证实了这一点。

EQUAL=false
从 54 字节开始。谢谢评论里的帮忙。

@Test
fun test() {
    for (n in 100 downTo 0) {
        val secureRandom = SecureRandom()
        val bytes = ByteArray(n)
        secureRandom.nextBytes(bytes)
        val password = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes)

        val passwordHash = BCrypt.hashpw(password, BCrypt.gensalt())
        val modified = password.dropLast(1)
        assertNotEquals(password, modified)

        val equal = BCrypt.checkpw(password, passwordHash) && BCrypt.checkpw(modified, passwordHash)
        println("BYTES=$n EQUAL=$equal")
    }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.