in encryption/src/main/java/org/apache/solr/encryption/crypto/EncryptingOutputStream.java [93:124]
public EncryptingOutputStream(OutputStream outputStream,
long position,
byte[] iv,
byte[] key,
AesCtrEncrypterFactory factory,
int bufferCapacity)
throws IOException {
if (position < 0) {
throw new IllegalArgumentException("Invalid position " + position);
}
this.outputStream = outputStream;
assert bufferCapacity % AES_BLOCK_SIZE == 0;
inBuffer = new byte[bufferCapacity];
outBuffer = new byte[bufferCapacity + AES_BLOCK_SIZE];
oneByteBuf = new byte[1];
long counter;
if (position == 0) {
iv = generateRandomIv();
// Write the IV at the beginning of the output stream. It's public.
outputStream.write(iv, 0, iv.length);
counter = 0;
} else if (iv == null) {
throw new IllegalArgumentException("IV must be provided when position is not zero");
} else {
counter = position / AES_BLOCK_SIZE;
padding = (int) (position & (AES_BLOCK_SIZE - 1));
inSize = padding;
}
this.iv = iv;
encrypter = factory.create(key, iv);
encrypter.init(counter);
}