豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 97578f9

Browse files
committed
corrected constant time equals.
1 parent df52193 commit 97578f9

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/src/main/java/org/bouncycastle/crypto/generators/OpenBSDBCrypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private static boolean doCheckPassword(
309309
boolean isEqual = sLength == newBcryptString.length();
310310
for (int i = 0; i != sLength; i++)
311311
{
312-
isEqual &= (bcryptString.indexOf(i) == newBcryptString.indexOf(i));
312+
isEqual &= (bcryptString.charAt(i) == newBcryptString.charAt(i));
313313
}
314314
return isEqual;
315315
}

core/src/test/java/org/bouncycastle/crypto/test/OpenBSDBCryptTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.crypto.test;
22

3+
import java.security.SecureRandom;
4+
35
import org.bouncycastle.crypto.generators.OpenBSDBCrypt;
46
import org.bouncycastle.util.Strings;
57
import org.bouncycastle.util.test.SimpleTest;
@@ -199,6 +201,24 @@ public void performTest()
199201
fail("twoBVec mismatch: " + "[" + i + "] " + password);
200202
}
201203
}
204+
205+
206+
int costFactor = 4;
207+
SecureRandom random = new SecureRandom();
208+
salt = new byte[16];
209+
for (int i = 0; i < 1000; i++)
210+
{
211+
random.nextBytes(salt);
212+
final String tokenString = OpenBSDBCrypt
213+
.generate("test-token".toCharArray(), salt, costFactor);
214+
215+
isTrue(OpenBSDBCrypt.checkPassword(tokenString, "test-token".toCharArray()));
216+
isTrue(!OpenBSDBCrypt.checkPassword(tokenString, "wrong-token".toCharArray()));
217+
}
202218
}
219+
220+
221+
222+
203223
}
204224

0 commit comments

Comments
 (0)