Skip to content

Commit edb22d3

Browse files
committed
reduce memory consumption to get encrypted payloads with hmac
1 parent 29b79f7 commit edb22d3

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import haveno.common.util.Hex;
2121
import haveno.common.util.Utilities;
2222
import java.io.ByteArrayOutputStream;
23-
import java.io.IOException;
2423
import java.security.InvalidKeyException;
2524
import java.security.KeyFactory;
2625
import java.security.KeyPair;
@@ -101,36 +100,19 @@ public static SecretKey getSecretKeyFromBytes(byte[] secretKeyBytes) {
101100
///////////////////////////////////////////////////////////////////////////////////////////
102101

103102
private static byte[] getPayloadWithHmac(byte[] payload, SecretKey secretKey) {
104-
byte[] payloadWithHmac;
105103
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)) {
111106
outputStream.write(payload);
112107
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();
125109
}
126110
} catch (Throwable e) {
127111
log.error("Could not create hmac", e);
128-
throw new RuntimeException("Could not create hmac");
112+
throw new RuntimeException("Could not create hmac", e);
129113
}
130-
return payloadWithHmac;
131114
}
132115

133-
134116
private static boolean verifyHmac(byte[] message, byte[] hmac, SecretKey secretKey) {
135117
try {
136118
byte[] hmacTest = getHmac(message, secretKey);

0 commit comments

Comments
 (0)