35
35
*
36
36
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
37
37
*/
38
- public class TimeBasedOneTimePasswordGenerator extends HmacOneTimePasswordGenerator {
38
+ public class TimeBasedOneTimePasswordGenerator {
39
+ private final HmacOneTimePasswordGenerator hotp ;
39
40
private final Duration timeStep ;
40
41
41
42
/**
@@ -112,10 +113,8 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
112
113
* @see #TOTP_ALGORITHM_HMAC_SHA256
113
114
* @see #TOTP_ALGORITHM_HMAC_SHA512
114
115
*/
115
- public TimeBasedOneTimePasswordGenerator (final Duration timeStep , final int passwordLength , final String algorithm )
116
- throws NoSuchAlgorithmRuntimeException {
117
-
118
- super (passwordLength , algorithm );
116
+ public TimeBasedOneTimePasswordGenerator (final Duration timeStep , final int passwordLength , final String algorithm ) {
117
+ this .hotp = new HmacOneTimePasswordGenerator (passwordLength , algorithm );
119
118
this .timeStep = timeStep ;
120
119
}
121
120
@@ -131,7 +130,7 @@ public TimeBasedOneTimePasswordGenerator(final Duration timeStep, final int pass
131
130
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
132
131
*/
133
132
public int generateOneTimePassword (final Key key , final Instant timestamp ) throws InvalidKeyException {
134
- return this .generateOneTimePassword (key , timestamp .toEpochMilli () / this .timeStep .toMillis ());
133
+ return this .hotp . generateOneTimePassword (key , timestamp .toEpochMilli () / this .timeStep .toMillis ());
135
134
}
136
135
137
136
/**
@@ -163,7 +162,7 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
163
162
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
164
163
*/
165
164
public String generateOneTimePasswordString (final Key key , final Instant timestamp , final Locale locale ) throws InvalidKeyException {
166
- return this .formatOneTimePassword (this .generateOneTimePassword (key , timestamp ), locale );
165
+ return this .hotp . formatOneTimePassword (this .generateOneTimePassword (key , timestamp ), locale );
167
166
}
168
167
169
168
/**
@@ -174,4 +173,22 @@ public String generateOneTimePasswordString(final Key key, final Instant timesta
174
173
public Duration getTimeStep () {
175
174
return this .timeStep ;
176
175
}
176
+
177
+ /**
178
+ * Returns the length, in decimal digits, of passwords produced by this generator.
179
+ *
180
+ * @return the length, in decimal digits, of passwords produced by this generator
181
+ */
182
+ public int getPasswordLength () {
183
+ return this .hotp .getPasswordLength ();
184
+ }
185
+
186
+ /**
187
+ * Returns the name of the HMAC algorithm used by this generator.
188
+ *
189
+ * @return the name of the HMAC algorithm used by this generator
190
+ */
191
+ public String getAlgorithm () {
192
+ return this .hotp .getAlgorithm ();
193
+ }
177
194
}
0 commit comments