|
20 | 20 | import haveno.common.util.Hex;
|
21 | 21 | import haveno.common.util.Utilities;
|
22 | 22 | import java.io.ByteArrayOutputStream;
|
23 |
| -import java.io.IOException; |
24 | 23 | import java.security.InvalidKeyException;
|
25 | 24 | import java.security.KeyFactory;
|
26 | 25 | import java.security.KeyPair;
|
@@ -101,36 +100,19 @@ public static SecretKey getSecretKeyFromBytes(byte[] secretKeyBytes) {
|
101 | 100 | ///////////////////////////////////////////////////////////////////////////////////////////
|
102 | 101 |
|
103 | 102 | private static byte[] getPayloadWithHmac(byte[] payload, SecretKey secretKey) {
|
104 |
| - byte[] payloadWithHmac; |
105 | 103 | try {
|
106 |
| - |
107 |
| - ByteArrayOutputStream outputStream = null; |
108 |
| - try { |
109 |
| - byte[] hmac = getHmac(payload, secretKey); |
110 |
| - outputStream = new ByteArrayOutputStream(); |
| 104 | + byte[] hmac = getHmac(payload, secretKey); |
| 105 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(payload.length + hmac.length)) { |
111 | 106 | outputStream.write(payload);
|
112 | 107 | 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 |
| - } |
| 108 | + return outputStream.toByteArray(); |
125 | 109 | }
|
126 | 110 | } catch (Throwable e) {
|
127 | 111 | log.error("Could not create hmac", e);
|
128 |
| - throw new RuntimeException("Could not create hmac"); |
| 112 | + throw new RuntimeException("Could not create hmac", e); |
129 | 113 | }
|
130 |
| - return payloadWithHmac; |
131 | 114 | }
|
132 | 115 |
|
133 |
| - |
134 | 116 | private static boolean verifyHmac(byte[] message, byte[] hmac, SecretKey secretKey) {
|
135 | 117 | try {
|
136 | 118 | byte[] hmacTest = getHmac(message, secretKey);
|
|
0 commit comments