001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.digest;
018:
019: import junit.framework.TestCase;
020:
021: /**
022: * Tests Digest methods.
023: *
024: * @author Apache Software Foundation
025: */
026: public class DigestUtilsTest extends TestCase {
027:
028: public void testInternalNoSuchAlgorithmException() {
029: try {
030: DigestUtils.getDigest("Bogus Bogus");
031: fail("A RuntimeException should have been thrown.");
032: } catch (RuntimeException e) {
033: // Expected exception.
034: }
035: }
036:
037: public void testMd5Hex() {
038: // Examples from RFC 1321
039: assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils
040: .md5Hex(""));
041:
042: assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils
043: .md5Hex("a"));
044:
045: assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils
046: .md5Hex("abc"));
047:
048: assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils
049: .md5Hex("message digest"));
050:
051: assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils
052: .md5Hex("abcdefghijklmnopqrstuvwxyz"));
053:
054: assertEquals("d174ab98d277d9f5a5611c2c9f419d9f", DigestUtils
055: .md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
056: + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
057:
058: assertEquals("57edf4a22be3c955ac49da2e2107b67a", DigestUtils
059: .md5Hex("1234567890123456789012345678901234567890"
060: + "1234567890123456789012345678901234567890"));
061: }
062:
063: /**
064: * An MD5 hash converted to hex should always be 32 characters.
065: */
066: public void testMD5HexLength() {
067: String hashMe = "this is some string that is longer than 32 characters";
068: String hash = DigestUtils.md5Hex(hashMe.getBytes());
069: assertEquals(32, hash.length());
070:
071: hashMe = "length < 32";
072: hash = DigestUtils.md5Hex(hashMe.getBytes());
073: assertEquals(32, hash.length());
074: }
075:
076: /**
077: * An MD5 hash should always be a 16 element byte[].
078: */
079: public void testMD5Length() {
080: String hashMe = "this is some string that is longer than 16 characters";
081: byte[] hash = DigestUtils.md5(hashMe.getBytes());
082: assertEquals(16, hash.length);
083:
084: hashMe = "length < 16";
085: hash = DigestUtils.md5(hashMe.getBytes());
086: assertEquals(16, hash.length);
087: }
088:
089: public void testShaHex() {
090: // Examples from FIPS 180-1
091: assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d",
092: DigestUtils.shaHex("abc"));
093:
094: assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d",
095: DigestUtils.shaHex("abc".getBytes()));
096:
097: assertEquals("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
098: DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij"
099: + "hijkijkljklmklmnlmnomnopnopq"));
100: }
101:
102: }
|