Skip to content

Commit a9c17e6

Browse files
committed
Revert "Clarified that keys should be SecretKey instances and not just Key instances."
This reverts commit f1ea6bc.
1 parent f1ea6bc commit a9c17e6

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
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 SecretKey secretKey;
16+
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 (1024-bit) 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();

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

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

2121
package com.eatthepath.otp;
2222

23-
import javax.crypto.Mac;
24-
import javax.crypto.SecretKey;
2523
import java.nio.ByteBuffer;
2624
import java.security.InvalidKeyException;
25+
import java.security.Key;
2726
import java.security.NoSuchAlgorithmException;
2827

28+
import javax.crypto.Mac;
29+
2930
/**
3031
* <p>Generates HMAC-based one-time passwords (HOTP) as specified in
3132
* <a href="https://tools.ietf.org/html/rfc4226">RFC&nbsp;4226</a>.</p>
@@ -122,20 +123,20 @@ protected HmacOneTimePasswordGenerator(final int passwordLength, final String al
122123
/**
123124
* Generates a one-time password using the given key and counter value.
124125
*
125-
* @param secretKey a secret key to be used to generate the password
126+
* @param key a secret key to be used to generate the password
126127
* @param counter the counter value to be used to generate the password
127128
*
128129
* @return an integer representation of a one-time password; callers will need to format the password for display
129130
* on their own
130131
*
131132
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
132133
*/
133-
public int generateOneTimePassword(final SecretKey secretKey, final long counter) throws InvalidKeyException {
134+
public int generateOneTimePassword(final Key key, final long counter) throws InvalidKeyException {
134135
final Mac mac;
135136

136137
try {
137138
mac = Mac.getInstance(this.algorithm);
138-
mac.init(secretKey);
139+
mac.init(key);
139140
} catch (final NoSuchAlgorithmException e) {
140141
// This should never happen since we verify that the algorithm is legit in the constructor.
141142
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;
2524
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 secretKey a secret key to be used to generate the password
132+
* @param key 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 SecretKey secretKey, final Date timestamp) throws InvalidKeyException {
141-
return this.generateOneTimePassword(secretKey, timestamp.getTime() / this.timeStepMillis);
140+
public int generateOneTimePassword(final Key key, final Date timestamp) throws InvalidKeyException {
141+
return this.generateOneTimePassword(key, timestamp.getTime() / this.timeStepMillis);
142142
}
143143

144144
/**

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

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

2323
import javax.crypto.KeyGenerator;
24-
import javax.crypto.SecretKey;
2524
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-
3231
public static void main(final String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
3332
final TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
3433

35-
final SecretKey secretKey;
34+
final Key secretKey;
3635
{
3736
final KeyGenerator keyGenerator = KeyGenerator.getInstance(totp.getAlgorithm());
3837

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

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

2121
package com.eatthepath.otp;
2222

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

28-
import javax.crypto.SecretKey;
29-
import javax.crypto.spec.SecretKeySpec;
3025
import java.nio.charset.StandardCharsets;
26+
import java.security.InvalidKeyException;
27+
import java.security.Key;
3128
import java.security.NoSuchAlgorithmException;
3229

33-
import static org.junit.Assert.assertEquals;
30+
import javax.crypto.spec.SecretKeySpec;
31+
32+
import junitparams.JUnitParamsRunner;
33+
import junitparams.Parameters;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
3436

3537
@RunWith(JUnitParamsRunner.class)
3638
public class HmacOneTimePasswordGeneratorTest {
@@ -81,7 +83,7 @@ public void testGetAlgorithm() throws NoSuchAlgorithmException {
8183
public void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
8284
final HmacOneTimePasswordGenerator hmacOneTimePasswordGenerator = this.getDefaultGenerator();
8385

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

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;
2928
import javax.crypto.spec.SecretKeySpec;
3029
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(getSecretKeyForAlgorithm(algorithm), date));
90+
assertEquals(expectedOneTimePassword, totp.generateOneTimePassword(getKeyForAlgorithm(algorithm), date));
9191
}
9292

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

9696
switch (algorithm) {

0 commit comments

Comments
 (0)