Class Cryptography
java.lang.Object
com.easypost.utils.Cryptography
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Enums for the supported HMAC algorithms. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
createHMAC
(byte @NotNull [] data, @NotNull String key, @NotNull Cryptography.HmacAlgorithm algorithm) Calculate the HMAC hex digest of a string.static String
hexEncodeToString
(byte @NotNull [] bytes) Hex-encode a byte array to a string.static boolean
signaturesMatch
(byte @NotNull [] signature1, byte @NotNull [] signature2) Check whether two signatures match.static boolean
signaturesMatch
(@NotNull String signature1, @NotNull String signature2) Check whether two signatures match.static String
toHMACSHA256HexDigest
(byte @NotNull [] data, @NotNull String key, @Nullable Normalizer.Form normalizationForm) Calculate the HMAC-SHA256 hex digest of a string.
-
Constructor Details
-
Cryptography
public Cryptography()
-
-
Method Details
-
hexEncodeToString
Hex-encode a byte array to a string.- Parameters:
bytes
- the byte array to hex-encode.- Returns:
- the hex-encoded byte array string.
-
toHMACSHA256HexDigest
public static String toHMACSHA256HexDigest(byte @NotNull [] data, @NotNull @NotNull String key, @Nullable Normalizer.Form normalizationForm) Calculate the HMAC-SHA256 hex digest of a string.- Parameters:
data
- Data to calculate hex digest for.key
- Key to use in HMAC calculation.normalizationForm
-Normalizer.Form
to use when normalizing key. No normalization when null.- Returns:
- Hex digest of data.
-
createHMAC
public static byte[] createHMAC(byte @NotNull [] data, @NotNull @NotNull String key, @NotNull @NotNull Cryptography.HmacAlgorithm algorithm) Calculate the HMAC hex digest of a string.- Parameters:
data
- Data to calculate hex digest for.key
- Key to use in HMAC calculation.algorithm
-Cryptography.HmacAlgorithm
to use to calculate HMAC.- Returns:
- Hex digest of data.
-
signaturesMatch
public static boolean signaturesMatch(byte @NotNull [] signature1, byte @NotNull [] signature2) Check whether two signatures match. This is safe against timing attacks.- Parameters:
signature1
- First signature to check.signature2
- Second signature to check.- Returns:
- True if signatures match, false otherwise.
-
signaturesMatch
public static boolean signaturesMatch(@NotNull @NotNull String signature1, @NotNull @NotNull String signature2) Check whether two signatures match. This is safe against timing attacks.- Parameters:
signature1
- First signature to check.signature2
- Second signature to check.- Returns:
- True if signatures match, false otherwise.
-