1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  package org.apache.commons.codec.digest;
19  
20  import junit.framework.TestCase;
21  
22  /**
23   * Tests DigestUtils methods.
24   * 
25   * @author Apache Software Foundation
26   * @version $Id: DigestUtilsTest.java 480406 2006-11-29 04:56:58Z bayard $
27   */
28  public class DigestUtilsTest extends TestCase {
29  
30      public void testInternalNoSuchAlgorithmException() {
31          try {
32              DigestUtils.getDigest("Bogus Bogus");
33              fail("A RuntimeException should have been thrown.");
34          } catch (RuntimeException e) {
35              // Expected exception.
36          }
37      }
38  
39      public void testMd5Hex() {
40          // Examples from RFC 1321
41          assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
42  
43          assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
44  
45          assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
46  
47          assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
48  
49          assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
50  
51          assertEquals(
52              "d174ab98d277d9f5a5611c2c9f419d9f",
53              DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
54  
55          assertEquals(
56              "57edf4a22be3c955ac49da2e2107b67a",
57              DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
58      }
59  
60      /**
61  	 * An MD5 hash converted to hex should always be 32 characters.
62  	 */
63      public void testMD5HexLength() {
64          String hashMe = "this is some string that is longer than 32 characters";
65          String hash = DigestUtils.md5Hex(hashMe.getBytes());
66          assertEquals(32, hash.length());
67  
68          hashMe = "length < 32";
69          hash = DigestUtils.md5Hex(hashMe.getBytes());
70          assertEquals(32, hash.length());
71      }
72  
73      /**
74  	 * An MD5 hash should always be a 16 element byte[].
75  	 */
76      public void testMD5Length() {
77          String hashMe = "this is some string that is longer than 16 characters";
78          byte[] hash = DigestUtils.md5(hashMe.getBytes());
79          assertEquals(16, hash.length);
80  
81          hashMe = "length < 16";
82          hash = DigestUtils.md5(hashMe.getBytes());
83          assertEquals(16, hash.length);
84      }
85  
86      public void testSha256() {
87  	// Examples from FIPS 180-2
88  	assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
89  		     DigestUtils.sha256Hex("abc"));
90  	assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
91  		     DigestUtils.sha256Hex("abc".getBytes()));
92  	assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
93  		     DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
94      }
95  
96      public void testSha384() {
97  	// Examples from FIPS 180-2
98  	assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + 
99  		     "8086072ba1e7cc2358baeca134c825a7",
100 		     DigestUtils.sha384Hex("abc"));
101 	assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + 
102 		     "8086072ba1e7cc2358baeca134c825a7",
103 		     DigestUtils.sha384Hex("abc".getBytes()));
104 	assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" +
105 	        "fcc7c71a557e2db966c3e9fa91746039",
106 		     DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
107 					   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
108     }    
109 
110     public void testSha512() {
111 	// Examples from FIPS 180-2
112 	assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
113 	        "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
114 		     DigestUtils.sha512Hex("abc"));
115 	assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
116 		     "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
117 		     DigestUtils.sha512Hex("abc".getBytes()));
118 	assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + 
119 		     "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
120 		     DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + 
121 					   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
122     }
123 
124     public void testShaHex() {
125         // Examples from FIPS 180-1
126         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
127 
128         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes()));
129 
130         assertEquals(
131             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
132             DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
133     }    
134 }