|
34 | 34 |
|
35 | 35 | import org.apache.logging.log4j.LogManager; |
36 | 36 | import org.apache.logging.log4j.Logger; |
| 37 | + |
37 | 38 | import ro.kuberam.libs.java.crypto.CryptoError; |
38 | 39 | import ro.kuberam.libs.java.crypto.CryptoException; |
39 | 40 | import ro.kuberam.libs.java.crypto.ExpathCryptoModule; |
40 | 41 | import ro.kuberam.libs.java.crypto.utils.Buffer; |
41 | 42 |
|
42 | 43 | public class Hmac { |
43 | 44 |
|
44 | | - private static final Logger LOG = LogManager.getLogger(Hmac.class); |
45 | | - |
46 | | - public static String hmac(final byte[] data, final byte[] secretKey, final String algorithm, @Nullable final String format) throws CryptoException { |
47 | | - |
48 | | - // TODO: validate the format |
49 | | - final String actualFormat = Optional.ofNullable(format) |
50 | | - .filter(str -> !str.isEmpty()) |
51 | | - .orElse("base64"); // default to Base64 |
52 | | - |
53 | | - if (LOG.isDebugEnabled()) { |
54 | | - LOG.debug("secretKey = " + secretKey); |
55 | | - } |
56 | | - |
57 | | - final byte[] resultBytes = hmac(data, secretKey, algorithm); |
| 45 | + private static final Logger LOG = LogManager.getLogger(Hmac.class); |
58 | 46 |
|
59 | | - final String result; |
60 | | - if (actualFormat.equals("base64")) { |
61 | | - result = Base64.getEncoder().encodeToString(resultBytes); |
62 | | - } else { |
63 | | - result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase(); |
64 | | - } |
| 47 | + public static String hmac(final byte[] data, final byte[] secretKey, final String algorithm, |
| 48 | + @Nullable final String format) throws CryptoException { |
65 | 49 |
|
66 | | - if (LOG.isDebugEnabled()) { |
67 | | - LOG.debug("result = " + result); |
68 | | - } |
| 50 | + // TODO: validate the format |
| 51 | + final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64"); |
| 52 | + LOG.debug("secretKey = {}", () -> secretKey); |
69 | 53 |
|
70 | | - return result; |
71 | | - } |
| 54 | + final byte[] resultBytes = hmac(data, secretKey, algorithm); |
72 | 55 |
|
73 | | - public static String hmac(final InputStream data, final byte[] secretKey, final String algorithm, @Nullable final String format) throws CryptoException, IOException { |
| 56 | + final String result; |
| 57 | + if (actualFormat.equals("base64")) { |
| 58 | + result = Base64.getEncoder().encodeToString(resultBytes); |
| 59 | + } else { |
| 60 | + result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase(); |
| 61 | + } |
| 62 | + LOG.debug("result = {}", () -> result); |
74 | 63 |
|
75 | | - // TODO: validate the format |
76 | | - final String actualFormat = Optional.ofNullable(format) |
77 | | - .filter(str -> !str.isEmpty()) |
78 | | - .orElse("base64"); // default to Base64 |
| 64 | + return result; |
| 65 | + } |
79 | 66 |
|
80 | | - if (LOG.isDebugEnabled()) { |
81 | | - LOG.debug("secretKey = " + secretKey); |
82 | | - } |
| 67 | + public static String hmac(final InputStream data, final byte[] secretKey, final String algorithm, |
| 68 | + @Nullable final String format) throws CryptoException, IOException { |
83 | 69 |
|
84 | | - final byte[] resultBytes = hmac(data, secretKey, algorithm); |
| 70 | + // TODO: validate the format |
| 71 | + final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64"); |
| 72 | + LOG.debug("secretKey = {}", () -> secretKey); |
85 | 73 |
|
86 | | - final String result; |
87 | | - if (actualFormat.equals("base64")) { |
88 | | - result = Base64.getEncoder().encodeToString(resultBytes); |
89 | | - } else { |
90 | | - result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase(); |
91 | | - } |
| 74 | + final byte[] resultBytes = hmac(data, secretKey, algorithm); |
92 | 75 |
|
93 | | - if (LOG.isDebugEnabled()) { |
94 | | - LOG.debug("result = " + result); |
95 | | - } |
| 76 | + final String result; |
| 77 | + if (actualFormat.equals("base64")) { |
| 78 | + result = Base64.getEncoder().encodeToString(resultBytes); |
| 79 | + } else { |
| 80 | + result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase(); |
| 81 | + } |
| 82 | + LOG.debug("result = {}", () -> result); |
96 | 83 |
|
97 | | - return result; |
98 | | - } |
| 84 | + return result; |
| 85 | + } |
99 | 86 |
|
100 | | - public static byte[] hmac(final byte[] data, final byte[] secretKey, String algorithm) throws CryptoException { |
101 | | - final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames; |
| 87 | + public static byte[] hmac(final byte[] data, final byte[] secretKey, String algorithm) throws CryptoException { |
| 88 | + final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames; |
102 | 89 |
|
103 | | - if (javaStandardAlgorithmNames.containsKey(algorithm)) { |
104 | | - algorithm = javaStandardAlgorithmNames.get(algorithm); |
105 | | - } |
| 90 | + if (javaStandardAlgorithmNames.containsKey(algorithm)) { |
| 91 | + algorithm = javaStandardAlgorithmNames.get(algorithm); |
| 92 | + } |
106 | 93 |
|
107 | | - final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm); |
| 94 | + final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm); |
108 | 95 |
|
109 | | - try { |
110 | | - final Mac mac = Mac.getInstance(algorithm); |
111 | | - mac.init(signingKey); |
112 | | - return mac.doFinal(data); |
| 96 | + try { |
| 97 | + final Mac mac = Mac.getInstance(algorithm); |
| 98 | + mac.init(signingKey); |
| 99 | + return mac.doFinal(data); |
113 | 100 |
|
114 | | - } catch (final NoSuchAlgorithmException e) { |
115 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
116 | | - } catch (final InvalidKeyException e) { |
117 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
118 | | - } |
119 | | - } |
| 101 | + } catch (final NoSuchAlgorithmException e) { |
| 102 | + throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
| 103 | + } catch (final InvalidKeyException e) { |
| 104 | + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
| 105 | + } |
| 106 | + } |
120 | 107 |
|
121 | | - public static byte[] hmac(final InputStream data, final byte[] secretKey, String algorithm) throws CryptoException, IOException { |
122 | | - final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames; |
| 108 | + public static byte[] hmac(final InputStream data, final byte[] secretKey, String algorithm) |
| 109 | + throws CryptoException, IOException { |
| 110 | + final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames; |
123 | 111 |
|
124 | | - if (javaStandardAlgorithmNames.containsKey(algorithm)) { |
125 | | - algorithm = javaStandardAlgorithmNames.get(algorithm); |
126 | | - } |
| 112 | + if (javaStandardAlgorithmNames.containsKey(algorithm)) { |
| 113 | + algorithm = javaStandardAlgorithmNames.get(algorithm); |
| 114 | + } |
127 | 115 |
|
128 | | - final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm); |
| 116 | + final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm); |
129 | 117 |
|
130 | | - try { |
131 | | - final Mac mac = Mac.getInstance(algorithm); |
132 | | - mac.init(signingKey); |
| 118 | + try { |
| 119 | + final Mac mac = Mac.getInstance(algorithm); |
| 120 | + mac.init(signingKey); |
133 | 121 |
|
134 | | - final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; |
135 | | - int read = -1; |
136 | | - while((read = data.read(buf)) > -1) { |
137 | | - mac.update(buf, 0, read); |
138 | | - } |
| 122 | + final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; |
| 123 | + int read = -1; |
| 124 | + while ((read = data.read(buf)) > -1) { |
| 125 | + mac.update(buf, 0, read); |
| 126 | + } |
139 | 127 |
|
140 | | - return mac.doFinal(); |
| 128 | + return mac.doFinal(); |
141 | 129 |
|
142 | | - } catch (final NoSuchAlgorithmException e) { |
143 | | - throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
144 | | - } catch (final InvalidKeyException e) { |
145 | | - throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
146 | | - } |
147 | | - } |
| 130 | + } catch (final NoSuchAlgorithmException e) { |
| 131 | + throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e); |
| 132 | + } catch (final InvalidKeyException e) { |
| 133 | + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); |
| 134 | + } |
| 135 | + } |
148 | 136 | } |
0 commit comments