Skip to content

Commit ff8334f

Browse files
author
Jon Chambers
committed
Fixed a few typos and clarified the README.
1 parent 6a57acc commit ff8334f

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ java-otp is a library for generating one-time passwords using the [HOTP (RFC 422
44

55
## Usage
66

7-
To demonstrate generating one-time passwords, we'll focus on the TOTP algorithm. To create a TOTP generator with a default password length, time step, and HMAC algorithm:
7+
To demonstrate generating one-time passwords, we'll focus on the TOTP algorithm. To create a TOTP generator with a default password length (6 digits), time step (30 seconds), and HMAC algorithm (HMAC-SHA1):
88

99
```java
1010
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
@@ -17,7 +17,7 @@ final Key secretKey;
1717
{
1818
final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());
1919

20-
// SHA-1 and SHA-256 prefer 64-byte (512-bit) keys; SHA512 prefers 128-byte keys
20+
// HMAC-SHA1 and HMAC-SHA256 prefer 64-byte (512-bit) keys; HMAC-SHA512 prefers 128-byte (1024-bit) keys
2121
keyGenerator.init(512);
2222

2323
secretKey = keyGenerator.generateKey();
@@ -28,12 +28,19 @@ Armed with a secret key, we can deterministically generate one-time passwords fo
2828

2929
```java
3030
final Date now = new Date();
31-
final Date later = new Date(now.getTime() + TimeUnit.SECONDS.toMillis(30));
31+
final Date later = new Date(now.getTime() + totp.getTimeStep(TimeUnit.MILLISECONDS));
3232

3333
System.out.format("Current password: %06d\n", totp.generateOneTimePassword(secretKey, now));
3434
System.out.format("Future password: %06d\n", totp.generateOneTimePassword(secretKey, later));
3535
```
3636

37+
…which produces (for one randomly-generated key):
38+
39+
```
40+
Current password: 164092
41+
Future password: 046148
42+
```
43+
3744
## License and copyright
3845

3946
java-otp is published under the [MIT License](https://opensource.org/licenses/MIT).

src/main/java/com/eatthepath/otp/TimeBasedOneTimePasswordGenerator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
package com.eatthepath.otp;
2222

23+
import javax.crypto.Mac;
2324
import java.security.InvalidKeyException;
2425
import java.security.Key;
2526
import java.security.NoSuchAlgorithmException;
2627
import java.util.Date;
2728
import java.util.concurrent.TimeUnit;
2829

29-
import javax.crypto.Mac;
30-
3130
/**
3231
* <p>Generates time-based one-time passwords (TOTP) as specified in
3332
* <a href="https://tools.ietf.org/html/rfc6238">RFC&nbsp;6238</a>.</p>
@@ -99,7 +98,7 @@ public TimeBasedOneTimePasswordGenerator(final long timeStep, final TimeUnit tim
9998
* happen except in cases of serious misconfiguration
10099
*/
101100
public TimeBasedOneTimePasswordGenerator(final long timeStep, final TimeUnit timeStepUnit, final int passwordLength) throws NoSuchAlgorithmException {
102-
this(timeStep, timeStepUnit, passwordLength, HmacOneTimePasswordGenerator.HOTP_HMAC_ALGORITHM);
101+
this(timeStep, timeStepUnit, passwordLength, TOTP_ALGORITHM_HMAC_SHA1);
103102
}
104103

105104
/**

src/test/java/com/eatthepath/otp/ExampleApp.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
package com.eatthepath.otp;
2222

23+
import javax.crypto.KeyGenerator;
2324
import java.security.InvalidKeyException;
2425
import java.security.Key;
2526
import java.security.NoSuchAlgorithmException;
2627
import java.util.Date;
2728
import java.util.concurrent.TimeUnit;
2829

29-
import javax.crypto.KeyGenerator;
30-
3130
public class ExampleApp {
3231
public static void main(final String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
3332
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
@@ -36,14 +35,14 @@ public static void main(final String[] args) throws NoSuchAlgorithmException, In
3635
{
3736
final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());
3837

39-
// SHA-1 and SHA-256 prefer 64-byte (512-bit) keys; SHA512 prefers 128-byte keys
38+
// SHA-1 and SHA-256 prefer 64-byte (512-bit) keys; SHA512 prefers 128-byte (1024-bit) keys
4039
keyGenerator.init(512);
4140

4241
secretKey = keyGenerator.generateKey();
4342
}
4443

4544
final Date now = new Date();
46-
final Date later = new Date(now.getTime() + TimeUnit.SECONDS.toMillis(30));
45+
final Date later = new Date(now.getTime() + totp.getTimeStep(TimeUnit.MILLISECONDS));
4746

4847
System.out.format("Current password: %06d\n", totp.generateOneTimePassword(secretKey, now));
4948
System.out.format("Future password: %06d\n", totp.generateOneTimePassword(secretKey, later));

src/test/java/com/eatthepath/otp/TimeBasedOneTimePasswordGeneratorTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,19 @@
2020

2121
package com.eatthepath.otp;
2222

23-
import static org.junit.Assert.*;
23+
import junitparams.JUnitParamsRunner;
24+
import junitparams.Parameters;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
2427

28+
import javax.crypto.spec.SecretKeySpec;
2529
import java.nio.charset.StandardCharsets;
26-
import java.security.InvalidKeyException;
2730
import java.security.Key;
2831
import java.security.NoSuchAlgorithmException;
2932
import java.util.Date;
30-
import java.util.HashMap;
31-
import java.util.Map;
3233
import java.util.concurrent.TimeUnit;
3334

34-
import javax.crypto.spec.SecretKeySpec;
35-
36-
import junitparams.JUnitParamsRunner;
37-
import junitparams.Parameters;
38-
import org.junit.Test;
39-
import org.junit.runner.RunWith;
35+
import static org.junit.Assert.assertEquals;
4036

4137
@RunWith(JUnitParamsRunner.class)
4238
public class TimeBasedOneTimePasswordGeneratorTest extends HmacOneTimePasswordGeneratorTest {
@@ -61,7 +57,7 @@ public void testGetTimeStep() throws NoSuchAlgorithmException {
6157
* Tests time-based one-time password generation using the test vectors from
6258
* <a href="https://tools.ietf.org/html/rfc6238#appendix-B">RFC&nbsp;6238, Appendix B</a>. Note that the RFC
6359
* incorrectly states that the same key is used for all test vectors. The
64-
* <a href="https://www.rfc-editor.org/errata_search.php?rfc=6238&eid=2866">>errata</a> correctly points out that
60+
* <a href="https://www.rfc-editor.org/errata_search.php?rfc=6238&eid=2866">errata</a> correctly points out that
6561
* different keys are used for each of the various HMAC algorithms.
6662
*/
6763
@Test

0 commit comments

Comments
 (0)