SEC-1689: Re-instate crypto as separate library (for use in non-Spring Security apps), as well as packaging with core.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package org.springframework.security.crypto.codec;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
public class Utf8Tests {
|
||||
|
||||
// SEC-1752
|
||||
@Test
|
||||
public void utf8EncodesAndDecodesCorrectly() throws Exception {
|
||||
byte[] bytes = Utf8.encode("6048b75ed560785c");
|
||||
assertEquals(16, bytes.length);
|
||||
assertTrue(Arrays.equals("6048b75ed560785c".getBytes("UTF-8"), bytes));
|
||||
|
||||
String decoded = Utf8.decode(bytes);
|
||||
|
||||
assertEquals("6048b75ed560785c", decoded);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.springframework.security.crypto.encrypt;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EncryptorsTests {
|
||||
|
||||
@Test
|
||||
public void standard() throws Exception {
|
||||
BytesEncryptor encryptor = Encryptors.standard("password", "5c0744940b5c369b");
|
||||
byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
|
||||
assertNotNull(result);
|
||||
assertFalse(new String(result).equals("text"));
|
||||
assertEquals("text", new String(encryptor.decrypt(result)));
|
||||
assertFalse(new String(result).equals(new String(encryptor.encrypt("text".getBytes()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void text() {
|
||||
TextEncryptor encryptor = Encryptors.text("password", "5c0744940b5c369b");
|
||||
String result = encryptor.encrypt("text");
|
||||
assertNotNull(result);
|
||||
assertFalse(result.equals("text"));
|
||||
assertEquals("text", encryptor.decrypt(result));
|
||||
assertFalse(result.equals(encryptor.encrypt("text")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryableText() {
|
||||
TextEncryptor encryptor = Encryptors.queryableText("password", "5c0744940b5c369b");
|
||||
String result = encryptor.encrypt("text");
|
||||
assertNotNull(result);
|
||||
assertFalse(result.equals("text"));
|
||||
assertEquals("text", encryptor.decrypt(result));
|
||||
assertTrue(result.equals(encryptor.encrypt("text")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noOpText() {
|
||||
TextEncryptor encryptor = Encryptors.noOpText();
|
||||
assertEquals("text", encryptor.encrypt("text"));
|
||||
assertEquals("text", encryptor.decrypt("text"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.springframework.security.crypto.keygen;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
|
||||
public class KeyGeneratorsTests {
|
||||
|
||||
@Test
|
||||
public void secureRandom() {
|
||||
BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom();
|
||||
assertEquals(8, keyGenerator.getKeyLength());
|
||||
byte[] key = keyGenerator.generateKey();
|
||||
assertEquals(8, key.length);
|
||||
byte[] key2 = keyGenerator.generateKey();
|
||||
assertFalse(Arrays.equals(key, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secureRandomCustomLength() {
|
||||
BytesKeyGenerator keyGenerator = KeyGenerators.secureRandom(21);
|
||||
assertEquals(21, keyGenerator.getKeyLength());
|
||||
byte[] key = keyGenerator.generateKey();
|
||||
assertEquals(21, key.length);
|
||||
byte[] key2 = keyGenerator.generateKey();
|
||||
assertFalse(Arrays.equals(key, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shared() throws Exception {
|
||||
BytesKeyGenerator keyGenerator = KeyGenerators.shared(21);
|
||||
assertEquals(21, keyGenerator.getKeyLength());
|
||||
byte[] key = keyGenerator.generateKey();
|
||||
assertEquals(21, key.length);
|
||||
byte[] key2 = keyGenerator.generateKey();
|
||||
assertTrue(Arrays.equals(key, key2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string() {
|
||||
StringKeyGenerator keyGenerator = KeyGenerators.string();
|
||||
String hexStringKey = keyGenerator.generateKey();
|
||||
assertEquals(16, hexStringKey.length());
|
||||
assertEquals(8, Hex.decode(hexStringKey).length);
|
||||
String hexStringKey2 = keyGenerator.generateKey();
|
||||
assertFalse(hexStringKey.equals(hexStringKey2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.springframework.security.crypto.password;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
import org.springframework.security.crypto.codec.Utf8;
|
||||
import org.springframework.security.crypto.password.Digester;
|
||||
|
||||
public class DigesterTests {
|
||||
|
||||
@Test
|
||||
public void digestIsCorrectFor3Iterations() {
|
||||
Digester digester = new Digester("SHA-1", 3);
|
||||
byte[] result = digester.digest(Utf8.encode("text"));
|
||||
// echo -n text | openssl sha1 -binary | openssl sha1 -binary | openssl sha1
|
||||
assertEquals("3cfa28da425eca5b894f0af2b158adf7001e000f", new String(Hex.encode(result)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.springframework.security.crypto.password;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StandardPasswordEncoderTests {
|
||||
|
||||
private StandardPasswordEncoder encoder = new StandardPasswordEncoder("secret");
|
||||
|
||||
@Test
|
||||
public void matches() {
|
||||
String result = encoder.encode("password");
|
||||
assertFalse(result.equals("password"));
|
||||
assertTrue(encoder.matches("password", result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesLengthChecked() {
|
||||
String result = encoder.encode("password");
|
||||
assertFalse(encoder.matches("password", result.substring(0,result.length()-2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notMatches() {
|
||||
String result = encoder.encode("password");
|
||||
assertFalse(encoder.matches("bogus", result));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.springframework.security.crypto.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
|
||||
public class EncodingUtilsTests {
|
||||
|
||||
@Test
|
||||
public void hexEncode() {
|
||||
byte[] bytes = new byte[] { (byte)0x01, (byte)0xFF, (byte)65, (byte)66, (byte)67, (byte)0xC0, (byte)0xC1, (byte)0xC2 };
|
||||
String result = new String(Hex.encode(bytes));
|
||||
assertEquals("01ff414243c0c1c2", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hexDecode() {
|
||||
byte[] bytes = new byte[] { (byte)0x01, (byte)0xFF, (byte)65, (byte)66, (byte)67, (byte)0xC0, (byte)0xC1, (byte)0xC2 };
|
||||
byte[] result = Hex.decode("01ff414243c0c1c2");
|
||||
assertTrue(Arrays.equals(bytes, result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatenate() {
|
||||
byte[] bytes = new byte[] { (byte)0x01, (byte)0xFF, (byte)65, (byte)66, (byte)67, (byte)0xC0, (byte)0xC1, (byte)0xC2 };
|
||||
byte[] one = new byte[] { (byte)0x01 };
|
||||
byte[] two = new byte[] { (byte)0xFF, (byte)65, (byte)66 };
|
||||
byte[] three = new byte[] { (byte)67, (byte)0xC0, (byte)0xC1, (byte)0xC2 };
|
||||
assertTrue(Arrays.equals(bytes, EncodingUtils.concatenate(one, two, three)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subArray() {
|
||||
byte[] bytes = new byte[] { (byte)0x01, (byte)0xFF, (byte)65, (byte)66, (byte)67, (byte)0xC0, (byte)0xC1, (byte)0xC2 };
|
||||
byte[] two = new byte[] { (byte)0xFF, (byte)65, (byte)66 };
|
||||
byte[] subArray = EncodingUtils.subArray(bytes, 1, 4);
|
||||
assertEquals(3, subArray.length);
|
||||
assertTrue(Arrays.equals(two, subArray));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user