Skip to content

Commit

Permalink
Removed hard coded key size to key sizes that match HMAC algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Tjaden committed Feb 7, 2025
1 parent 7abe5c3 commit e8c10e4
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions examples/provider/CryptoBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ private static byte[] generateTestData(int size) {
return new byte[size];
}

private static int getHmacKeySize(String algorithm) {
// Key sizes in bytes based on hash block sizes
switch (algorithm) {
case "HmacMD5":
return 64;
case "HmacSHA1":
return 64;
case "HmacSHA256":
return 64;
case "HmacSHA384":
return 128;
case "HmacSHA512":
return 128;
default:
throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm);
}
}

private static void printProviderInfo(Provider provider) {
System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion());
}
Expand Down Expand Up @@ -421,9 +439,10 @@ private static void runHmacBenchmark(String algorithm, String providerName) thro
/* Initialize Mac with specific provider */
mac = Mac.getInstance(algorithm, providerName);

/* Initialize Mac with a random key */
/* Initialize Mac with a random key of appropriate length */
SecureRandom secureRandom = new SecureRandom();
byte[] keyBytes = new byte[64];
int keySize = getHmacKeySize(algorithm);
byte[] keyBytes = new byte[keySize];
secureRandom.nextBytes(keyBytes);
SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);
mac.init(key);
Expand Down

0 comments on commit e8c10e4

Please sign in to comment.