public class CtrCryptoOutputStream extends CryptoOutputStream
CtrCryptoOutputStream encrypts data. It is not thread-safe. AES CTR mode is required in order to ensure that the plain text and cipher text have a 1:1 mapping. The encryption is buffer based. The key points of the encryption are (1) calculating counter and (2) padding through stream position.
counter = base + pos/(algorithm blocksize); padding = pos%(algorithm blocksize);
The underlying stream offset is maintained as state.
This class should only be used with blocking sinks. Using this class to wrap a non-blocking sink may lead to high CPU usage.
Modifier | Constructor and Description |
---|---|
protected |
CtrCryptoOutputStream(Output output,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoOutputStream . |
protected |
CtrCryptoOutputStream(Output output,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoOutputStream . |
protected |
CtrCryptoOutputStream(OutputStream out,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoOutputStream . |
protected |
CtrCryptoOutputStream(OutputStream outputStream,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoOutputStream . |
|
CtrCryptoOutputStream(Properties props,
OutputStream out,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoOutputStream . |
|
CtrCryptoOutputStream(Properties properties,
OutputStream outputStream,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoOutputStream . |
|
CtrCryptoOutputStream(Properties props,
WritableByteChannel out,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoOutputStream . |
|
CtrCryptoOutputStream(Properties properties,
WritableByteChannel channel,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoOutputStream . |
protected |
CtrCryptoOutputStream(WritableByteChannel channel,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv)
Constructs a
CtrCryptoOutputStream . |
protected |
CtrCryptoOutputStream(WritableByteChannel channel,
CryptoCipher cipher,
int bufferSize,
byte[] key,
byte[] iv,
long streamOffset)
Constructs a
CtrCryptoOutputStream . |
Modifier and Type | Method and Description |
---|---|
protected void |
encrypt()
Does the encryption, input is
CryptoOutputStream.inBuffer and output is
CryptoOutputStream.outBuffer . |
protected void |
encryptFinal()
Does final encryption of the last data.
|
protected long |
getStreamOffset()
Get the underlying stream offset
|
protected void |
initCipher()
Overrides the
CryptoOutputStream.initCipher() . |
protected void |
setStreamOffset(long streamOffset)
Set the underlying stream offset
|
checkStream, close, flush, freeBuffers, getBufferSize, getCipher, getInBuffer, getOutBuffer, isOpen, write, write, write
write
protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoOutputStream
.output
- the Output instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoOutputStream
.output
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.IOException
- if an I/O error occurs.protected CtrCryptoOutputStream(OutputStream out, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoOutputStream
.out
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoOutputStream
.outputStream
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.IOException
- if an I/O error occurs.public CtrCryptoOutputStream(Properties props, OutputStream out, byte[] key, byte[] iv) throws IOException
CtrCryptoOutputStream
.props
- The Properties
class represents a set of
properties.out
- the output stream.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.public CtrCryptoOutputStream(Properties properties, OutputStream outputStream, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoOutputStream
.properties
- The Properties
class represents a set of
properties.outputStream
- the output stream.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.IOException
- if an I/O error occurs.public CtrCryptoOutputStream(Properties props, WritableByteChannel out, byte[] key, byte[] iv) throws IOException
CtrCryptoOutputStream
.props
- The Properties
class represents a set of
properties.out
- the WritableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.public CtrCryptoOutputStream(Properties properties, WritableByteChannel channel, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoOutputStream
.properties
- The Properties
class represents a set of
properties.channel
- the WritableByteChannel instance.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.IOException
- if an I/O error occurs.protected CtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv) throws IOException
CtrCryptoOutputStream
.channel
- the WritableByteChannel instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.IOException
- if an I/O error occurs.protected CtrCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException
CtrCryptoOutputStream
.channel
- the WritableByteChannel instance.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.iv
- Initialization vector for the cipher.streamOffset
- the start offset in the data.IOException
- if an I/O error occurs.protected void encrypt() throws IOException
CryptoOutputStream.inBuffer
and output is
CryptoOutputStream.outBuffer
.encrypt
in class CryptoOutputStream
IOException
- if an I/O error occurs.protected void encryptFinal() throws IOException
encryptFinal
in class CryptoOutputStream
IOException
- if an I/O error occurs.protected long getStreamOffset()
protected void initCipher()
CryptoOutputStream.initCipher()
. Initializes the
cipher.initCipher
in class CryptoOutputStream
protected void setStreamOffset(long streamOffset)
streamOffset
- the underlying stream offsetCopyright © 2016–2022 The Apache Software Foundation. All rights reserved.