Skip to content
Snippets Groups Projects
Commit 4dcf9a70 authored by akwizgran's avatar akwizgran
Browse files

Encrypt without allocating new buffers.

parent 2c387f80
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
class PacketEncrypterImpl extends FilterOutputStream
......@@ -67,22 +68,35 @@ implements PacketEncrypter {
@Override
public void write(int b) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(new byte[] {(byte) b});
if(ciphertext != null) out.write(ciphertext);
byte[] buf = new byte[] {(byte) b};
try {
int i = packetCipher.update(buf, 0, buf.length, buf);
assert i <= 1;
if(i == 1) out.write(b);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
@Override
public void write(byte[] b) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(b);
if(ciphertext != null) out.write(ciphertext);
try {
int i = packetCipher.update(b, 0, b.length, b);
assert i <= b.length;
out.write(b, 0, i);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(b, off, len);
if(ciphertext != null) out.write(ciphertext);
try {
int i = packetCipher.update(b, off, len, b, off);
assert i <= len;
out.write(b, off, i);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment