public final class HmacUtils
extends java.lang.Object
Mac
tasks. This class is immutable and thread-safe.
Note: Not all JCE implementations supports all algorithms. If not supported, an IllegalArgumentException is thrown.
Modifier and Type | Field and Description |
---|---|
private static int |
STREAM_BUFFER_LENGTH |
Constructor and Description |
---|
HmacUtils() |
Modifier and Type | Method and Description |
---|---|
static javax.crypto.Mac |
getHmacMd5(byte[] key)
Returns an initialized
Mac for the HmacMD5 algorithm. |
static javax.crypto.Mac |
getHmacSha1(byte[] key)
Returns an initialized
Mac for the HmacSHA1 algorithm. |
static javax.crypto.Mac |
getHmacSha256(byte[] key)
Returns an initialized
Mac for the HmacSHA256 algorithm. |
static javax.crypto.Mac |
getHmacSha384(byte[] key)
Returns an initialized
Mac for the HmacSHA384 algorithm. |
static javax.crypto.Mac |
getHmacSha512(byte[] key)
Returns an initialized
Mac for the HmacSHA512 algorithm. |
static javax.crypto.Mac |
getInitializedMac(HmacAlgorithms algorithm,
byte[] key)
Returns an initialized
Mac for the given algorithm . |
static javax.crypto.Mac |
getInitializedMac(java.lang.String algorithm,
byte[] key)
Returns an initialized
Mac for the given algorithm . |
static byte[] |
hmacMd5(byte[] key,
byte[] valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacMd5(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacMd5(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) for the given key and value.
|
static java.lang.String |
hmacMd5Hex(byte[] key,
byte[] valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) as a hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacMd5Hex(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) as a hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacMd5Hex(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacMD5 Message Authentication Code (MAC) as a hex string (lowercase) for the given key and value.
|
static byte[] |
hmacSha1(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha1(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha1(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) for the given key and value.
|
static java.lang.String |
hmacSha1Hex(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha1Hex(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha1Hex(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA1 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static byte[] |
hmacSha256(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha256(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha256(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value.
|
static java.lang.String |
hmacSha256Hex(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha256Hex(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha256Hex(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static byte[] |
hmacSha384(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha384(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha384(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) for the given key and value.
|
static java.lang.String |
hmacSha384Hex(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha384Hex(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha384Hex(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA384 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static byte[] |
hmacSha512(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha512(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.
|
static byte[] |
hmacSha512(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.
|
static java.lang.String |
hmacSha512Hex(byte[] key,
byte[] valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha512Hex(byte[] key,
java.io.InputStream valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static java.lang.String |
hmacSha512Hex(java.lang.String key,
java.lang.String valueToDigest)
Returns a HmacSHA512 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
|
static javax.crypto.Mac |
updateHmac(javax.crypto.Mac mac,
byte[] valueToDigest)
Updates the given
Mac . |
static javax.crypto.Mac |
updateHmac(javax.crypto.Mac mac,
java.io.InputStream valueToDigest)
Updates the given
Mac . |
static javax.crypto.Mac |
updateHmac(javax.crypto.Mac mac,
java.lang.String valueToDigest)
Updates the given
Mac . |
private static final int STREAM_BUFFER_LENGTH
public static javax.crypto.Mac getHmacMd5(byte[] key)
Mac
for the HmacMD5 algorithm.
Every implementation of the Java platform is required to support this standard Mac algorithm.
key
- They key for the keyed digest (must not be null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.Mac getHmacSha1(byte[] key)
Mac
for the HmacSHA1 algorithm.
Every implementation of the Java platform is required to support this standard Mac algorithm.
key
- They key for the keyed digest (must not be null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.Mac getHmacSha256(byte[] key)
Mac
for the HmacSHA256 algorithm.
Every implementation of the Java platform is required to support this standard Mac algorithm.
key
- They key for the keyed digest (must not be null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.Mac getHmacSha384(byte[] key)
Mac
for the HmacSHA384 algorithm.
Every implementation of the Java platform is not required to support this Mac algorithm.
key
- They key for the keyed digest (must not be null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.Mac getHmacSha512(byte[] key)
Mac
for the HmacSHA512 algorithm.
Every implementation of the Java platform is not required to support this Mac algorithm.
key
- They key for the keyed digest (must not be null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.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)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static javax.crypto.Mac getInitializedMac(java.lang.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)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.Mac.getInstance(String)
,
Mac.init(Key)
public static byte[] hmacMd5(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacMd5(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacMd5(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacMd5Hex(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacMd5Hex(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacMd5Hex(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha1(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha1(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha1(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha1Hex(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha1Hex(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha1Hex(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha256(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha256(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.
s * @throws IllegalArgumentException
when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha256(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha256Hex(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha256Hex(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha256Hex(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha384(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha384(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha384(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha384Hex(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha384Hex(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha384Hex(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha512(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha512(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static byte[] hmacSha512(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha512Hex(byte[] key, byte[] valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha512Hex(byte[] key, java.io.InputStream valueToDigest) throws java.io.IOException
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest
The InputStream must not be null and will not be closed
java.io.IOException
- If an I/O error occurs.java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static java.lang.String hmacSha512Hex(java.lang.String key, java.lang.String valueToDigest)
key
- They key for the keyed digest (must not be null)valueToDigest
- The value (data) which should to digest (maybe empty or null)java.lang.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught or key is null or key is invalid.public static javax.crypto.Mac updateHmac(javax.crypto.Mac mac, byte[] valueToDigest)
Mac
. This generates a digest for valueToDigest and the key the Mac was initializedmac
- the initialized Mac
to updatevalueToDigest
- the value to update the Mac
with (maybe null or empty)Mac
java.lang.IllegalStateException
- if the Mac was not initializedpublic static javax.crypto.Mac updateHmac(javax.crypto.Mac mac, java.io.InputStream valueToDigest) throws java.io.IOException
Mac
. This generates a digest for valueToDigest and the key the Mac was initializedmac
- the initialized Mac
to updatevalueToDigest
- the value to update the Mac
with
The InputStream must not be null and will not be closed
Mac
java.io.IOException
- If an I/O error occurs.java.lang.IllegalStateException
- If the Mac was not initializedpublic static javax.crypto.Mac updateHmac(javax.crypto.Mac mac, java.lang.String valueToDigest)
Mac
. This generates a digest for valueToDigest and the key the Mac was initializedmac
- the initialized Mac
to updatevalueToDigest
- the value to update the Mac
with (maybe null or empty)Mac
java.lang.IllegalStateException
- if the Mac was not initialized