/* ******************************************************************
Copyright 2010 Xu Hui Hui (http://xhh.me)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************** */
package me.xhh.utils;
import java.security.NoSuchAlgorithmException;
import junit.framework.TestCase;
public class DigestTest extends TestCase {
public void testMd5() {
assertNull(Digest.md5(null));
assertEquals("d41d8cd98f00b204e9800998ecf8427e", Digest.md5(""));
assertEquals("7215ee9c7d9dc229d2921a40e899ec5f", Digest.md5(" "));// space
assertEquals("5e732a1878be2342dbfeff5fe3ca5aa3", Digest.md5("\t"));// tab
assertEquals("d8957008c0f71eaaea6d8b1dde49fd74", Digest.md5("\\t"));// two chars: \t
assertEquals("68b329da9893e34099c7d8ad5cb9c940", Digest.md5("\n"));
assertEquals("661f8009fa8e56a9d0e94a0a644397d7", Digest.md5("test-string"));
assertEquals("2dbaca18c2319d103048c20a1a0001ed", Digest.md5("How about THIS? lol ;-)"));
}
public void testSha() {
assertNull(Digest.sha(null));
assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", Digest.sha(""));
assertEquals("b858cb282617fb0956d960215c8e84d1ccf909c6", Digest.sha(" "));// space
assertEquals("ac9231da4082430afe8f4d40127814c613648d8e", Digest.sha("\t"));// tab
assertEquals("8bf81043e29dfc96a6fe1f30f7116f552de6e7d7", Digest.sha("\\t"));// two chars: \t
assertEquals("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc", Digest.sha("\n"));
assertEquals("4f49d69613b186e71104c7ca1b26c1e5b78c9193", Digest.sha("test-string"));
assertEquals("3e003e8e2af334bf01dac7527dcc05ee94b212d2", Digest.sha("How about THIS? lol ;-)"));
}
public void testDigest() throws NoSuchAlgorithmException {
String s = "testing-str";
try {
Digest.digest(s, null);
fail("Should throw an IllegalArgumentException for argument \"null\"!");
} catch (IllegalArgumentException e) {
}
try {
Digest.digest(s, "");
fail("Should throw a NoSuchAlgorithmException for argument of empty string!");
} catch (NoSuchAlgorithmException e) {
}
assertEquals(Digest.md5(s), Digest.digest(s, "MD5"));
assertEquals(Digest.sha(s), Digest.digest(s, "SHA"));
}
}