|
1 | 1 | package com.bastiaanjansen.otp;
|
2 | 2 |
|
| 3 | +import java.time.Clock; |
3 | 4 | import java.time.Duration;
|
4 | 5 |
|
5 | 6 | public class ExampleApp {
|
6 | 7 | public static void main(String[] args) {
|
7 | 8 |
|
8 | 9 | // 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 | + } |
28 | 32 | }
|
29 | 33 | }
|
0 commit comments