mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Encrypt without allocating new buffers.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user