|
2 | 2 | * ==================================================================== |
3 | 3 | * |
4 | 4 | * The Apache Software License, Version 1.1 |
5 | | - * |
6 | | - * Copyright (c) 2001-2003 The Apache Software Foundation. All rights |
7 | | - * reserved. |
8 | | - * |
| 5 | + * |
| 6 | + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights reserved. |
| 7 | + * |
9 | 8 | * Redistribution and use in source and binary forms, with or without |
10 | | - * modification, are permitted provided that the following conditions |
11 | | - * are met: |
12 | | - * |
13 | | - * 1. Redistributions of source code must retain the above copyright |
14 | | - * notice, this list of conditions and the following disclaimer. |
15 | | - * |
16 | | - * 2. Redistributions in binary form must reproduce the above copyright |
17 | | - * notice, this list of conditions and the following disclaimer in |
18 | | - * the documentation and/or other materials provided with the |
19 | | - * distribution. |
20 | | - * |
21 | | - * 3. The end-user documentation included with the redistribution, |
22 | | - * if any, must include the following acknowledgement: |
23 | | - * "This product includes software developed by the |
24 | | - * Apache Software Foundation (http://www.apache.org/)." |
25 | | - * Alternately, this acknowledgement may appear in the software itself, |
26 | | - * if and wherever such third-party acknowledgements normally appear. |
27 | | - * |
28 | | - * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software |
29 | | - * Foundation" must not be used to endorse or promote products derived |
30 | | - * from this software without prior written permission. For written |
31 | | - * permission, please contact apache@apache.org. |
32 | | - * |
33 | | - * 5. Products derived from this software may not be called "Apache", |
34 | | - * "Apache" nor may "Apache" appear in their name without prior |
35 | | - * written permission of the Apache Software Foundation. |
36 | | - * |
37 | | - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
38 | | - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
39 | | - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
40 | | - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
41 | | - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
42 | | - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
43 | | - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
44 | | - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
45 | | - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
46 | | - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
47 | | - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
48 | | - * SUCH DAMAGE. |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * 3. The end-user documentation included with the redistribution, if any, |
| 16 | + * must include the following acknowledgement: "This product includes software |
| 17 | + * developed by the Apache Software Foundation (http://www.apache.org/)." |
| 18 | + * Alternately, this acknowledgement may appear in the software itself, if and |
| 19 | + * wherever such third-party acknowledgements normally appear. |
| 20 | + * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache |
| 21 | + * Software Foundation" must not be used to endorse or promote products derived |
| 22 | + * from this software without prior written permission. For written permission, |
| 23 | + * please contact apache@apache.org. |
| 24 | + * 5. Products derived from this software may not be called "Apache", "Apache" |
| 25 | + * nor may "Apache" appear in their name without prior written permission of |
| 26 | + * the Apache Software Foundation. |
| 27 | + * |
| 28 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, |
| 29 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 31 | + * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 32 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 33 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 34 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 35 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 37 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
49 | 38 | * ==================================================================== |
50 | | - * |
51 | | - * This software consists of voluntary contributions made by many |
52 | | - * individuals on behalf of the Apache Software Foundation. For more |
53 | | - * information on the Apache Software Foundation, please see |
54 | | - * <http://www.apache.org/>. |
55 | | - * |
56 | | - */ |
| 39 | + * |
| 40 | + * This software consists of voluntary contributions made by many individuals |
| 41 | + * on behalf of the Apache Software Foundation. For more information on the |
| 42 | + * Apache Software Foundation, please see <http://www.apache.org/> . |
| 43 | + * |
| 44 | + */ |
57 | 45 |
|
58 | 46 | package org.apache.commons.codec.digest; |
59 | 47 |
|
60 | 48 | import junit.framework.TestCase; |
61 | 49 |
|
62 | 50 | /** |
63 | 51 | * Tests Digest methods. |
64 | | - * |
| 52 | + * |
65 | 53 | * @author Dave Dribin |
66 | 54 | * @author David Graham |
| 55 | + * @author Gary Gregory |
67 | 56 | */ |
68 | 57 | public class DigestUtilsTest extends TestCase { |
69 | 58 |
|
70 | | - public void testMd5Hex() { |
71 | | - // Examples from RFC 1321 |
72 | | - assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex("")); |
| 59 | + public void testInternalNoSuchAlgorithmException() { |
| 60 | + try { |
| 61 | + DigestUtils.getDigest("Bogus Bogus"); |
| 62 | + fail("A RuntimeException should have been thrown."); |
| 63 | + } catch (RuntimeException e) { |
| 64 | + // Expected exception. |
| 65 | + } |
| 66 | + } |
73 | 67 |
|
74 | | - assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a")); |
| 68 | + public void testMd5Hex() { |
| 69 | + // Examples from RFC 1321 |
| 70 | + assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex("")); |
75 | 71 |
|
76 | | - assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc")); |
| 72 | + assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a")); |
77 | 73 |
|
78 | | - assertEquals( |
79 | | - "f96b697d7cb7938d525a2f31aaf161d0", |
80 | | - DigestUtils.md5Hex("message digest")); |
| 74 | + assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc")); |
81 | 75 |
|
82 | | - assertEquals( |
83 | | - "c3fcd3d76192e4007dfb496cca67e13b", |
84 | | - DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz")); |
| 76 | + assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest")); |
85 | 77 |
|
86 | | - assertEquals( |
87 | | - "d174ab98d277d9f5a5611c2c9f419d9f", |
88 | | - DigestUtils.md5Hex( |
89 | | - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
90 | | - + "abcdefghijklmnopqrstuvwxyz" |
91 | | - + "0123456789")); |
| 78 | + assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz")); |
92 | 79 |
|
93 | | - assertEquals( |
94 | | - "57edf4a22be3c955ac49da2e2107b67a", |
95 | | - DigestUtils.md5Hex( |
96 | | - "1234567890123456789012345678901234567890" |
97 | | - + "1234567890123456789012345678901234567890")); |
98 | | - } |
| 80 | + assertEquals( |
| 81 | + "d174ab98d277d9f5a5611c2c9f419d9f", |
| 82 | + DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789")); |
99 | 83 |
|
100 | | - /** |
| 84 | + assertEquals( |
| 85 | + "57edf4a22be3c955ac49da2e2107b67a", |
| 86 | + DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890")); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
101 | 90 | * An MD5 hash converted to hex should always be 32 characters. |
102 | 91 | */ |
103 | | - public void testMD5HexLength() { |
104 | | - String hashMe = "this is some string that is longer than 32 characters"; |
105 | | - String hash = DigestUtils.md5Hex(hashMe.getBytes()); |
106 | | - assertEquals(32, hash.length()); |
| 92 | + public void testMD5HexLength() { |
| 93 | + String hashMe = "this is some string that is longer than 32 characters"; |
| 94 | + String hash = DigestUtils.md5Hex(hashMe.getBytes()); |
| 95 | + assertEquals(32, hash.length()); |
107 | 96 |
|
108 | | - hashMe = "length < 32"; |
109 | | - hash = DigestUtils.md5Hex(hashMe.getBytes()); |
110 | | - assertEquals(32, hash.length()); |
111 | | - } |
| 97 | + hashMe = "length < 32"; |
| 98 | + hash = DigestUtils.md5Hex(hashMe.getBytes()); |
| 99 | + assertEquals(32, hash.length()); |
| 100 | + } |
112 | 101 |
|
113 | | - /** |
| 102 | + /** |
114 | 103 | * An MD5 hash should always be a 16 element byte[]. |
115 | 104 | */ |
116 | | - public void testMD5Length() { |
117 | | - String hashMe = "this is some string that is longer than 16 characters"; |
118 | | - byte[] hash = DigestUtils.md5(hashMe.getBytes()); |
119 | | - assertEquals(16, hash.length); |
120 | | - |
121 | | - hashMe = "length < 16"; |
122 | | - hash = DigestUtils.md5(hashMe.getBytes()); |
123 | | - assertEquals(16, hash.length); |
124 | | - } |
125 | | - |
126 | | - public void testShaHex() { |
127 | | - // Examples from FIPS 180-1 |
128 | | - assertEquals( |
129 | | - "a9993e364706816aba3e25717850c26c9cd0d89d", |
130 | | - DigestUtils.shaHex("abc")); |
| 105 | + public void testMD5Length() { |
| 106 | + String hashMe = "this is some string that is longer than 16 characters"; |
| 107 | + byte[] hash = DigestUtils.md5(hashMe.getBytes()); |
| 108 | + assertEquals(16, hash.length); |
| 109 | + |
| 110 | + hashMe = "length < 16"; |
| 111 | + hash = DigestUtils.md5(hashMe.getBytes()); |
| 112 | + assertEquals(16, hash.length); |
| 113 | + } |
| 114 | + |
| 115 | + public void testShaHex() { |
| 116 | + // Examples from FIPS 180-1 |
| 117 | + assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); |
| 118 | + |
| 119 | + assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes())); |
131 | 120 |
|
132 | 121 | assertEquals( |
133 | | - "a9993e364706816aba3e25717850c26c9cd0d89d", |
134 | | - DigestUtils.shaHex("abc".getBytes())); |
135 | | - |
136 | | - assertEquals( |
137 | | - "84983e441c3bd26ebaae4aa1f95129e5e54670f1", |
138 | | - DigestUtils.shaHex( |
139 | | - "abcdbcdecdefdefgefghfghighij" |
140 | | - + "hijkijkljklmklmnlmnomnopnopq")); |
141 | | - } |
142 | | - |
| 122 | + "84983e441c3bd26ebaae4aa1f95129e5e54670f1", |
| 123 | + DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); |
| 124 | + } |
| 125 | + |
143 | 126 | } |
0 commit comments