Skip to content

Commit a1e8c4e

Browse files
Create a new SHA-256 hasher on each call to makeHash
MessageDigest does not seem to be thread safe, and when making multiple calls in quick succession (like LiteFile does) will somehow result in the same API key getting different hashes, resulting in those calls failing.
1 parent f2c5235 commit a1e8c4e

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

  • proxyserver/src/main/java/edu/suffolk/litlab/efsp/utils

proxyserver/src/main/java/edu/suffolk/litlab/efsp/utils/Hasher.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77

88
public class Hasher {
99

10-
private static final MessageDigest digest;
11-
12-
static {
13-
try {
14-
digest = MessageDigest.getInstance("SHA-256");
15-
} catch (NoSuchAlgorithmException ex) {
16-
throw new AssertionError(ex);
17-
}
18-
}
19-
2010
/**
2111
* Returns a hash of the given input string. Used to hash user IDs and API keys (neither of which
2212
* need salt, as they're already random).
@@ -25,6 +15,12 @@ public static String makeHash(String input) {
2515
if (input == null) {
2616
return "";
2717
}
28-
return new String(Hex.encode(digest.digest(input.getBytes(StandardCharsets.UTF_8))));
18+
19+
try {
20+
var shaHasher = MessageDigest.getInstance("SHA-256");
21+
return new String(Hex.encode(shaHasher.digest(input.getBytes(StandardCharsets.UTF_8))));
22+
} catch (NoSuchAlgorithmException ex) {
23+
throw new AssertionError(ex);
24+
}
2925
}
3026
}

0 commit comments

Comments
 (0)