From 4e5dd914dd6b29059863be946009fb6fced0f1c2 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Mon, 26 Feb 2024 10:56:14 -0600 Subject: [PATCH] Add varargs variant to HmacUtil --- .../textsecuregcm/util/HmacUtils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/util/HmacUtils.java b/service/src/main/java/org/whispersystems/textsecuregcm/util/HmacUtils.java index 6469fe01b..ec059884d 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/util/HmacUtils.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/util/HmacUtils.java @@ -27,16 +27,28 @@ public final class HmacUtils { } }); - public static byte[] hmac256(final byte[] key, final byte[] input) { + private static Mac initializedThreadLocalMac(final byte[] key) { try { final Mac mac = THREAD_LOCAL_HMAC_SHA_256.get(); mac.init(new SecretKeySpec(key, HMAC_SHA_256)); - return mac.doFinal(input); + return mac; } catch (final InvalidKeyException e) { throw new RuntimeException(e); } } + public static byte[] hmac256(final byte[] key, final byte[] input) { + return initializedThreadLocalMac(key).doFinal(input); + } + + public static byte[] hmac256(final byte[] key, final byte[]... inputs) { + final Mac mac = initializedThreadLocalMac(key); + for (byte[] input : inputs) { + mac.update(input); + } + return mac.doFinal(); + } + public static byte[] hmac256(final byte[] key, final String input) { return hmac256(key, input.getBytes(StandardCharsets.UTF_8)); }