Search in sources :

Example 1 with CipherFactory

use of org.apache.cassandra.security.CipherFactory in project cassandra by apache.

the class SegmentReaderTest method underlyingEncryptedSegmenterTest.

public void underlyingEncryptedSegmenterTest(BiFunction<FileDataInput, Integer, ByteBuffer> readFun) throws IOException {
    EncryptionContext context = EncryptionContextGenerator.createContext(true);
    CipherFactory cipherFactory = new CipherFactory(context.getTransparentDataEncryptionOptions());
    int plainTextLength = (1 << 13) - 137;
    ByteBuffer plainTextBuffer = ByteBuffer.allocate(plainTextLength);
    random.nextBytes(plainTextBuffer.array());
    ByteBuffer compressedBuffer = EncryptionUtils.compress(plainTextBuffer, null, true, context.getCompressor());
    Cipher cipher = cipherFactory.getEncryptor(context.getTransparentDataEncryptionOptions().cipher, context.getTransparentDataEncryptionOptions().key_alias);
    File encryptedFile = File.createTempFile("encrypted-segment-", ".log");
    encryptedFile.deleteOnExit();
    FileChannel channel = new RandomAccessFile(encryptedFile, "rw").getChannel();
    channel.write(ByteBufferUtil.bytes(plainTextLength));
    EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, cipher);
    channel.close();
    try (RandomAccessReader reader = RandomAccessReader.open(encryptedFile)) {
        context = EncryptionContextGenerator.createContext(cipher.getIV(), true);
        EncryptedSegmenter segmenter = new EncryptedSegmenter(reader, context);
        SyncSegment syncSegment = segmenter.nextSegment(0, (int) reader.length());
        // EncryptedSegmenter includes the Sync header length in the syncSegment.endPosition (value)
        Assert.assertEquals(plainTextLength, syncSegment.endPosition - CommitLogSegment.SYNC_MARKER_SIZE);
        ByteBuffer fileBuffer = readFun.apply(syncSegment.input, plainTextLength);
        plainTextBuffer.position(0);
        Assert.assertEquals(plainTextBuffer, fileBuffer);
    }
}
Also used : EncryptionContext(org.apache.cassandra.security.EncryptionContext) RandomAccessFile(java.io.RandomAccessFile) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) FileChannel(java.nio.channels.FileChannel) EncryptedSegmenter(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.EncryptedSegmenter) CipherFactory(org.apache.cassandra.security.CipherFactory) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SyncSegment(org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)

Aggregations

File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 Cipher (javax.crypto.Cipher)1 EncryptedSegmenter (org.apache.cassandra.db.commitlog.CommitLogSegmentReader.EncryptedSegmenter)1 SyncSegment (org.apache.cassandra.db.commitlog.CommitLogSegmentReader.SyncSegment)1 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)1 CipherFactory (org.apache.cassandra.security.CipherFactory)1 EncryptionContext (org.apache.cassandra.security.EncryptionContext)1