|
| 1 | +package com.easypost.utils; |
| 2 | + |
| 3 | +import org.apache.commons.codec.binary.Hex; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | +import org.jetbrains.annotations.Nullable; |
| 6 | + |
| 7 | +import javax.crypto.Mac; |
| 8 | +import javax.crypto.spec.SecretKeySpec; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.security.InvalidKeyException; |
| 11 | +import java.security.MessageDigest; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | +import java.text.Normalizer; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class for various cryptography utilities. |
| 17 | + */ |
| 18 | +public abstract class Cryptography { |
| 19 | + /** |
| 20 | + * Enums for the supported HMAC algorithms. |
| 21 | + */ |
| 22 | + public enum HmacAlgorithm { |
| 23 | + MD5("HmacMD5"), |
| 24 | + SHA1("HmacSHA1"), |
| 25 | + SHA256("HmacSHA256"), |
| 26 | + SHA512("HmacSHA512"); |
| 27 | + |
| 28 | + private final String algorithmString; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor. |
| 32 | + * |
| 33 | + * @param algorithmString the algorithm string |
| 34 | + */ |
| 35 | + HmacAlgorithm(String algorithmString) { |
| 36 | + this.algorithmString = algorithmString; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the algorithm string. |
| 41 | + * |
| 42 | + * @return the algorithm string. |
| 43 | + */ |
| 44 | + String getAlgorithmString() { |
| 45 | + return algorithmString; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Hex-encode a byte array to a string. |
| 51 | + * |
| 52 | + * @param bytes the byte array to hex-encode. |
| 53 | + * @return the hex-encoded byte array string. |
| 54 | + */ |
| 55 | + public static String hexEncodeToString(byte @NotNull [] bytes) { |
| 56 | + return new String(Hex.encodeHex(bytes)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Hex-encode a byte array to a char array. |
| 61 | + * |
| 62 | + * @param bytes the byte array to hex-encode. |
| 63 | + * @return the hex-encoded byte array char array. |
| 64 | + */ |
| 65 | + public static char[] hexEncode(byte @NotNull [] bytes) { |
| 66 | + return Hex.encodeHex(bytes); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Calculate the HMAC-SHA256 hex digest of a string. |
| 71 | + * |
| 72 | + * @param data Data to calculate hex digest for. |
| 73 | + * @param key Key to use in HMAC calculation. |
| 74 | + * @param normalizationForm {@link Normalizer.Form} to use when normalizing key. No normalization when null. |
| 75 | + * @return Hex digest of data. |
| 76 | + */ |
| 77 | + public static String toHMACSHA256HexDigest(byte @NotNull [] data, @NotNull String key, |
| 78 | + @Nullable Normalizer.Form normalizationForm) { |
| 79 | + if (normalizationForm != null) { |
| 80 | + key = Normalizer.normalize(key, normalizationForm); |
| 81 | + } |
| 82 | + |
| 83 | + byte[] hmacBytes = createHMAC(data, key, HmacAlgorithm.SHA256); |
| 84 | + return hexEncodeToString(hmacBytes); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Calculate the HMAC-SHA256 hex digest of a string. |
| 89 | + * |
| 90 | + * @param data Data to calculate hex digest for. |
| 91 | + * @param key Key to use in HMAC calculation. |
| 92 | + * @param normalizationForm {@link Normalizer.Form} to use when normalizing key. No normalization when null. |
| 93 | + * @return Hex digest of data. |
| 94 | + */ |
| 95 | + public static String toHMACSHA256HexDigest(@NotNull String data, @NotNull String key, |
| 96 | + @Nullable Normalizer.Form normalizationForm) { |
| 97 | + byte[] dataBytes = data.getBytes(); |
| 98 | + return toHMACSHA256HexDigest(dataBytes, key, normalizationForm); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Calculate the HMAC hex digest of a string. |
| 103 | + * |
| 104 | + * @param data Data to calculate hex digest for. |
| 105 | + * @param key Key to use in HMAC calculation. |
| 106 | + * @param algorithm {@link HmacAlgorithm} to use to calculate HMAC. |
| 107 | + * @return Hex digest of data. |
| 108 | + */ |
| 109 | + public static byte[] createHMAC(byte @NotNull [] data, @NotNull String key, @NotNull HmacAlgorithm algorithm) { |
| 110 | + // create HMAC-SHA256 generator and compute hash of data |
| 111 | + byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); |
| 112 | + SecretKeySpec keyHash = new SecretKeySpec(keyBytes, algorithm.algorithmString); |
| 113 | + |
| 114 | + try { |
| 115 | + Mac hmac = Mac.getInstance(algorithm.algorithmString); |
| 116 | + hmac.init(keyHash); |
| 117 | + return hmac.doFinal(data); |
| 118 | + } catch (InvalidKeyException | NoSuchAlgorithmException e) { |
| 119 | + throw new IllegalStateException("Cannot initialize Mac Generator", e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Check whether two signatures match. This is safe against timing attacks. |
| 125 | + * |
| 126 | + * @param signature1 First signature to check. |
| 127 | + * @param signature2 Second signature to check. |
| 128 | + * @return True if signatures match, false otherwise. |
| 129 | + */ |
| 130 | + public static boolean signaturesMatch(byte @NotNull [] signature1, byte @NotNull [] signature2) { |
| 131 | + // after Java SE 6 Update 17, MessageDigest.isEqual() is safe against timing attacks. |
| 132 | + // see: https://codahale.com//a-lesson-in-timing-attacks/ |
| 133 | + return MessageDigest.isEqual(signature1, signature2); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check whether two signatures match. This is safe against timing attacks. |
| 138 | + * |
| 139 | + * @param signature1 First signature to check. |
| 140 | + * @param signature2 Second signature to check. |
| 141 | + * @return True if signatures match, false otherwise. |
| 142 | + */ |
| 143 | + public static boolean signaturesMatch(@NotNull String signature1, @NotNull String signature2) { |
| 144 | + byte[] signature1Bytes = signature1.getBytes(StandardCharsets.UTF_8); |
| 145 | + byte[] signature2Bytes = signature2.getBytes(StandardCharsets.UTF_8); |
| 146 | + return signaturesMatch(signature1Bytes, signature2Bytes); |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments