Skip to content

Commit 46f0b80

Browse files
committed
reduce memory consumption to get encrypted payloads with hmac
1 parent 77674d3 commit 46f0b80

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

common/src/main/java/haveno/common/crypto/Encryption.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,19 @@ public static SecretKey getSecretKeyFromBytes(byte[] secretKeyBytes) {
101101
///////////////////////////////////////////////////////////////////////////////////////////
102102

103103
private static byte[] getPayloadWithHmac(byte[] payload, SecretKey secretKey) {
104-
byte[] payloadWithHmac;
105104
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)) {
111107
outputStream.write(payload);
112108
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();
125110
}
126111
} catch (Throwable e) {
127112
log.error("Could not create hmac", e);
128-
throw new RuntimeException("Could not create hmac");
113+
throw new RuntimeException("Could not create hmac", e);
129114
}
130-
return payloadWithHmac;
131115
}
132116

133-
134117
private static boolean verifyHmac(byte[] message, byte[] hmac, SecretKey secretKey) {
135118
try {
136119
byte[] hmacTest = getHmac(message, secretKey);

0 commit comments

Comments
 (0)