Package dev.vink.jtotp
Class HMACUtils
java.lang.Object
dev.vink.jtotp.HMACUtils
Utility class for generating HMAC (Hash-based Message Authentication Code)
and converting byte arrays to hexadecimal strings.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
calculateHMAC
(byte[] key, byte[] data) Calculates the HMAC of the given data using the default HMAC algorithm (HmacSHA1).static byte[]
calculateHMAC
(byte[] key, byte[] data, String algorithm) Calculates the HMAC of the given data using the specified key and algorithm.static String
toHex
(byte[] bytes) Converts a byte array into a hexadecimal string.
-
Field Details
-
SHA1_ALGORITHM
Algorithm input for TOTP Builder for HMAC-SHA1.- See Also:
-
SHA256_ALGORITHM
Algorithm input for TOTP Builder for HMAC-SHA256.- See Also:
-
SHA512_ALGORITHM
Algorithm input for TOTP Builder for HMAC-SHA512.- See Also:
-
-
Method Details
-
calculateHMAC
public static byte[] calculateHMAC(byte[] key, byte[] data, String algorithm) throws NoSuchAlgorithmException, InvalidKeyException Calculates the HMAC of the given data using the specified key and algorithm.- Parameters:
key
- The secret key used for HMAC generation as a byte array.data
- The data to be hashed as a byte array.algorithm
- The HMAC algorithm to use (e.g., HmacSHA1, HmacSHA256, HmacSHA512).- Returns:
- A byte array representing the HMAC of the input data.
- Throws:
NoSuchAlgorithmException
- If the HMAC algorithm is not available.InvalidKeyException
- If the provided key is invalid.
-
calculateHMAC
public static byte[] calculateHMAC(byte[] key, byte[] data) throws NoSuchAlgorithmException, InvalidKeyException Calculates the HMAC of the given data using the default HMAC algorithm (HmacSHA1).- Parameters:
key
- The secret key used for HMAC generation as a byte array.data
- The data to be hashed as a byte array.- Returns:
- A byte array representing the HMAC of the input data.
- Throws:
NoSuchAlgorithmException
- If the HMAC algorithm is not available.InvalidKeyException
- If the provided key is invalid.
-
toHex
Converts a byte array into a hexadecimal string.- Parameters:
bytes
- The byte array to convert.- Returns:
- A string representing the hexadecimal representation of the byte array.
-