Skip to content

Commit e8c10e4

Browse files
committed
Removed hard coded key size to key sizes that match HMAC algorithms
1 parent 7abe5c3 commit e8c10e4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

examples/provider/CryptoBenchmark.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ private static byte[] generateTestData(int size) {
7474
return new byte[size];
7575
}
7676

77+
private static int getHmacKeySize(String algorithm) {
78+
// Key sizes in bytes based on hash block sizes
79+
switch (algorithm) {
80+
case "HmacMD5":
81+
return 64;
82+
case "HmacSHA1":
83+
return 64;
84+
case "HmacSHA256":
85+
return 64;
86+
case "HmacSHA384":
87+
return 128;
88+
case "HmacSHA512":
89+
return 128;
90+
default:
91+
throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm);
92+
}
93+
}
94+
7795
private static void printProviderInfo(Provider provider) {
7896
System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion());
7997
}
@@ -421,9 +439,10 @@ private static void runHmacBenchmark(String algorithm, String providerName) thro
421439
/* Initialize Mac with specific provider */
422440
mac = Mac.getInstance(algorithm, providerName);
423441

424-
/* Initialize Mac with a random key */
442+
/* Initialize Mac with a random key of appropriate length */
425443
SecureRandom secureRandom = new SecureRandom();
426-
byte[] keyBytes = new byte[64];
444+
int keySize = getHmacKeySize(algorithm);
445+
byte[] keyBytes = new byte[keySize];
427446
secureRandom.nextBytes(keyBytes);
428447
SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);
429448
mac.init(key);

0 commit comments

Comments
 (0)