Skip to content

Commit 7341c85

Browse files
committed
Revert "Make MACs thread-local to avoid repeated reinstantiation/garbage collection."
This reverts commit 5e625e7.
1 parent f8a50be commit 7341c85

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public class HmacOneTimePasswordGenerator {
4242

4343
private final int modDivisor;
4444

45-
private final ThreadLocal<Mac> macThreadLocal;
46-
4745
/**
4846
* The default length, in decimal digits, for one-time passwords.
4947
*/
@@ -120,16 +118,6 @@ protected HmacOneTimePasswordGenerator(final int passwordLength, final String al
120118
// Our purpose here is just to throw an exception immediately if the algorithm is bogus.
121119
Mac.getInstance(algorithm);
122120
this.algorithm = algorithm;
123-
124-
this.macThreadLocal = ThreadLocal.withInitial(() -> {
125-
try {
126-
return Mac.getInstance(algorithm);
127-
} catch (final NoSuchAlgorithmException e) {
128-
// This should never happen; we just checked to make sure we could instantiate a MAC with this
129-
// algorithm, and if we made it this far, we know it worked.
130-
throw new RuntimeException(e);
131-
}
132-
});
133121
}
134122

135123
/**
@@ -144,8 +132,15 @@ protected HmacOneTimePasswordGenerator(final int passwordLength, final String al
144132
* @throws InvalidKeyException if the given key is inappropriate for initializing the {@link Mac} for this generator
145133
*/
146134
public int generateOneTimePassword(final Key key, final long counter) throws InvalidKeyException {
147-
final Mac mac = this.macThreadLocal.get();
148-
mac.init(key);
135+
final Mac mac;
136+
137+
try {
138+
mac = Mac.getInstance(this.algorithm);
139+
mac.init(key);
140+
} catch (final NoSuchAlgorithmException e) {
141+
// This should never happen since we verify that the algorithm is legit in the constructor.
142+
throw new RuntimeException(e);
143+
}
149144

150145
final ByteBuffer buffer = ByteBuffer.allocate(8);
151146
buffer.putLong(0, counter);

0 commit comments

Comments
 (0)