forked from j256/two-factor-auth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TwoFactorAuthExample.java
40 lines (33 loc) · 1.35 KB
/
TwoFactorAuthExample.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.j256.totp;
/**
* Little test program to show how to use the TwoFactorAuthUtil utility class.
*
* See: https://github.com/j256/java-two-factor-auth
*
* @author graywatson
*/
public class TwoFactorAuthExample {
public static void main(String[] args) throws Exception {
TwoFactorAuthUtil twoFactorAuthUtil = new TwoFactorAuthUtil();
// String base32Secret = twoFactorAuthUtil.generateBase32Secret();
String base32Secret = "NY4A5CPJZ46LXZCP";
System.out.println("secret = " + base32Secret);
// this is the name of the key which can be displayed by the authenticator program
String keyId = "[email protected]";
// generate the QR code
System.out.println("Image url = " + twoFactorAuthUtil.qrImageUrl(keyId, base32Secret));
// we can display this image to the user to let them load it into their auth program
// we can use the code here and compare it against user input
String code = twoFactorAuthUtil.generateCurrentNumber(base32Secret);
/*
* this loop shows how the number changes over time
*/
while (true) {
long diff = TwoFactorAuthUtil.TIME_STEP_SECONDS
- ((System.currentTimeMillis() / 1000) % TwoFactorAuthUtil.TIME_STEP_SECONDS);
code = twoFactorAuthUtil.generateCurrentNumber(base32Secret);
System.out.println("Secret code = " + code + ", change in " + diff + " seconds");
Thread.sleep(1000);
}
}
}