KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > digest > DigestUtilsTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.digest;
18
19 import junit.framework.TestCase;
20
21 /**
22  * Tests Digest methods.
23  *
24  * @author Apache Software Foundation
25  */

26 public class DigestUtilsTest extends TestCase {
27
28     public void testInternalNoSuchAlgorithmException() {
29         try {
30             DigestUtils.getDigest("Bogus Bogus");
31             fail("A RuntimeException should have been thrown.");
32         } catch (RuntimeException JavaDoc e) {
33             // Expected exception.
34
}
35     }
36
37     public void testMd5Hex() {
38         // Examples from RFC 1321
39
assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
40
41         assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
42
43         assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
44
45         assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
46
47         assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
48
49         assertEquals(
50             "d174ab98d277d9f5a5611c2c9f419d9f",
51             DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
52
53         assertEquals(
54             "57edf4a22be3c955ac49da2e2107b67a",
55             DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
56     }
57
58     /**
59      * An MD5 hash converted to hex should always be 32 characters.
60      */

61     public void testMD5HexLength() {
62         String JavaDoc hashMe = "this is some string that is longer than 32 characters";
63         String JavaDoc hash = DigestUtils.md5Hex(hashMe.getBytes());
64         assertEquals(32, hash.length());
65
66         hashMe = "length < 32";
67         hash = DigestUtils.md5Hex(hashMe.getBytes());
68         assertEquals(32, hash.length());
69     }
70
71     /**
72      * An MD5 hash should always be a 16 element byte[].
73      */

74     public void testMD5Length() {
75         String JavaDoc hashMe = "this is some string that is longer than 16 characters";
76         byte[] hash = DigestUtils.md5(hashMe.getBytes());
77         assertEquals(16, hash.length);
78
79         hashMe = "length < 16";
80         hash = DigestUtils.md5(hashMe.getBytes());
81         assertEquals(16, hash.length);
82     }
83
84     public void testShaHex() {
85         // Examples from FIPS 180-1
86
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
87
88         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes()));
89
90         assertEquals(
91             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
92             DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
93     }
94
95 }
96
Popular Tags