1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-21 05:31:42 +00:00

File encryption utilities

This commit is contained in:
jendib
2013-08-20 18:06:08 +02:00
parent 0bc658a396
commit 00b00f0d0c
7 changed files with 172 additions and 45 deletions

View File

@@ -0,0 +1,51 @@
package com.sismics.docs.core.util;
import java.io.InputStream;
import junit.framework.Assert;
import org.bouncycastle.util.io.Streams;
import org.junit.Test;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
/**
* Test of the encryption utilities.
*
* @author bgamard
*/
public class TestEncryptUtil {
/**
* Test private key.
*/
String pk = "OnceUponATime";
@Test
public void generatePrivateKeyTest() throws Exception {
String key = EncryptionUtil.generatePrivateKey();
System.out.println(key);
Assert.assertFalse(Strings.isNullOrEmpty(key));
}
@Test
public void encryptStreamTest() throws Exception {
InputStream inputStream = EncryptionUtil.encryptStream(this.getClass().getResourceAsStream("/file/udhr.pdf"), pk);
byte[] encryptedData = Streams.readAll(inputStream);
byte[] assertData = Streams.readAll(this.getClass().getResourceAsStream("/file/udhr_encrypted.pdf"));
Assert.assertTrue(ByteStreams.equal(
ByteStreams.newInputStreamSupplier(encryptedData),
ByteStreams.newInputStreamSupplier(assertData)));
}
@Test
public void decryptStreamTest() throws Exception {
InputStream inputStream = EncryptionUtil.decryptStream(this.getClass().getResourceAsStream("/file/udhr_encrypted.pdf"), pk);
byte[] encryptedData = Streams.readAll(inputStream);
byte[] assertData = Streams.readAll(this.getClass().getResourceAsStream("/file/udhr.pdf"));
Assert.assertTrue(ByteStreams.equal(
ByteStreams.newInputStreamSupplier(encryptedData),
ByteStreams.newInputStreamSupplier(assertData)));
}
}