Skip to content

Commit

Permalink
Specify time unit as variable suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Dec 17, 2024
1 parent 55635e6 commit dafbe74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions otp/src/main/java/org/openjax/security/otp/GAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public static String getBarCode(final String key, final String account, final St
public static String getTOTPCode(final String key) {
final String normalizedKey = key.replace(" ", "").toUpperCase();
final byte[] bytes = Base32.decode(normalizedKey, false);
final long time = System.currentTimeMillis() / 1000 / 30;
final String hexTime = Long.toHexString(time);
final long time30Se = System.currentTimeMillis() / 1000 / 30;
final String hexTime = Long.toHexString(time30Se);
return TOTP.generateTOTP(Hexadecimal.encode(bytes), hexTime, 6, Hmac.SHA1);
}

Expand Down
12 changes: 6 additions & 6 deletions otp/src/test/java/org/openjax/security/otp/TOTPTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void test() {
logger.info("| Time(sec) | Time (UTC format) | Value of T(Hex) | TOTP | Mode |");
logger.info("+---------------+-----------------------+------------------+--------+--------+");

final long[] times = {59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L};
for (int i = 0, i$ = times.length; i < i$; ++i) { // [A]
final long time = times[i] / 30;
String steps = Long.toHexString(time).toUpperCase();
final long[] timeSes = {59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L};
for (int i = 0, i$ = timeSes.length; i < i$; ++i) { // [A]
final long time30Se = timeSes[i] / 30;
String steps = Long.toHexString(time30Se).toUpperCase();
if (steps.length() < 16)
steps = Strings.repeat("0", 16 - steps.length()) + steps;

final String fmtTime = String.format("%1$-11s", times[i]);
final String utcTime = dateFormat.format(new Date(times[i] * 1000));
final String fmtTime = String.format("%1$-11s", timeSes[i]);
final String utcTime = dateFormat.format(new Date(timeSes[i] * 1000));
logger.info("| " + fmtTime + " | " + utcTime + " | " + steps + " |" + TOTP.generateTOTP(seed, steps, 8, Hmac.SHA1) + "| SHA1 |");
logger.info("| " + fmtTime + " | " + utcTime + " | " + steps + " |" + TOTP.generateTOTP(seed32, steps, 8, Hmac.SHA256) + "| SHA256 |");
logger.info("| " + fmtTime + " | " + utcTime + " | " + steps + " |" + TOTP.generateTOTP(seed64, steps, 8, Hmac.SHA512) + "| SHA512 |");
Expand Down

0 comments on commit dafbe74

Please sign in to comment.