gpt4 book ai didi

java - 为什么 org.mindrot.JBCrypt 在这里说“盐长度错误”?

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:45 25 4
gpt4 key购买 nike

希望这个例子胜过一千字。如果没有,这里有几个测试来对纯文本进行哈希处理 hello world使用盐种子static seed to be used在第一次测试中和 static seed to be usedd在第二次测试中。盐种子用于生成静态盐,可以将其传递到 BCrypt.hashpw(plainText, staticSalt)功能。如您所见,盐字节和盐字节字符串的长度相似,但会引发错误。我知道这很糟糕,但我有静态盐的原因,所以请把注意力集中在这个问题上。

使用 JDK1.7 测试 1 的 org.mindrot.jbcrypt.BCrypt - 纯文本:“hello world”,saltseed:“要使用的静态种子”:

Salt bytes for "static seed to be used": [-30, -8, 86, -8, 6, -126, -64, -30, -82, -82, -104, -64, -8, -118, -64, 108, -82, -64, 14, -30, -82, -104]
Salt bytes string: 4vhW+AaCwOKurpjA+IrAbK7ADuKumA==, length: 32
complete salt: $2a$12$4vhW+AaCwOKurpjA+IrAbK7ADuKumA==
Exception in thread "main" java.lang.IllegalArgumentException: Bad salt length
at org.mindrot.jbcrypt.BCrypt.crypt_raw(BCrypt.java:619)
at org.mindrot.jbcrypt.BCrypt.hashpw(BCrypt.java:684)

使用 JDK1.8 测试 1 的 org.springframework.security.crypto.bcrypt.BCrypt - PlainText:“hello world”,saltseed:“要使用的静态种子”:

Salt bytes for "static seed to be used": [-30, -8, 86, -8, 6, -126, -64, -30, -82, -82, -104, -64, -8, -118, -64, 108, -82, -64, 14, -30, -82, -104]
Salt bytes string: 4vhW+AaCwOKurpjA+IrAbK7ADuKumA==, length: 32
complete salt: $2a$12$4vhW+AaCwOKurpjA+IrAbK7ADuKumA==
Plain text: hello world, Hash text: $2a$12$4vhWHrTxEMtyyv6wmpOtX.YYbTqHwHv/dxe

使用 JDK1.7 测试 2 的 org.mindrot.jbcrypt.BCrypt - 纯文本:“hello world”,saltseed:“要使用的静态种子”:

Salt bytes for "static seed to be usedd": [85, 108, -73, 108, 111, -27, -32, 85, 19, 19, -4, -32, 108, -7, -32, -50, 19, -32, -125, 85, 19, -4]
Salt bytes string: VWy3bG/l4FUTE/zgbPngzhPgg1UT/A==, length: 32
complete salt: $2a$12$VWy3bG/l4FUTE/zgbPngzhPgg1UT/A==
Plain text: hello world, Hash text: $2a$12$VWy3bG/l4FUTE/zgbPngze9KDSXjF72NBMBNE6ZJk4StahyAhykgO

使用 JDK1.8 测试 2 的 org.springframework.security.crypto.bcrypt.BCrypt - PlainText:“hello world”,saltseed:“要使用的静态种子”:

Salt bytes for "static seed to be usedd": [85, 108, -73, 108, 111, -27, -32, 85, 19, 19, -4, -32, 108, -7, -32, -50, 19, -32, -125, 85, 19, -4]
Salt bytes string: VWy3bG/l4FUTE/zgbPngzhPgg1UT/A==, length: 32
complete salt: $2a$12$VWy3bG/l4FUTE/zgbPngzhPgg1UT/A==
Plain text: hello world, Hash text: $2a$12$VWy3bG/l4FUTE/zgbPngze9KDSXjF72NBMBNE6ZJk4StahyAhykgO

我尝试添加和删除更多字母并获得成功的哈希值。我很高兴 JUnit 测试中使用的第一个 String 抛出了错误。

提前致谢。

最佳答案

我通过查看 GitHub 页面上的实现找到了原因...BCrypt 的此实现支持 base64 点(.) 字符,而不是标准的加号(+) 字符。

static private final byte index_64[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 0, 1, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, -1, -1,
-1, -1, -1, -1, -1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
-1, -1, -1, -1, -1, -1, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, -1, -1, -1, -1, -1
};

“+”字符的整数值为 43,因此从该数组返回 -1,并且从盐中检索盐字节的函数提前中断,留下了 4 个字节的盐。甚至它的实现也表示它不支持标准的 Base64 编码字符串:

/**
* Decode a string encoded using bcrypt's base64 scheme to a
* byte array. Note that this is *not* compatible with
* the standard MIME-base64 encoding.
*/
static byte[] decode_base64(String s, int maxolen)
throws IllegalArgumentException {

从 . to + 为我提供了与 Spring 中包含 + 的盐不同的哈希值。不太熟悉 shift 和 & 来进行更改,因此将寻找 Spring-Security 的实现。

编辑:刚刚看了一下 Spring 的实现。难怪它不起作用...除了 Spring 继续使用 4 字节盐进行哈希处理而 jbcrypt 抛出错误之外,完全相同,因此 Spring 中存在错误,而如果您生成自己的哈希并且它包含 +,则 checkpw(plainText, hashedText) 将返回 false,因为 hashtedText 的生成的 salt 部分与用户生成的 salt 不同。

关于java - 为什么 org.mindrot.JBCrypt 在这里说“盐长度错误”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53771795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com