public final class HmacUtils extends Object
Mac tasks. This class is immutable and thread-safe.
However the Mac may not be.
Note: Not all JCE implementations support all algorithms. If not supported, an IllegalArgumentException is thrown.
Sample usage:
import static HmacAlgorithms.*;
byte[] key = {1,2,3,4}; // don't use this actual key!
String valueToDigest = "The quick brown fox jumps over the lazy dog";
byte[] hmac = new HmacUtils(HMAC_SHA_224, key).hmac(valueToDigest);
// Mac re-use
HmacUtils hm1 = new HmacUtils("HmacAlgoName", key); // use a valid name here!
String hexPom = hm1.hmacHex(new File("pom.xml"));
String hexNot = hm1.hmacHex(new File("NOTICE.txt"));
| Constructor and Description |
|---|
HmacUtils(HmacAlgorithms algorithm,
byte[] key)
Creates an instance using the provided algorithm type.
|
HmacUtils(HmacAlgorithms algorithm,
String key)
Creates an instance using the provided algorithm type.
|
HmacUtils(String algorithm,
byte[] key)
Creates an instance using the provided algorithm type.
|
HmacUtils(String algorithm,
String key)
Creates an instance using the provided algorithm type.
|
| Modifier and Type | Method and Description |
|---|---|
static Mac |
getInitializedMac(HmacAlgorithms algorithm,
byte[] key)
Returns an initialized
Mac for the given algorithm. |
static Mac |
getInitializedMac(String algorithm,
byte[] key)
Returns an initialized
Mac for the given algorithm. |
byte[] |
hmac(byte[] valueToDigest)
Returns the digest for the input data.
|
byte[] |
hmac(ByteBuffer valueToDigest)
Returns the digest for the input data.
|
byte[] |
hmac(File valueToDigest)
Returns the digest for the file.
|
byte[] |
hmac(InputStream valueToDigest)
Returns the digest for the stream.
|
byte[] |
hmac(String valueToDigest)
Returns the digest for the input data.
|
String |
hmacHex(byte[] valueToDigest)
Returns the digest for the input data.
|
String |
hmacHex(ByteBuffer valueToDigest)
Returns the digest for the input data.
|
String |
hmacHex(File valueToDigest)
Returns the digest for the file.
|
String |
hmacHex(InputStream valueToDigest)
Returns the digest for the stream.
|
String |
hmacHex(String valueToDigest)
Returns the digest for the input data.
|
static boolean |
isAvailable(HmacAlgorithms name)
Returns whether this algorithm is available
|
static boolean |
isAvailable(String name)
Returns whether this algorithm is available
|
static Mac |
updateHmac(Mac mac,
byte[] valueToDigest)
Resets and then updates the given
Mac with the value. |
static Mac |
updateHmac(Mac mac,
InputStream valueToDigest)
Resets and then updates the given
Mac with the value. |
static Mac |
updateHmac(Mac mac,
String valueToDigest)
Resets and then updates the given
Mac with the value. |
public HmacUtils(HmacAlgorithms algorithm, byte[] key)
algorithm - to use.key - the key to useIllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.public HmacUtils(HmacAlgorithms algorithm, String key)
algorithm - to usekey - the key to useIllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.public HmacUtils(String algorithm, byte[] key)
algorithm - to usekey - the key to useIllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.public HmacUtils(String algorithm, String key)
algorithm - to usekey - the key to useIllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.public static Mac getInitializedMac(HmacAlgorithms algorithm, byte[] key)
Mac for the given algorithm.algorithm - the name of the algorithm requested. See
Appendix A in the Java Cryptography Architecture Reference Guide for information about standard
algorithm names.key - They key for the keyed digest (must not be null)IllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.Mac.getInstance(String),
Mac.init(Key)public static Mac getInitializedMac(String algorithm, byte[] key)
Mac for the given algorithm.algorithm - the name of the algorithm requested. See
Appendix A in the Java Cryptography Architecture Reference Guide for information about standard
algorithm names.key - They key for the keyed digest (must not be null)IllegalArgumentException - when a NoSuchAlgorithmException is caught or key is null or key is invalid.Mac.getInstance(String),
Mac.init(Key)public byte[] hmac(byte[] valueToDigest)
valueToDigest - the input to usepublic byte[] hmac(ByteBuffer valueToDigest)
valueToDigest - the input to usepublic byte[] hmac(File valueToDigest) throws IOException
valueToDigest - the file to useIOException - If an I/O error occurs.public byte[] hmac(InputStream valueToDigest) throws IOException
valueToDigest - the data to use
The InputStream must not be null and will not be closed
IOException - If an I/O error occurs.public byte[] hmac(String valueToDigest)
valueToDigest - the input to use, treated as UTF-8public String hmacHex(byte[] valueToDigest)
valueToDigest - the input to usepublic String hmacHex(ByteBuffer valueToDigest)
valueToDigest - the input to usepublic String hmacHex(File valueToDigest) throws IOException
valueToDigest - the file to useIOException - If an I/O error occurs.public String hmacHex(InputStream valueToDigest) throws IOException
valueToDigest - the data to use
The InputStream must not be null and will not be closed
IOException - If an I/O error occurs.public String hmacHex(String valueToDigest)
valueToDigest - the input to use, treated as UTF-8public static boolean isAvailable(HmacAlgorithms name)
name - the name to checkpublic static boolean isAvailable(String name)
name - the name to checkpublic static Mac updateHmac(Mac mac, byte[] valueToDigest)
Mac with the value.mac - the initialized Mac to updatevalueToDigest - the value to update the Mac with (maybe null or empty)MacIllegalStateException - if the Mac was not initializedpublic static Mac updateHmac(Mac mac, InputStream valueToDigest) throws IOException
Mac with the value.mac - the initialized Mac to updatevalueToDigest - the value to update the Mac with
The InputStream must not be null and will not be closed
MacIOException - If an I/O error occurs.IllegalStateException - If the Mac was not initializedpublic static Mac updateHmac(Mac mac, String valueToDigest)
Mac with the value.mac - the initialized Mac to updatevalueToDigest - the value to update the Mac with (maybe null or empty)MacIllegalStateException - if the Mac was not initialized