Refactored Digest encoding for better support of all MessageDigest algorithms, such as the SHA family.
This commit is contained in:
@@ -23,6 +23,7 @@ import junit.framework.TestCase;
|
||||
*
|
||||
* @author colin sampaleanu
|
||||
* @author Ben Alex
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class Md5PasswordEncoderTests extends TestCase {
|
||||
@@ -36,11 +37,17 @@ public class Md5PasswordEncoderTests extends TestCase {
|
||||
String encoded = pe.encodePassword(raw, salt);
|
||||
assertTrue(pe.isPasswordValid(encoded, raw, salt));
|
||||
assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
|
||||
assertTrue(encoded.length() == 32);
|
||||
assertEquals("a68aafd90299d0b137de28fb4bb68573", encoded);
|
||||
assertEquals("MD5", pe.getAlgorithm());
|
||||
}
|
||||
|
||||
// now try Base64
|
||||
public void testBase64() throws Exception {
|
||||
Md5PasswordEncoder pe = new Md5PasswordEncoder();
|
||||
pe.setEncodeHashAsBase64(true);
|
||||
encoded = pe.encodePassword(raw, salt);
|
||||
String raw = "abc123";
|
||||
String badRaw = "abc321";
|
||||
String salt = "THIS_IS_A_SALT";
|
||||
String encoded = pe.encodePassword(raw, salt);
|
||||
assertTrue(pe.isPasswordValid(encoded, raw, salt));
|
||||
assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
|
||||
assertTrue(encoded.length() != 32);
|
||||
|
||||
@@ -23,6 +23,7 @@ import junit.framework.TestCase;
|
||||
*
|
||||
* @author colin sampaleanu
|
||||
* @author Ben Alex
|
||||
* @author Ray Krueger
|
||||
* @version $Id$
|
||||
*/
|
||||
public class ShaPasswordEncoderTests extends TestCase {
|
||||
@@ -36,13 +37,36 @@ public class ShaPasswordEncoderTests extends TestCase {
|
||||
String encoded = pe.encodePassword(raw, salt);
|
||||
assertTrue(pe.isPasswordValid(encoded, raw, salt));
|
||||
assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
|
||||
assertTrue(encoded.length() == 40);
|
||||
assertEquals("b2f50ffcbd3407fe9415c062d55f54731f340d32", encoded);
|
||||
|
||||
// now try Base64
|
||||
}
|
||||
|
||||
public void testBase64() throws Exception {
|
||||
ShaPasswordEncoder pe = new ShaPasswordEncoder();
|
||||
pe.setEncodeHashAsBase64(true);
|
||||
encoded = pe.encodePassword(raw, salt);
|
||||
String raw = "abc123";
|
||||
String badRaw = "abc321";
|
||||
String salt = "THIS_IS_A_SALT";
|
||||
String encoded = pe.encodePassword(raw, salt);
|
||||
assertTrue(pe.isPasswordValid(encoded, raw, salt));
|
||||
assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
|
||||
assertTrue(encoded.length() != 40);
|
||||
}
|
||||
|
||||
public void test256() throws Exception {
|
||||
ShaPasswordEncoder pe = new ShaPasswordEncoder(256);
|
||||
String encoded = pe.encodePassword("abc123", null);
|
||||
assertEquals("6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090", encoded);
|
||||
String encodedWithSalt = pe.encodePassword("abc123", "THIS_IS_A_SALT");
|
||||
assertEquals("4b79b7de23eb23b78cc5ede227d532b8a51f89b2ec166f808af76b0dbedc47d7", encodedWithSalt);
|
||||
}
|
||||
|
||||
public void testInvalidStrength() throws Exception {
|
||||
try {
|
||||
new ShaPasswordEncoder(666);
|
||||
fail("IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
//expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user