5
5
![ ] ( https://img.shields.io/github/license/BastiaanJansen/OTP-Java )
6
6
![ ] ( https://img.shields.io/github/issues/BastiaanJansen/OTP-Java )
7
7
8
- A small and easy-to-use one-time password generator for Java according to [ RFC 4226] ( https://tools.ietf.org/html/rfc4226 ) (HOTP) and [ RFC 6238] ( https://tools.ietf.org/html/rfc6238 ) (TOTP).
8
+ A small and easy-to-use one-time password generator for Java implementing [ RFC 4226] ( https://tools.ietf.org/html/rfc4226 ) (HOTP) and [ RFC 6238] ( https://tools.ietf.org/html/rfc6238 ) (TOTP).
9
9
10
10
## Table of Contents
11
11
12
12
* [ Features] ( #features )
13
13
* [ Installation] ( #installation )
14
14
* [ Usage] ( #usage )
15
15
* [ HOTP (Counter-based one-time passwords)] ( #hotp-counter-based-one-time-passwords )
16
- * [ TOTP (Time-based one-time passwords)] ( #totp -time-based-one-time-passwords )
16
+ * [ TOTP (Time-based one-time passwords)] ( #totpGenerator -time-based-one-time-passwords )
17
17
* [ Recovery codes] ( #recovery-codes )
18
18
19
19
## Features
@@ -30,7 +30,7 @@ The following features are supported:
30
30
<dependency >
31
31
<groupId >com.github.bastiaanjansen</groupId >
32
32
<artifactId >otp-java</artifactId >
33
- <version >1.3.2 </version >
33
+ <version >2.0.0 </version >
34
34
</dependency >
35
35
```
36
36
@@ -58,17 +58,14 @@ To create a `HOTP` instance, use the `HOTP.Builder` class as follows:
58
58
59
59
``` java
60
60
byte [] secret = " VV3KOX7UQJ4KYAKOHMZPPH3US4CJIMH6F3ZKNB5C2OOBQ6V2KIYHM27Q" . getBytes();
61
- HOTP . Builder builder = new HOTP .Builder (secret);
62
- HOTP hotp = builder. build();
61
+ HOTPGenerator hotp = new HOTPGenerator .Builder (secret). build();
63
62
```
64
63
The above builder creates a HOTP instance with default values for passwordLength = 6 and algorithm = SHA1. Use the builder to change these defaults:
65
64
``` java
66
- HOTP . Builder builder = new HOTP .Builder (secret);
67
- builder
68
- .withPasswordLength(8 )
69
- .withAlgorithm(HMACAlgorithm . SHA256 );
70
-
71
- HOTP hotp = builder. build();
65
+ HOTPGenerator hotp = new HOTPGenerator .Builder (secret)
66
+ .withPasswordLength(8 )
67
+ .withAlgorithm(HMACAlgorithm . SHA256 )
68
+ .build();
72
69
```
73
70
74
71
When you don't already have a secret, you can let the library generate it:
@@ -89,7 +86,6 @@ HOTP hotp = HOTP.fromURI(uri);
89
86
Get information about the generator:
90
87
91
88
``` java
92
- byte [] secret = hotp. getSecret();
93
89
int passwordLength = hotp. getPasswordLength(); // 6
94
90
HMACAlgorithm algorithm = hotp. getAlgorithm(); // HMACAlgorithm.SHA1
95
91
```
@@ -119,37 +115,36 @@ TOTP can accept more paramaters: `passwordLength`, `period`, `algorithm` and `se
119
115
// Generate a secret (or use your own secret)
120
116
byte [] secret = SecretGenerator . generate();
121
117
122
- TOTP . Builder builder = new TOTP .Builder (secret);
123
-
124
- builder
125
- .withPasswordLength(6 )
126
- .withAlgorithm(HMACAlgorithm . SHA1 ) // SHA256 and SHA512 are also supported
127
- .withPeriod(Duration . ofSeconds(30 ));
128
-
129
- TOTP totp = builder. build();
118
+ TOTPGenerator totp = new TOTPGenerator .Builder (secret)
119
+ .withHOTPGenerator(builder - > {
120
+ builder. withPasswordLength(6 );
121
+ builder. withAlgorithm(HMACAlgorithm . SHA1 ); // SHA256 and SHA512 are also supported
122
+ })
123
+ .withPeriod(Duration . ofSeconds(30 ))
124
+ .build();
130
125
```
131
126
Or create a ` TOTP ` instance from an OTPAuth URI:
132
127
``` java
133
- URI uri = new URI (" otpauth://totp /issuer?secret=ABCDEFGHIJKLMNOP&algorithm=SHA1&digits=6&period=30" );
134
- TOTP totp = TOTP . fromURI(uri);
128
+ URI uri = new URI (" otpauth://totpGenerator /issuer?secret=ABCDEFGHIJKLMNOP&algorithm=SHA1&digits=6&period=30" );
129
+ TOTPGenerator totpGenerator = TOTPGenerator . fromURI(uri);
135
130
```
136
131
137
132
Get information about the generator:
138
133
``` java
139
- byte [] secret = totp . getSecret();
140
- int passwordLength = totp . getPasswordLength(); // 6
141
- HMACAlgorithm algorithm = totp . getAlgorithm(); // HMACAlgorithm.SHA1
142
- Duration period = totp . getPeriod(); // Duration.ofSeconds(30)
134
+ byte [] secret = totpGenerator . getSecret();
135
+ int passwordLength = totpGenerator . getPasswordLength(); // 6
136
+ HMACAlgorithm algorithm = totpGenerator . getAlgorithm(); // HMACAlgorithm.SHA1
137
+ Duration period = totpGenerator . getPeriod(); // Duration.ofSeconds(30)
143
138
```
144
139
145
140
#### Generation of TOTP code
146
141
After creating an instance of the TOTP class, a code can be generated by using the ` now() ` method, similarly with the HOTP class:
147
142
``` java
148
143
try {
149
- String code = totp . now();
144
+ String code = totpGenerator . now();
150
145
151
146
// To verify a token:
152
- boolean isValid = totp . verify(code);
147
+ boolean isValid = totpGenerator . verify(code);
153
148
} catch (IllegalStateException e) {
154
149
// Handle error
155
150
}
@@ -159,16 +154,16 @@ The above code will generate a time-based one-time password based on the current
159
154
``` java
160
155
try {
161
156
// Based on current time
162
- totp . now();
157
+ totpGenerator . now();
163
158
164
159
// Based on specific date
165
- totp . at(new Date ());
160
+ totpGenerator . at(new Date ());
166
161
167
162
// Based on seconds past 1970
168
- totp . at(9238346823 );
163
+ totpGenerator . at(9238346823 );
169
164
170
165
// Based on an instant
171
- totp . at(Instant . now());
166
+ totpGenerator . at(Instant . now());
172
167
} catch (IllegalStateException e) {
173
168
// Handle error
174
169
}
@@ -177,9 +172,9 @@ try {
177
172
### Generation of OTPAuth URI's
178
173
To easily generate a OTPAuth URI for easy on-boarding, use the ` getURI() ` method for both ` HOTP ` and ` TOTP ` . Example for ` TOTP ` :
179
174
``` java
180
- TOTP totp = new TOTP .Builder (secret). build();
175
+ TOTPGenerator totpGenerator = new TOTPGenerator .Builder (secret). build();
181
176
182
- URI uri = totp . getURI(" issuer" , " account" ); // otpauth://totp /issuer:account?period=30&digits=6&secret=SECRET&algorithm=SHA1
177
+ URI uri = totpGenerator . getURI(" issuer" , " account" ); // otpauth://totpGenerator /issuer:account?period=30&digits=6&secret=SECRET&algorithm=SHA1
183
178
184
179
```
185
180
0 commit comments