mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +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.Cipher;
|
||||||
import javax.crypto.IllegalBlockSizeException;
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.ShortBufferException;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
class PacketEncrypterImpl extends FilterOutputStream
|
class PacketEncrypterImpl extends FilterOutputStream
|
||||||
@@ -67,22 +68,35 @@ implements PacketEncrypter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
// FIXME: Encrypt into same buffer
|
byte[] buf = new byte[] {(byte) b};
|
||||||
byte[] ciphertext = packetCipher.update(new byte[] {(byte) b});
|
try {
|
||||||
if(ciphertext != null) out.write(ciphertext);
|
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
|
@Override
|
||||||
public void write(byte[] b) throws IOException {
|
public void write(byte[] b) throws IOException {
|
||||||
// FIXME: Encrypt into same buffer
|
try {
|
||||||
byte[] ciphertext = packetCipher.update(b);
|
int i = packetCipher.update(b, 0, b.length, b);
|
||||||
if(ciphertext != null) out.write(ciphertext);
|
assert i <= b.length;
|
||||||
|
out.write(b, 0, i);
|
||||||
|
} catch(ShortBufferException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
// FIXME: Encrypt into same buffer
|
try {
|
||||||
byte[] ciphertext = packetCipher.update(b, off, len);
|
int i = packetCipher.update(b, off, len, b, off);
|
||||||
if(ciphertext != null) out.write(ciphertext);
|
assert i <= len;
|
||||||
|
out.write(b, off, i);
|
||||||
|
} catch(ShortBufferException badCipher) {
|
||||||
|
throw new RuntimeException(badCipher);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user