SEC-1689: Moved core codec code into crypto package and removed existing duplication (Hex encoding etc). Refactoring of crypto code to use CharSequence for where possible instead of String.

This commit is contained in:
Luke Taylor
2011-03-17 00:48:53 +00:00
parent 3a3b2df1c5
commit e470eaa41d
34 changed files with 162 additions and 159 deletions

View File

@@ -10,9 +10,9 @@ import org.junit.Test;
public class EncryptorsTests {
@Test
public void standard() {
public void standard() throws Exception {
BytesEncryptor encryptor = Encryptors.standard("password", "5c0744940b5c369b");
byte[] result = encryptor.encrypt("text".getBytes());
byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
assertNotNull(result);
assertFalse(new String(result).equals("text"));
assertEquals("text", new String(encryptor.decrypt(result)));

View File

@@ -6,7 +6,7 @@ import static org.junit.Assert.assertFalse;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.security.crypto.util.EncodingUtils;
import org.springframework.security.crypto.codec.Hex;
public class KeyGeneratorsTests {
@@ -35,7 +35,7 @@ public class KeyGeneratorsTests {
StringKeyGenerator keyGenerator = KeyGenerators.string();
String hexStringKey = keyGenerator.generateKey();
assertEquals(16, hexStringKey.length());
assertEquals(8, EncodingUtils.hexDecode(hexStringKey).length);
assertEquals(8, Hex.decode(hexStringKey).length);
String hexStringKey2 = keyGenerator.generateKey();
assertFalse(hexStringKey.equals(hexStringKey2));
}

View File

@@ -1,4 +1,4 @@
package org.springframework.security.crypto.util;
package org.springframework.security.crypto.password;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -7,6 +7,7 @@ import java.security.MessageDigest;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.security.crypto.password.Digester;
public class DigesterTests {

View File

@@ -19,7 +19,7 @@ public class StandardPasswordEncoderTests {
@Test
public void matchesLengthChecked() {
String result = encoder.encode("password");
assertFalse(encoder.matches("password", result.substring(0,result.length()-1)));
assertFalse(encoder.matches("password", result.substring(0,result.length()-2)));
}
@Test

View File

@@ -6,20 +6,21 @@ 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 = EncodingUtils.hexEncode(bytes);
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 = EncodingUtils.hexDecode("01ff414243c0c1c2");
byte[] result = Hex.decode("01ff414243c0c1c2");
assertTrue(Arrays.equals(bytes, result));
}