Cache the Mac so we do not need to constantly recreate it
Cache the Mac so we do not need to constantly recreate it
@@ -27,6 +27,7 @@ import java.util.Arrays;
*/
public class HMACConfidentialKey extends ConfidentialKey {
private volatile SecretKey key;
+ private Mac mac;
private final int length;
/**
@@ -61,12 +62,14 @@ public class HMACConfidentialKey extends ConfidentialKey {
this(owner,shortName,Integer.MAX_VALUE);
}
-
/**
* Computes the message authentication code for the specified byte sequence.
*/
- public byte[] mac(byte[] message) {
- return chop(createMac().doFinal(message));
+ public synchronized byte[] mac(byte[] message) {
+ if (mac == null) {
+ mac = createMac();
+ }
+ return chop(mac.doFinal(message));
}
/**
