Search in sources :

Example 6 with PaddingInputStream

use of org.whispersystems.signalservice.internal.crypto.PaddingInputStream in project Signal-Android by WhisperSystems.

the class AttachmentCipherTest method attachment_encryptDecryptPaddedContent.

@Test
public void attachment_encryptDecryptPaddedContent() throws IOException, InvalidMessageException {
    int[] lengths = { 531, 600, 724, 1019, 1024 };
    for (int length : lengths) {
        byte[] plaintextInput = new byte[length];
        for (int i = 0; i < length; i++) {
            plaintextInput[i] = (byte) 0x97;
        }
        byte[] key = Util.getSecretBytes(64);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(plaintextInput);
        InputStream dataStream = new PaddingInputStream(inputStream, length);
        ByteArrayOutputStream encryptedStream = new ByteArrayOutputStream();
        DigestingOutputStream digestStream = new AttachmentCipherOutputStreamFactory(key, null).createFor(encryptedStream);
        Util.copy(dataStream, digestStream);
        digestStream.flush();
        byte[] digest = digestStream.getTransmittedDigest();
        byte[] encryptedData = encryptedStream.toByteArray();
        encryptedStream.close();
        inputStream.close();
        File cipherFile = writeToFile(encryptedData);
        InputStream decryptedStream = AttachmentCipherInputStream.createForAttachment(cipherFile, length, key, digest);
        byte[] plaintextOutput = readInputStreamFully(decryptedStream);
        assertArrayEquals(plaintextInput, plaintextOutput);
        cipherFile.delete();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PaddingInputStream(org.whispersystems.signalservice.internal.crypto.PaddingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PaddingInputStream(org.whispersystems.signalservice.internal.crypto.PaddingInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) AttachmentCipherOutputStreamFactory(org.whispersystems.signalservice.internal.push.http.AttachmentCipherOutputStreamFactory) Test(org.junit.Test)

Aggregations

PaddingInputStream (org.whispersystems.signalservice.internal.crypto.PaddingInputStream)6 AttachmentCipherOutputStreamFactory (org.whispersystems.signalservice.internal.push.http.AttachmentCipherOutputStreamFactory)6 InputStream (java.io.InputStream)5 PushAttachmentData (org.whispersystems.signalservice.internal.push.PushAttachmentData)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)1 AttachmentUploadAttributes (org.whispersystems.signalservice.internal.push.AttachmentUploadAttributes)1 AttachmentPointer (org.whispersystems.signalservice.internal.push.SignalServiceProtos.AttachmentPointer)1