Skip to content

Commit e957203

Browse files
2.0.0 with redesigned api
1 parent 857d3e9 commit e957203

13 files changed

+1117
-1242
lines changed

README.md

+29-34
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
![](https://img.shields.io/github/license/BastiaanJansen/OTP-Java)
66
![](https://img.shields.io/github/issues/BastiaanJansen/OTP-Java)
77

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).
99

1010
## Table of Contents
1111

1212
* [Features](#features)
1313
* [Installation](#installation)
1414
* [Usage](#usage)
1515
* [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)
1717
* [Recovery codes](#recovery-codes)
1818

1919
## Features
@@ -30,7 +30,7 @@ The following features are supported:
3030
<dependency>
3131
<groupId>com.github.bastiaanjansen</groupId>
3232
<artifactId>otp-java</artifactId>
33-
<version>1.3.2</version>
33+
<version>2.0.0</version>
3434
</dependency>
3535
```
3636

@@ -58,17 +58,14 @@ To create a `HOTP` instance, use the `HOTP.Builder` class as follows:
5858

5959
```java
6060
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();
6362
```
6463
The above builder creates a HOTP instance with default values for passwordLength = 6 and algorithm = SHA1. Use the builder to change these defaults:
6564
```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();
7269
```
7370

7471
When you don't already have a secret, you can let the library generate it:
@@ -89,7 +86,6 @@ HOTP hotp = HOTP.fromURI(uri);
8986
Get information about the generator:
9087

9188
```java
92-
byte[] secret = hotp.getSecret();
9389
int passwordLength = hotp.getPasswordLength(); // 6
9490
HMACAlgorithm algorithm = hotp.getAlgorithm(); // HMACAlgorithm.SHA1
9591
```
@@ -119,37 +115,36 @@ TOTP can accept more paramaters: `passwordLength`, `period`, `algorithm` and `se
119115
// Generate a secret (or use your own secret)
120116
byte[] secret = SecretGenerator.generate();
121117

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();
130125
```
131126
Or create a `TOTP` instance from an OTPAuth URI:
132127
```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);
135130
```
136131

137132
Get information about the generator:
138133
```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)
143138
```
144139

145140
#### Generation of TOTP code
146141
After creating an instance of the TOTP class, a code can be generated by using the `now()` method, similarly with the HOTP class:
147142
```java
148143
try {
149-
String code = totp.now();
144+
String code = totpGenerator.now();
150145

151146
// To verify a token:
152-
boolean isValid = totp.verify(code);
147+
boolean isValid = totpGenerator.verify(code);
153148
} catch (IllegalStateException e) {
154149
// Handle error
155150
}
@@ -159,16 +154,16 @@ The above code will generate a time-based one-time password based on the current
159154
```java
160155
try {
161156
// Based on current time
162-
totp.now();
157+
totpGenerator.now();
163158

164159
// Based on specific date
165-
totp.at(new Date());
160+
totpGenerator.at(new Date());
166161

167162
// Based on seconds past 1970
168-
totp.at(9238346823);
163+
totpGenerator.at(9238346823);
169164

170165
// Based on an instant
171-
totp.at(Instant.now());
166+
totpGenerator.at(Instant.now());
172167
} catch (IllegalStateException e) {
173168
// Handle error
174169
}
@@ -177,9 +172,9 @@ try {
177172
### Generation of OTPAuth URI's
178173
To easily generate a OTPAuth URI for easy on-boarding, use the `getURI()` method for both `HOTP` and `TOTP`. Example for `TOTP`:
179174
```java
180-
TOTP totp = new TOTP.Builder(secret).build();
175+
TOTPGenerator totpGenerator = new TOTPGenerator.Builder(secret).build();
181176

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
183178

184179
```
185180

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.github.bastiaanjansen</groupId>
1212
<artifactId>otp-java</artifactId>
13-
<version>1.3.2</version>
13+
<version>2.0.0</version>
1414

1515
<name>OTP-Java</name>
1616
<description>A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).</description>

src/main/java/com/bastiaanjansen/otp/HOTP.java

-128
This file was deleted.

0 commit comments

Comments
 (0)