View Javadoc
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 static org.apache.commons.codec.binary.StringUtils.getBytesUtf8;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.nio.ByteBuffer;
28  import java.security.MessageDigest;
29  import java.util.Random;
30  
31  import org.apache.commons.codec.binary.Hex;
32  import org.apache.commons.codec.binary.StringUtils;
33  import org.apache.commons.lang3.JavaVersion;
34  import org.apache.commons.lang3.SystemUtils;
35  import org.junit.After;
36  import org.junit.Assume;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  /**
41   * Tests DigestUtils methods.
42   *
43   * @version $Id: DigestUtilsTest.java 1811343 2017-10-06 15:19:35Z ggregory $
44   */
45  public class DigestUtilsTest {
46  
47      private final byte[] testData = new byte[1024 * 1024];
48  
49      private File testFile;
50  
51      private void assumeJava8() {
52          Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
53      }
54  
55      byte[] getTestData() {
56          return testData;
57      }
58  
59      File getTestFile() {
60          return testFile;
61      }
62  
63      @Before
64      public void setUp() throws Exception {
65          new Random().nextBytes(testData);
66          testFile = File.createTempFile(DigestUtilsTest.class.getName(), ".dat");
67          final FileOutputStream fos = new FileOutputStream(testFile);
68          fos.write(testData);
69          fos.close();
70      }
71  
72      @After
73      public void tearDown() {
74          if (!testFile.delete()) {
75              testFile.deleteOnExit();
76          }
77      }
78  
79      @Test(expected=IllegalArgumentException.class)
80      public void testInternalNoSuchAlgorithmException() {
81          DigestUtils.getDigest("Bogus Bogus");
82      }
83  
84      @Test
85      public void testMd2Hex() throws IOException {
86          // Examples from RFC 1319
87          assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex(""));
88  
89          assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a"));
90  
91          assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc"));
92  
93          assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest"));
94  
95          assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz"));
96  
97          assertEquals(
98              "da33def2a42df13975352846c30338cd",
99              DigestUtils.md2Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
100 
101         assertEquals(
102             "d5976f79d83d3a0dc9806c3c66f3efd8",
103             DigestUtils.md2Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
104 
105         assertEquals(DigestUtils.md2Hex(testData),
106                 DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
107 }
108 
109     /**
110      * An MD2 hash converted to hex should always be 32 characters.
111      */
112     @Test
113     public void testMd2HexLength() {
114         String hashMe = "this is some string that is longer than 32 characters";
115         String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
116         assertEquals(32, hash.length());
117 
118         hashMe = "length < 32";
119         hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
120         assertEquals(32, hash.length());
121     }
122 
123     /**
124      * An MD2 hash should always be a 16 element byte[].
125      */
126     @Test
127     public void testMd2Length() {
128         String hashMe = "this is some string that is longer than 16 characters";
129         byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe));
130         assertEquals(16, hash.length);
131 
132         hashMe = "length < 16";
133         hash = DigestUtils.md2(getBytesUtf8(hashMe));
134         assertEquals(16, hash.length);
135     }
136 
137     @Test
138     public void testMd5Hex() throws IOException {
139         // Examples from RFC 1321
140         assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
141 
142         assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
143 
144         assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
145 
146         assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
147 
148         assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
149 
150         assertEquals(
151             "d174ab98d277d9f5a5611c2c9f419d9f",
152             DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
153 
154         assertEquals(
155             "57edf4a22be3c955ac49da2e2107b67a",
156             DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
157 
158         assertEquals(DigestUtils.md5Hex(testData),
159                 DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
160 }
161 
162     /**
163      * An MD5 hash converted to hex should always be 32 characters.
164      */
165     @Test
166     public void testMd5HexLengthForBytes() {
167         String hashMe = "this is some string that is longer than 32 characters";
168         String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
169         assertEquals(32, hash.length());
170 
171         hashMe = "length < 32";
172         hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
173         assertEquals(32, hash.length());
174     }
175 
176     /**
177      * An MD5 hash should always be a 16 element byte[].
178      */
179     @Test
180     public void testMd5LengthForBytes() {
181         String hashMe = "this is some string that is longer than 16 characters";
182         byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe));
183         assertEquals(16, hash.length);
184 
185         hashMe = "length < 16";
186         hash = DigestUtils.md5(getBytesUtf8(hashMe));
187         assertEquals(16, hash.length);
188     }
189 
190     @Test
191     public void testSha1Hex() throws IOException {
192         // Examples from FIPS 180-1
193         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc"));
194 
195         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc")));
196 
197         assertEquals(
198             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
199             DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
200         assertEquals(DigestUtils.sha1Hex(testData),
201                 DigestUtils.sha1Hex(new ByteArrayInputStream(testData)));
202     }
203 
204     @Test
205     public void testSha1UpdateWithByteArray(){
206         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
207         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
208 
209         MessageDigest messageDigest = DigestUtils.getSha1Digest();
210         messageDigest.update(d1.getBytes());
211         messageDigest.update(d2.getBytes());
212         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
213 
214         messageDigest = DigestUtils.getSha1Digest();
215         DigestUtils.updateDigest(messageDigest, d1.getBytes());
216         DigestUtils.updateDigest(messageDigest, d2.getBytes());
217         final String actualResult = Hex.encodeHexString(messageDigest.digest());
218 
219         assertEquals(expectedResult, actualResult);
220     }
221 
222     @Test
223     public void testSha1UpdateWithByteBuffer(){
224         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
225         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
226 
227         MessageDigest messageDigest = DigestUtils.getSha1Digest();
228         messageDigest.update(d1.getBytes());
229         messageDigest.update(d2.getBytes());
230         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
231 
232         messageDigest = DigestUtils.getSha1Digest();
233         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d1.getBytes()));
234         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d2.getBytes()));
235         final String actualResult = Hex.encodeHexString(messageDigest.digest());
236 
237         assertEquals(expectedResult, actualResult);
238     }
239 
240     @Test
241     public void testSha1UpdateWithString(){
242         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
243         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
244 
245         MessageDigest messageDigest = DigestUtils.getSha1Digest();
246         messageDigest.update(StringUtils.getBytesUtf8(d1));
247         messageDigest.update(StringUtils.getBytesUtf8(d2));
248         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
249 
250         messageDigest = DigestUtils.getSha1Digest();
251         DigestUtils.updateDigest(messageDigest, d1);
252         DigestUtils.updateDigest(messageDigest, d2);
253         final String actualResult = Hex.encodeHexString(messageDigest.digest());
254 
255         assertEquals(expectedResult, actualResult);
256     }
257 
258     @Test
259     public void testSha224() throws IOException {
260         assumeJava8();
261         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
262                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(("")));
263         assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
264                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex("The quick brown fox jumps over the lazy dog"));
265 
266         // Examples from FIPS 180-4?
267     }
268 
269     @Test
270     public void testSha256() throws IOException {
271     // Examples from FIPS 180-2
272     assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
273              DigestUtils.sha256Hex("abc"));
274     assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
275              DigestUtils.sha256Hex(getBytesUtf8("abc")));
276     assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
277              DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
278 
279     assertEquals(DigestUtils.sha256Hex(testData),
280             DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
281     }
282 
283     @Test
284     public void testSha384() throws IOException {
285     // Examples from FIPS 180-2
286     assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" +
287              "8086072ba1e7cc2358baeca134c825a7",
288              DigestUtils.sha384Hex("abc"));
289     assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" +
290              "8086072ba1e7cc2358baeca134c825a7",
291              DigestUtils.sha384Hex(getBytesUtf8("abc")));
292     assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" +
293             "fcc7c71a557e2db966c3e9fa91746039",
294              DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
295                        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
296     assertEquals(DigestUtils.sha384Hex(testData),
297             DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
298     }
299 
300     @Test
301     public void testSha512() throws IOException {
302     // Examples from FIPS 180-2
303     assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
304             "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
305              DigestUtils.sha512Hex("abc"));
306     assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
307              "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
308              DigestUtils.sha512Hex(getBytesUtf8("abc")));
309     assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
310              "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
311              DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
312                        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
313     }
314 
315     @Test
316     public void testSha512HexInputStream() throws IOException {
317         assertEquals(DigestUtils.sha512Hex(testData),
318                 DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
319     }
320 
321     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
322     @Test
323     public void testShaHex() throws IOException {
324         // Examples from FIPS 180-1
325         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
326 
327         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc")));
328 
329         assertEquals(
330             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
331             DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
332         assertEquals(DigestUtils.shaHex(testData),
333                 DigestUtils.shaHex(new ByteArrayInputStream(testData)));
334     }
335 
336     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
337     @Test
338     public void testShaUpdateWithByteArray(){
339         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
340         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
341 
342         MessageDigest messageDigest = DigestUtils.getShaDigest();
343         messageDigest.update(d1.getBytes());
344         messageDigest.update(d2.getBytes());
345         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
346 
347         messageDigest = DigestUtils.getShaDigest();
348         DigestUtils.updateDigest(messageDigest, d1.getBytes());
349         DigestUtils.updateDigest(messageDigest, d2.getBytes());
350         final String actualResult = Hex.encodeHexString(messageDigest.digest());
351 
352         assertEquals(expectedResult, actualResult);
353     }
354 
355     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
356     @Test
357     public void testShaUpdateWithString(){
358         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
359         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
360 
361         MessageDigest messageDigest = DigestUtils.getShaDigest();
362         messageDigest.update(StringUtils.getBytesUtf8(d1));
363         messageDigest.update(StringUtils.getBytesUtf8(d2));
364         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
365 
366         messageDigest = DigestUtils.getShaDigest();
367         DigestUtils.updateDigest(messageDigest, d1);
368         DigestUtils.updateDigest(messageDigest, d2);
369         final String actualResult = Hex.encodeHexString(messageDigest.digest());
370 
371         assertEquals(expectedResult, actualResult);
372     }
373 
374 }