1
zj
2025-04-17 ff2d1f5acdadc466d7e199028ef385ae8ca277e7
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
package util;
 
/* 
 * Not really a unit test- but it shows usage 
 */
public class GoogleAuthTest {
 
    public static void genSecretTest() {
        String secret = GoogleAuthenticator.generateSecretKey();
        String url = GoogleAuthenticator.getQRBarcodeURL("testuser", "testhost", secret);
        System.out.println("Please register " + url);
        System.out.println("Secret key is " + secret);
    }
 
    // Change this to the saved secret from the running the above test.
    static String savedSecret = "4EWEHLG7CYXLSKTN";
 
    public static void authTest() {
        // enter the code shown on device. Edit this and run it fast before the code
        // expires!
        long code = 350326;
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5); // should give 5 * 30 seconds of grace...
        boolean r = ga.check_code(savedSecret, code, t);
        System.out.println("Check code = " + r);
    }
 
    public static void main(String[] args) {
        authTest();
 
    }
}