|
1 | 1 | package at.favre.lib.bytes; |
2 | 2 |
|
| 3 | +import javax.crypto.Mac; |
| 4 | +import javax.crypto.spec.SecretKeySpec; |
3 | 5 | import java.io.ByteArrayInputStream; |
4 | 6 | import java.io.ByteArrayOutputStream; |
5 | 7 | import java.io.IOException; |
@@ -113,6 +115,37 @@ public static BytesTransformer decompressGzip() { |
113 | 115 | return new GzipCompressor(false); |
114 | 116 | } |
115 | 117 |
|
| 118 | + /** |
| 119 | + * Create a {@link BytesTransformer} which returns the HMAC-SHA1 with given key, of the target byte array |
| 120 | + * |
| 121 | + * @param key to use for HMAC |
| 122 | + * @return hmac |
| 123 | + */ |
| 124 | + public static BytesTransformer hmacSha1(byte[] key) { |
| 125 | + return new HmacTransformer(key, HmacTransformer.HMAC_SHA1); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Create a {@link BytesTransformer} which returns the HMAC-SHA256 with given key, of the target byte array |
| 130 | + * |
| 131 | + * @param key to use for HMAC |
| 132 | + * @return hmac |
| 133 | + */ |
| 134 | + public static BytesTransformer hmacSha256(byte[] key) { |
| 135 | + return new HmacTransformer(key, HmacTransformer.HMAC_SHA256); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Create a {@link BytesTransformer} which returns the HMAC with given key, algorithm of the target byte array |
| 140 | + * |
| 141 | + * @param key to use for HMAC |
| 142 | + * @param algorithmName e.g. 'HmacSHA256' - check if the algorithm is supported on your JVM/runtime |
| 143 | + * @return hmac (length depends on algorithm) |
| 144 | + */ |
| 145 | + public static BytesTransformer hmac(byte[] key, String algorithmName) { |
| 146 | + return new HmacTransformer(key, algorithmName); |
| 147 | + } |
| 148 | + |
116 | 149 | /** |
117 | 150 | * Shuffles the internal byte array |
118 | 151 | */ |
@@ -303,4 +336,36 @@ public boolean supportInPlaceTransformation() { |
303 | 336 | return false; |
304 | 337 | } |
305 | 338 | } |
| 339 | + |
| 340 | + /** |
| 341 | + * HMAC transformer |
| 342 | + */ |
| 343 | + public static final class HmacTransformer implements BytesTransformer { |
| 344 | + static final String HMAC_SHA1 = "HmacSHA1"; |
| 345 | + static final String HMAC_SHA256 = "HmacSHA256"; |
| 346 | + |
| 347 | + private final byte[] secretKey; |
| 348 | + private final String macAlgorithmName; |
| 349 | + |
| 350 | + public HmacTransformer(byte[] secretKey, String macAlgorithmName) { |
| 351 | + this.macAlgorithmName = macAlgorithmName; |
| 352 | + this.secretKey = secretKey; |
| 353 | + } |
| 354 | + |
| 355 | + @Override |
| 356 | + public byte[] transform(byte[] currentArray, boolean inPlace) { |
| 357 | + try { |
| 358 | + Mac mac = Mac.getInstance(macAlgorithmName); |
| 359 | + mac.init(new SecretKeySpec(secretKey, macAlgorithmName)); |
| 360 | + return mac.doFinal(currentArray); |
| 361 | + } catch (Exception e) { |
| 362 | + throw new IllegalArgumentException(e); |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + @Override |
| 367 | + public boolean supportInPlaceTransformation() { |
| 368 | + return false; |
| 369 | + } |
| 370 | + } |
306 | 371 | } |
0 commit comments