Skip to content

Commit ad55568

Browse files
updated example app
1 parent ef3d9a7 commit ad55568

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
package com.bastiaanjansen.otp;
22

3+
import java.time.Clock;
34
import java.time.Duration;
45

56
public class ExampleApp {
67
public static void main(String[] args) {
78

89
// Generate a secret, if you don't have one already
9-
// byte[] secret = SecretGenerator.generate();
10-
//
11-
// // Create a TOTPGenerate instance
12-
// TOTPGenerator.Builder builder = new TOTPGenerator.Builder(secret);
13-
// TOTPGenerator totpGenerator = builder
14-
// .withPasswordLength(6)
15-
// .withAlgorithm(HMACAlgorithm.SHA1)
16-
// .withPeriod(Duration.ofMinutes(15))
17-
// .build();
18-
//
19-
// try {
20-
// String code = totpGenerator.now();
21-
// System.out.println("Generated code: " + code);
22-
//
23-
// // To verify a codes
24-
// totpGenerator.verify(code); // true
25-
// } catch (IllegalStateException e) {
26-
// e.printStackTrace();
27-
// }
10+
byte[] secret = SecretGenerator.generate();
11+
Clock clock = Clock.systemUTC();
12+
13+
// Create a TOTPGenerate instance
14+
TOTPGenerator totpGenerator = new TOTPGenerator.Builder(secret)
15+
.withHOTPGenerator(builder -> {
16+
builder.withAlgorithm(HMACAlgorithm.SHA1);
17+
builder.withPasswordLength(6);
18+
})
19+
.withClock(clock)
20+
.withPeriod(Duration.ofMinutes(15))
21+
.build();
22+
23+
try {
24+
String code = totpGenerator.now();
25+
System.out.println("Generated code: " + code);
26+
27+
// To verify a codes
28+
totpGenerator.verify(code); // true
29+
} catch (IllegalStateException e) {
30+
e.printStackTrace();
31+
}
2832
}
2933
}

0 commit comments

Comments
 (0)