org.apache.cassandra.security

## Class EncryptionUtils

- java.lang.Object
- - org.apache.cassandra.security.EncryptionUtils

    public class EncryptionUtils
    extends java.lang.Object

Encryption and decryption functions specific to the commit log. See comments in `EncryptedSegment` for details on the binary format. The normal, and expected, invocation pattern is to compress then encrypt the data on the encryption pass, then decrypt and uncompress the data on the decrypt pass.

### Nested Class Summary

Nested Classes
`static class` `EncryptionUtils.ChannelProxyReadChannel`

### Field Summary

Fields
`static int` `COMPRESSED_BLOCK_HEADER_SIZE`
`static int` `ENCRYPTED_BLOCK_HEADER_SIZE`

### Constructor Summary

Constructors
Constructor and Description
`EncryptionUtils()`

### Method Summary

All Methods Static Methods Concrete Methods
`static java.nio.ByteBuffer` `compress(java.nio.ByteBuffer inputBuffer, java.nio.ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor)` Compress the raw data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.
`static java.nio.ByteBuffer` `decrypt(FileDataInput fileDataInput, java.nio.ByteBuffer outputBuffer, boolean allowBufferResize, javax.crypto.Cipher cipher)`
`static java.nio.ByteBuffer` `decrypt(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer outputBuffer, boolean allowBufferResize, javax.crypto.Cipher cipher)` Decrypt the input data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.
`static java.nio.ByteBuffer` `encrypt(java.nio.ByteBuffer inputBuffer, java.nio.ByteBuffer outputBuffer, boolean allowBufferResize, javax.crypto.Cipher cipher)`
`static java.nio.ByteBuffer` `encryptAndWrite(java.nio.ByteBuffer inputBuffer, java.nio.channels.WritableByteChannel channel, boolean allowBufferResize, javax.crypto.Cipher cipher)` Encrypt the input data, and writes out to the same input buffer; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.
`static int` `uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, ICompressor compressor)`
`static java.nio.ByteBuffer` `uncompress(java.nio.ByteBuffer inputBuffer, java.nio.ByteBuffer outputBuffer, boolean allowBufferResize, ICompressor compressor)` Uncompress the input data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### COMPRESSED_BLOCK_HEADER_SIZE

    public static final int COMPRESSED_BLOCK_HEADER_SIZE

See Also:
  Constant Field Values

#### ENCRYPTED_BLOCK_HEADER_SIZE

    public static final int ENCRYPTED_BLOCK_HEADER_SIZE

See Also:
  Constant Field Values

### Constructor Detail

#### EncryptionUtils

    public EncryptionUtils()

### Method Detail

#### compress

    public static java.nio.ByteBuffer compress(java.nio.ByteBuffer inputBuffer,
                                               java.nio.ByteBuffer outputBuffer,
                                               boolean allowBufferResize,
                                               ICompressor compressor)
                                        throws java.io.IOException

Compress the raw data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer. Write the two header lengths (plain text length, compressed length) to the beginning of the buffer as we want those values encapsulated in the encrypted block, as well.

Returns:
  the byte buffer that was actaully written to; it may be the `outputBuffer` if it had enough capacity, or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
Throws:
  `java.io.IOException`

#### encryptAndWrite

    public static java.nio.ByteBuffer encryptAndWrite(java.nio.ByteBuffer inputBuffer,
                                                      java.nio.channels.WritableByteChannel channel,
                                                      boolean allowBufferResize,
                                                      javax.crypto.Cipher cipher)
                                               throws java.io.IOException

Encrypt the input data, and writes out to the same input buffer; if the buffer is not big enough, deallocate current, and allocate a large enough buffer. Writes the cipher text and headers out to the channel, as well. Note: channel is a parameter as we cannot write header info to the output buffer as we assume the input and output buffers can be the same buffer (and writing the headers to a shared buffer will corrupt any input data). Hence, we write out the headers directly to the channel, and then the cipher text (once encrypted).

Throws:
  `java.io.IOException`

#### encrypt

    public static java.nio.ByteBuffer encrypt(java.nio.ByteBuffer inputBuffer,
                                              java.nio.ByteBuffer outputBuffer,
                                              boolean allowBufferResize,
                                              javax.crypto.Cipher cipher)
                                       throws java.io.IOException

Throws:
  `java.io.IOException`

#### decrypt

    public static java.nio.ByteBuffer decrypt(java.nio.channels.ReadableByteChannel channel,
                                              java.nio.ByteBuffer outputBuffer,
                                              boolean allowBufferResize,
                                              javax.crypto.Cipher cipher)
                                       throws java.io.IOException

Decrypt the input data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.

Returns:
  the byte buffer that was actaully written to; it may be the `outputBuffer` if it had enough capacity, or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
Throws:
  `java.io.IOException`

#### decrypt

    public static java.nio.ByteBuffer decrypt(FileDataInput fileDataInput,
                                              java.nio.ByteBuffer outputBuffer,
                                              boolean allowBufferResize,
                                              javax.crypto.Cipher cipher)
                                       throws java.io.IOException

Throws:
  `java.io.IOException`

#### uncompress

    public static java.nio.ByteBuffer uncompress(java.nio.ByteBuffer inputBuffer,
                                                 java.nio.ByteBuffer outputBuffer,
                                                 boolean allowBufferResize,
                                                 ICompressor compressor)
                                          throws java.io.IOException

Uncompress the input data, as well as manage sizing of the `outputBuffer`; if the buffer is not big enough, deallocate current, and allocate a large enough buffer.

Returns:
  the byte buffer that was actaully written to; it may be the `outputBuffer` if it had enough capacity, or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
Throws:
  `java.io.IOException`

#### uncompress

    public static int uncompress(byte[] input,
                                 int inputOffset,
                                 int inputLength,
                                 byte[] output,
                                 int outputOffset,
                                 ICompressor compressor)
                          throws java.io.IOException

Throws:
  `java.io.IOException`