001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestStringEncryptor.java 3718 2007-04-17 11:18:17Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import java.security.NoSuchAlgorithmException;
011: import junit.framework.TestCase;
012:
013: public class TestStringEncryptor extends TestCase {
014: public TestStringEncryptor(String name) {
015: super (name);
016: }
017:
018: public void testAdaptiveEncrypt() {
019: String encrypted_value_sha = "SHA:Hw/gszrpwzs0GnKTaKbwetEqBb0=";
020: String encrypted_value_shahex = "SHAHEX:1f0fe0b33ae9c33b341a729368a6f07ad12a05bd";
021: String encrypted_value_md5 = "MD5:JZ3Y+j2LYEvTmtYRY1Khsw==";
022: String encrypted_value_md5hex = "MD5HEX:259dd8fa3d8b604bd39ad6116352a1b3";
023: String encrypted_value_obf = "OBF:1w261wtu1ugo1vo41vmy1uh21wuk1w1c";
024: String encrypted_value_wrp = "WRP:XRadzcqykoN26sWuGe0j85TRoOaErinQr0+QKWsy1hlTMJdtRMkLUo+A4S/Wr3xcj+Va6jc0XLGfcWusuNMrcg==";
025: String encrypted_value_wrphex = "WRPHEX:5d169dcdcab2928376eac5ae19ed23f394d1a0e684ae29d0af4f90296b32d6195330976d44c90b528f80e12fd6af7c5c8fe55aea37345cb19f716bacb8d32b72";
026: String encrypted_value_none = "thevalue";
027: String value = "thevalue";
028:
029: try {
030: assertEquals(encrypted_value_sha, StringEncryptor
031: .adaptiveEncrypt(value, "SHA:123"));
032: assertEquals(encrypted_value_shahex, StringEncryptor
033: .adaptiveEncrypt(value, "SHAHEX:123"));
034: assertEquals(encrypted_value_md5, StringEncryptor
035: .adaptiveEncrypt(value, "MD5:123"));
036: assertEquals(encrypted_value_md5hex, StringEncryptor
037: .adaptiveEncrypt(value, "MD5HEX:123"));
038: assertEquals(encrypted_value_obf, StringEncryptor
039: .adaptiveEncrypt(value, "OBF:123"));
040: assertEquals(encrypted_value_wrp, StringEncryptor
041: .adaptiveEncrypt(value, "WRP:123"));
042: assertEquals(encrypted_value_wrphex, StringEncryptor
043: .adaptiveEncrypt(value, "WRPHEX:123"));
044: assertEquals(encrypted_value_none, StringEncryptor
045: .adaptiveEncrypt(value, "123"));
046: } catch (NoSuchAlgorithmException e) {
047: fail();
048: }
049: }
050:
051: public void testGetEncryptor() {
052: String encrypted_value_sha = "SHA:Hw/gszrpwzs0GnKTaKbwetEqBb0=";
053: String encrypted_value_shahex = "SHAHEX:1f0fe0b33ae9c33b341a729368a6f07ad12a05bd";
054: String encrypted_value_md5 = "MD5:JZ3Y+j2LYEvTmtYRY1Khsw==";
055: String encrypted_value_md5hex = "MD5HEX:259dd8fa3d8b604bd39ad6116352a1b3";
056: String encrypted_value_obf = "OBF:1w261wtu1ugo1vo41vmy1uh21wuk1w1c";
057: String encrypted_value_wrp = "WRP:XRadzcqykoN26sWuGe0j85TRoOaErinQr0+QKWsy1hlTMJdtRMkLUo+A4S/Wr3xcj+Va6jc0XLGfcWusuNMrcg==";
058: String encrypted_value_wrphex = "WRPHEX:5d169dcdcab2928376eac5ae19ed23f394d1a0e684ae29d0af4f90296b32d6195330976d44c90b528f80e12fd6af7c5c8fe55aea37345cb19f716bacb8d32b72";
059: String value = "thevalue";
060:
061: try {
062: assertEquals(encrypted_value_sha, StringEncryptor
063: .getEncryptor(StringEncryptor.IDENTIFIER_SHA)
064: .encrypt(value));
065: assertEquals(encrypted_value_shahex, StringEncryptor
066: .getEncryptor(StringEncryptor.IDENTIFIER_SHAHEX)
067: .encrypt(value));
068: assertEquals(encrypted_value_md5, StringEncryptor
069: .getEncryptor(StringEncryptor.IDENTIFIER_MD5)
070: .encrypt(value));
071: assertEquals(encrypted_value_md5hex, StringEncryptor
072: .getEncryptor(StringEncryptor.IDENTIFIER_MD5HEX)
073: .encrypt(value));
074: assertEquals(encrypted_value_obf, StringEncryptor
075: .getEncryptor(StringEncryptor.IDENTIFIER_OBF)
076: .encrypt(value));
077: assertEquals(encrypted_value_wrp, StringEncryptor
078: .getEncryptor(StringEncryptor.IDENTIFIER_WHIRLPOOL)
079: .encrypt(value));
080: assertEquals(encrypted_value_wrphex, StringEncryptor
081: .getEncryptor(
082: StringEncryptor.IDENTIFIER_WHIRLPOOLHEX)
083: .encrypt(value));
084: assertNull(StringEncryptor.getEncryptor("BLAH"));
085: } catch (NoSuchAlgorithmException e) {
086: fail();
087: }
088: }
089:
090: public void testEncryptPlain() {
091: String value = "somevalue";
092:
093: try {
094: String encrypted = StringEncryptor.autoEncrypt(value);
095: assertEquals(encrypted, value);
096: assertTrue(StringEncryptor.matches(value, encrypted));
097: } catch (NoSuchAlgorithmException e) {
098: fail();
099: }
100: }
101:
102: public void testEncryptOBF() {
103: String value = "somevalue";
104: String value2 = "somevalue";
105: String value3 = "somevalue2";
106: String value4 = "SHA:somevalue";
107:
108: try {
109: String encrypted = StringEncryptor.autoEncrypt("OBF:"
110: + value);
111: assertTrue(StringEncryptor.matches(value2, encrypted));
112: assertTrue(!StringEncryptor.matches(value3, encrypted));
113: assertTrue(!StringEncryptor.matches(value4, encrypted));
114:
115: assertEquals(StringEncryptor.deobfuscate(encrypted), value);
116: } catch (NoSuchAlgorithmException e) {
117: fail();
118: }
119: }
120:
121: public void testEncryptMD5() {
122: String value = "MD5:somevalue";
123: String value2 = "somevalue";
124: String value3 = "somevalue2";
125: String value4 = "SHA:somevalue";
126:
127: try {
128: String encrypted = StringEncryptor.autoEncrypt(value);
129: assertTrue(StringEncryptor.matches(value2, encrypted));
130: assertTrue(!StringEncryptor.matches(value3, encrypted));
131: assertTrue(!StringEncryptor.matches(value4, encrypted));
132: } catch (NoSuchAlgorithmException e) {
133: fail();
134: }
135: }
136:
137: public void testEncryptMD5HEX() {
138: String value = "MD5HEX:somevalue";
139: String value2 = "somevalue";
140: String value3 = "somevalue2";
141: String value4 = "SHA:somevalue";
142:
143: try {
144: String encrypted = StringEncryptor.autoEncrypt(value);
145: assertTrue(StringEncryptor.matches(value2, encrypted));
146: assertTrue(!StringEncryptor.matches(value3, encrypted));
147: assertTrue(!StringEncryptor.matches(value4, encrypted));
148: } catch (NoSuchAlgorithmException e) {
149: fail();
150: }
151: }
152:
153: public void testEncryptSHA() {
154: String value = "SHA:somevalue";
155: String value2 = "somevalue";
156: String value3 = "somevalue2";
157: String value4 = "MD5:somevalue";
158:
159: try {
160: String encrypted = StringEncryptor.autoEncrypt(value);
161: assertTrue(StringEncryptor.matches(value2, encrypted));
162: assertTrue(!StringEncryptor.matches(value3, encrypted));
163: assertTrue(!StringEncryptor.matches(value4, encrypted));
164: } catch (NoSuchAlgorithmException e) {
165: fail();
166: }
167: }
168:
169: public void testEncryptSHAHEX() {
170: String value = "SHAHEX:somevalue";
171: String value2 = "somevalue";
172: String value3 = "somevalue2";
173: String value4 = "MD5HEX:somevalue";
174:
175: try {
176: String encrypted = StringEncryptor.autoEncrypt(value);
177: assertTrue(StringEncryptor.matches(value2, encrypted));
178: assertTrue(!StringEncryptor.matches(value3, encrypted));
179: assertTrue(!StringEncryptor.matches(value4, encrypted));
180: } catch (NoSuchAlgorithmException e) {
181: fail();
182: }
183: }
184:
185: public void testEncryptWRP() {
186: String value = "WRP:somevalue";
187: String value2 = "somevalue";
188: String value3 = "somevalue2";
189: String value4 = "MD5:somevalue";
190:
191: try {
192: String encrypted = StringEncryptor.autoEncrypt(value);
193: assertTrue(StringEncryptor.matches(value2, encrypted));
194: assertTrue(!StringEncryptor.matches(value3, encrypted));
195: assertTrue(!StringEncryptor.matches(value4, encrypted));
196: } catch (NoSuchAlgorithmException e) {
197: fail();
198: }
199: }
200:
201: public void testEncryptWRPHEX() {
202: String value = "WRPHEX:somevalue";
203: String value2 = "somevalue";
204: String value3 = "somevalue2";
205: String value4 = "MD5HEX:somevalue";
206:
207: try {
208: String encrypted = StringEncryptor.autoEncrypt(value);
209: assertTrue(StringEncryptor.matches(value2, encrypted));
210: assertTrue(!StringEncryptor.matches(value3, encrypted));
211: assertTrue(!StringEncryptor.matches(value4, encrypted));
212: } catch (NoSuchAlgorithmException e) {
213: fail();
214: }
215: }
216: }
|