Skip to content

Commit f1ea6bc

Browse files
committed
Clarified that keys should be SecretKey instances and not just Key instances.
1 parent 1f80e92 commit f1ea6bc

File tree

6 files changed

+25
-27
lines changed

6 files changed

+25
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGener
1313
To actually generate time-based one-time passwords, you'll need a secret key and a timestamp. Secure key management is beyond the scope of this document; for the purposes of an example, though, we'll generate a random key:
1414

1515
```java
16-
final Key secretKey;
16+
final SecretKey secretKey;
1717
{
1818
final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());
1919

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

2323
secretKey = keyGenerator.generateKey();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
package com.eatthepath.otp;
2222

23+
import javax.crypto.Mac;
24+
import javax.crypto.SecretKey;
2325
import java.nio.ByteBuffer;
2426
import java.security.InvalidKeyException;
25-
import java.security.Key;
2627
import java.security.NoSuchAlgorithmException;
2728

28-
import javax.crypto.Mac;
29-
3029
/**
3130
* <p>Generates HMAC-based one-time passwords (HOTP) as specified in
3231
* <a href="https://tools.ietf.org/html/rfc4226">RFC&nbsp;4226</a>.</p>
@@ -123,20 +122,20 @@ protected HmacOneTimePasswordGenerator(final int passwordLength, final String al
123122
/**
124123
* Generates a one-time password using the given key and counter value.
125124
*
126-
* @param key a secret key to be used to generate the password
125+
* @param secretKey a secret key to be used to generate the password
127126
* @param counter the counter value to be used to generate the password
128127
*
129128
* @return an integer representation of a one-time password; callers will need to format the password for display
130129
* on their own
131130
*
132131
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
133132
*/
134-
public int generateOneTimePassword(final Key key, final long counter) throws InvalidKeyException {
133+
public int generateOneTimePassword(final SecretKey secretKey, final long counter) throws InvalidKeyException {
135134
final Mac mac;
136135

137136
try {
138137
mac = Mac.getInstance(this.algorithm);
139-
mac.init(key);
138+
mac.init(secretKey);
140139
} catch (final NoSuchAlgorithmException e) {
141140
// This should never happen since we verify that the algorithm is legit in the constructor.
142141
throw new RuntimeException(e);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
package com.eatthepath.otp;
2222

2323
import javax.crypto.Mac;
24+
import javax.crypto.SecretKey;
2425
import java.security.InvalidKeyException;
25-
import java.security.Key;
2626
import java.security.NoSuchAlgorithmException;
2727
import java.util.Date;
2828
import java.util.concurrent.TimeUnit;
@@ -129,16 +129,16 @@ public TimeBasedOneTimePasswordGenerator(final long timeStep, final TimeUnit tim
129129
/**
130130
* Generates a one-time password using the given key and timestamp.
131131
*
132-
* @param key a secret key to be used to generate the password
132+
* @param secretKey a secret key to be used to generate the password
133133
* @param timestamp the timestamp for which to generate the password
134134
*
135135
* @return an integer representation of a one-time password; callers will need to format the password for display
136136
* on their own
137137
*
138138
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
139139
*/
140-
public int generateOneTimePassword(final Key key, final Date timestamp) throws InvalidKeyException {
141-
return this.generateOneTimePassword(key, timestamp.getTime() / this.timeStepMillis);
140+
public int generateOneTimePassword(final SecretKey secretKey, final Date timestamp) throws InvalidKeyException {
141+
return this.generateOneTimePassword(secretKey, timestamp.getTime() / this.timeStepMillis);
142142
}
143143

144144
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
package com.eatthepath.otp;
2222

2323
import javax.crypto.KeyGenerator;
24+
import javax.crypto.SecretKey;
2425
import java.security.InvalidKeyException;
25-
import java.security.Key;
2626
import java.security.NoSuchAlgorithmException;
2727
import java.util.Date;
2828
import java.util.concurrent.TimeUnit;
2929

3030
public class ExampleApp {
31+
3132
public static void main(final String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
3233
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
3334

34-
final Key secretKey;
35+
final SecretKey secretKey;
3536
{
3637
final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());
3738

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020

2121
package com.eatthepath.otp;
2222

23-
import static org.junit.Assert.*;
24-
25-
import java.nio.charset.StandardCharsets;
26-
import java.security.InvalidKeyException;
27-
import java.security.Key;
28-
import java.security.NoSuchAlgorithmException;
29-
30-
import javax.crypto.spec.SecretKeySpec;
31-
3223
import junitparams.JUnitParamsRunner;
3324
import junitparams.Parameters;
3425
import org.junit.Test;
3526
import org.junit.runner.RunWith;
3627

28+
import javax.crypto.SecretKey;
29+
import javax.crypto.spec.SecretKeySpec;
30+
import java.nio.charset.StandardCharsets;
31+
import java.security.NoSuchAlgorithmException;
32+
33+
import static org.junit.Assert.assertEquals;
34+
3735
@RunWith(JUnitParamsRunner.class)
3836
public class HmacOneTimePasswordGeneratorTest {
3937

@@ -83,7 +81,7 @@ public void testGetAlgorithm() throws NoSuchAlgorithmException {
8381
public void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
8482
final HmacOneTimePasswordGenerator hmacOneTimePasswordGenerator = this.getDefaultGenerator();
8583

86-
final Key key = new SecretKeySpec("12345678901234567890".getBytes(StandardCharsets.US_ASCII), "RAW");
84+
final SecretKey key = new SecretKeySpec("12345678901234567890".getBytes(StandardCharsets.US_ASCII), "RAW");
8785
assertEquals(expectedOneTimePassword, hmacOneTimePasswordGenerator.generateOneTimePassword(key, counter));
8886
}
8987

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.junit.Test;
2626
import org.junit.runner.RunWith;
2727

28+
import javax.crypto.SecretKey;
2829
import javax.crypto.spec.SecretKeySpec;
2930
import java.nio.charset.StandardCharsets;
30-
import java.security.Key;
3131
import java.security.NoSuchAlgorithmException;
3232
import java.util.Date;
3333
import java.util.concurrent.TimeUnit;
@@ -87,10 +87,10 @@ public void testGenerateOneTimePassword(final String algorithm, final long epoch
8787

8888
final Date date = new Date(TimeUnit.SECONDS.toMillis(epochSeconds));
8989

90-
assertEquals(expectedOneTimePassword, totp.generateOneTimePassword(getKeyForAlgorithm(algorithm), date));
90+
assertEquals(expectedOneTimePassword, totp.generateOneTimePassword(getSecretKeyForAlgorithm(algorithm), date));
9191
}
9292

93-
private static Key getKeyForAlgorithm(final String algorithm) {
93+
private static SecretKey getSecretKeyForAlgorithm(final String algorithm) {
9494
final String keyString;
9595

9696
switch (algorithm) {

0 commit comments

Comments
 (0)