@@ -101,36 +101,19 @@ public static SecretKey getSecretKeyFromBytes(byte[] secretKeyBytes) {
101
101
///////////////////////////////////////////////////////////////////////////////////////////
102
102
103
103
private static byte [] getPayloadWithHmac (byte [] payload , SecretKey secretKey ) {
104
- byte [] payloadWithHmac ;
105
104
try {
106
-
107
- ByteArrayOutputStream outputStream = null ;
108
- try {
109
- byte [] hmac = getHmac (payload , secretKey );
110
- outputStream = new ByteArrayOutputStream ();
105
+ byte [] hmac = getHmac (payload , secretKey );
106
+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream (payload .length + hmac .length )) {
111
107
outputStream .write (payload );
112
108
outputStream .write (hmac );
113
- outputStream .flush ();
114
- payloadWithHmac = outputStream .toByteArray ().clone ();
115
- } catch (IOException | NoSuchProviderException e ) {
116
- log .error ("Could not create hmac" , e );
117
- throw new RuntimeException ("Could not create hmac" );
118
- } finally {
119
- if (outputStream != null ) {
120
- try {
121
- outputStream .close ();
122
- } catch (IOException ignored ) {
123
- }
124
- }
109
+ return outputStream .toByteArray ();
125
110
}
126
111
} catch (Throwable e ) {
127
112
log .error ("Could not create hmac" , e );
128
- throw new RuntimeException ("Could not create hmac" );
113
+ throw new RuntimeException ("Could not create hmac" , e );
129
114
}
130
- return payloadWithHmac ;
131
115
}
132
116
133
-
134
117
private static boolean verifyHmac (byte [] message , byte [] hmac , SecretKey secretKey ) {
135
118
try {
136
119
byte [] hmacTest = getHmac (message , secretKey );
0 commit comments