8000 OTP support for binary secrets encoded in Base32 by rhyamada · Pull Request #20795 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ public void testHmac() {
System.out.println(hmacOTP.validateHOTP("550233", decoded, 0));
Assert.assertEquals(1, hmacOTP.validateHOTP("550233", decoded, 0));
}
@Test
public void testHmacBase32() {
HmacOTP hmacOTP = new HmacOTP(6, HmacOTP.HMAC_SHA1, 10);
String decoded = "{B32}JNSVMMTEKZCUGSKJIVGHMNSQOZBDA5JT";
System.out.println(hmacOTP.generateHOTP(decoded, 0));
System.out.println(hmacOTP.validateHOTP("550233", decoded, 0));
Assert.assertEquals(1, hmacOTP.validateHOTP("550233", decoded, 0));
}
@Test
public void testHmacBase32Binary() {
// Secret from https://github.com/keycloak/keycloak/issues/11561
// Codes values validated aganist https://totp.app/
HmacOTP hmacOTP = new HmacOTP(6, HmacOTP.HMAC_SHA1, 30);
String decoded = "{B32}CDLYAYRJ73ORTU4PUWWATWSYQCP4H2QL";
String counter = "000000000359675C";
Assert.assertEquals("754397", hmacOTP.generateOTP(decoded, counter, 6, HmacOTP.HMAC_SHA1));
String counter2 = "0000000003596781";
Assert.assertEquals("386679", hmacOTP.generateOTP(decoded, counter2, 6, HmacOTP.HMAC_SHA1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public String generateOTP(String key, String counter, int returnDigits, String c

// Adding one byte to get the right conversion
// byte[] k = hexStr2Bytes(key);
byte[] k = key.getBytes();
byte[] k = key.startsWith("{B32}") ? Base32.decode(key.substring(5)) : key.getBytes(); // if key has {B32} prefix is Base32 encoded

hash = hmac_sha1(crypto, k, msg);

Expand Down
0