Added PBKDF2 to crypto component.

This commit is contained in:
akwizgran
2013-04-16 12:04:23 +01:00
parent 0dcc1a6d54
commit e343c9f4bb
4 changed files with 175 additions and 16 deletions

View File

@@ -0,0 +1,41 @@
package net.sf.briar.crypto;
import static org.junit.Assert.assertArrayEquals;
import java.util.Random;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.crypto.CryptoComponent;
import org.junit.Test;
public class PasswordBasedKdfTest extends BriarTestCase {
@Test
public void testEncryptionAndDecryption() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[123];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);
byte[] output = crypto.decryptWithPassword(ciphertext, password);
assertArrayEquals(input, output);
}
@Test
public void testInvalidCiphertextReturnsNull() {
CryptoComponent crypto = new CryptoComponentImpl();
Random random = new Random();
byte[] input = new byte[123];
random.nextBytes(input);
char[] password = "password".toCharArray();
byte[] ciphertext = crypto.encryptWithPassword(input, password);
// Modify the ciphertext
int position = random.nextInt(ciphertext.length);
int value = random.nextInt(256);
ciphertext[position] = (byte) value;
byte[] output = crypto.decryptWithPassword(ciphertext, password);
assertNull(output);
}
}