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.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assumptions.assumeTrue;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.IOException;
33  import java.io.OutputStream;
34  import java.io.RandomAccessFile;
35  import java.nio.ByteBuffer;
36  import java.nio.file.Files;
37  import java.nio.file.Path;
38  import java.nio.file.Paths;
39  import java.security.MessageDigest;
40  import java.util.Locale;
41  import java.util.Random;
42  
43  import org.apache.commons.codec.binary.Hex;
44  import org.apache.commons.codec.binary.StringUtils;
45  import org.apache.commons.io.RandomAccessFileMode;
46  import org.apache.commons.lang3.JavaVersion;
47  import org.apache.commons.lang3.SystemUtils;
48  import org.junit.jupiter.api.AfterEach;
49  import org.junit.jupiter.api.BeforeEach;
50  import org.junit.jupiter.api.Test;
51  
52  /**
53   * Tests DigestUtils methods.
54   */
55  public class DigestUtilsTest {
56  
57      private static final String EMPTY_STRING = "";
58  
59      private final byte[] testData = new byte[DigestUtils.BUFFER_SIZE * DigestUtils.BUFFER_SIZE];
60  
61      private Path testFile;
62  
63      private Path testRandomAccessFile;
64  
65      private RandomAccessFile testRandomAccessFileWrapper;
66  
67      private void assumeJava8() {
68          assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
69      }
70  
71      private void assumeJava9() {
72          assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
73      }
74  
75      byte[] getTestData() {
76          return testData;
77      }
78  
79      Path getTestPath() {
80          return testFile;
81      }
82  
83      RandomAccessFile getTestRandomAccessFile() {
84          return testRandomAccessFileWrapper;
85      }
86  
87      @BeforeEach
88      public void setUp() throws Exception {
89          new Random().nextBytes(testData);
90          testFile = Files.createTempFile(DigestUtilsTest.class.getName(), ".dat");
91          try (final OutputStream fos = Files.newOutputStream(testFile)) {
92              fos.write(testData);
93          }
94  
95          testRandomAccessFile = Files.createTempFile(DigestUtilsTest.class.getName(), ".dat");
96          try (final OutputStream fos = Files.newOutputStream(testRandomAccessFile)) {
97              fos.write(testData);
98          }
99          testRandomAccessFileWrapper = RandomAccessFileMode.READ_WRITE.create(testRandomAccessFile);
100     }
101 
102     @AfterEach
103     public void tearDown() throws IOException {
104         if (testRandomAccessFileWrapper != null) {
105             testRandomAccessFileWrapper.close();
106         }
107         Files.deleteIfExists(testFile);
108         Files.deleteIfExists(testRandomAccessFile);
109     }
110 
111     @Test
112     public void testDigestAs() throws IOException {
113         final String expected = "d41d8cd98f00b204e9800998ecf8427e";
114         final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
115         final String algo = MessageDigestAlgorithms.MD5;
116         assertEquals(expected, new DigestUtils(algo).digestAsHex(new File(pathName)));
117         try (final FileInputStream inputStream = new FileInputStream(pathName)) {
118             assertEquals(expected, new DigestUtils(algo).digestAsHex(inputStream));
119         }
120         final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
121         assertEquals(expected, new DigestUtils(algo).digestAsHex(allBytes));
122         assertEquals(expected, new DigestUtils(algo).digestAsHex(ByteBuffer.wrap(allBytes)));
123     }
124 
125     @Test
126     public void testGetMessageDigest() {
127         final DigestUtils digestUtils = new DigestUtils(MessageDigestAlgorithms.MD5);
128         assertNotNull(digestUtils.getMessageDigest());
129         assertEquals(MessageDigestAlgorithms.MD5, digestUtils.getMessageDigest().getAlgorithm());
130     }
131 
132     @Test
133     public void testInternalNoSuchAlgorithmException() {
134         assertThrows(IllegalArgumentException.class, () -> DigestUtils.getDigest("Bogus Bogus"));
135     }
136 
137     @Test
138     public void testIsAvailable() {
139         assertTrue(DigestUtils.isAvailable(MessageDigestAlgorithms.MD5));
140         assertFalse(DigestUtils.isAvailable("FOO"));
141         assertFalse(DigestUtils.isAvailable(null));
142     }
143 
144     @Test
145     public void testMd2Hex() throws IOException {
146         // Examples from RFC 1319
147         assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex(EMPTY_STRING));
148 
149         assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a"));
150 
151         assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc"));
152 
153         assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest"));
154 
155         assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz"));
156 
157         assertEquals(
158             "da33def2a42df13975352846c30338cd",
159             DigestUtils.md2Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
160 
161         assertEquals(
162             "d5976f79d83d3a0dc9806c3c66f3efd8",
163             DigestUtils.md2Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
164 
165         assertEquals(DigestUtils.md2Hex(testData),
166                 DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
167 }
168 
169     /**
170      * An MD2 hash converted to hexadecimal should always be 32 characters.
171      */
172     @Test
173     public void testMd2HexLength() {
174         String hashMe = "this is some string that is longer than 32 characters";
175         String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
176         assertEquals(32, hash.length());
177 
178         hashMe = "length < 32";
179         hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
180         assertEquals(32, hash.length());
181     }
182 
183     /**
184      * An MD2 hash should always be a 16 element byte[].
185      */
186     @Test
187     public void testMd2Length() {
188         String hashMe = "this is some string that is longer than 16 characters";
189         byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe));
190         assertEquals(16, hash.length);
191 
192         hashMe = "length < 16";
193         hash = DigestUtils.md2(getBytesUtf8(hashMe));
194         assertEquals(16, hash.length);
195     }
196 
197     @Test
198     public void testMd5Hex() throws IOException {
199         // Examples from RFC 1321
200         assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(EMPTY_STRING));
201 
202         assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
203 
204         assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
205 
206         assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
207 
208         assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
209 
210         assertEquals(
211             "d174ab98d277d9f5a5611c2c9f419d9f",
212             DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"));
213 
214         assertEquals(
215             "57edf4a22be3c955ac49da2e2107b67a",
216             DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
217 
218         assertEquals(DigestUtils.md5Hex(testData),
219                 DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
220     }
221 
222     /**
223      * An MD5 hash converted to hexadecimal should always be 32 characters.
224      */
225     @Test
226     public void testMd5HexLengthForBytes() {
227         String hashMe = "this is some string that is longer than 32 characters";
228         String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
229         assertEquals(32, hash.length());
230 
231         hashMe = "length < 32";
232         hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
233         assertEquals(32, hash.length());
234     }
235 
236     /**
237      * An MD5 hash should always be a 16 element byte[].
238      */
239     @Test
240     public void testMd5LengthForBytes() {
241         String hashMe = "this is some string that is longer than 16 characters";
242         byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe));
243         assertEquals(16, hash.length);
244 
245         hashMe = "length < 16";
246         hash = DigestUtils.md5(getBytesUtf8(hashMe));
247         assertEquals(16, hash.length);
248     }
249 
250     @Test
251     public void testSha1Hex() throws IOException {
252         // Examples from FIPS 180-1
253         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc"));
254 
255         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc")));
256 
257         assertEquals(
258             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
259             DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
260         assertEquals(DigestUtils.sha1Hex(testData),
261                 DigestUtils.sha1Hex(new ByteArrayInputStream(testData)));
262     }
263 
264     @Test
265     public void testSha1UpdateWithByteArray(){
266         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
267         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
268 
269         MessageDigest messageDigest = DigestUtils.getSha1Digest();
270         messageDigest.update(d1.getBytes());
271         messageDigest.update(d2.getBytes());
272         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
273 
274         messageDigest = DigestUtils.getSha1Digest();
275         DigestUtils.updateDigest(messageDigest, d1.getBytes());
276         DigestUtils.updateDigest(messageDigest, d2.getBytes());
277         final String actualResult = Hex.encodeHexString(messageDigest.digest());
278 
279         assertEquals(expectedResult, actualResult);
280     }
281 
282     @Test
283     public void testSha1UpdateWithByteBuffer(){
284         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
285         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
286 
287         MessageDigest messageDigest = DigestUtils.getSha1Digest();
288         messageDigest.update(d1.getBytes());
289         messageDigest.update(d2.getBytes());
290         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
291 
292         messageDigest = DigestUtils.getSha1Digest();
293         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d1.getBytes()));
294         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d2.getBytes()));
295         final String actualResult = Hex.encodeHexString(messageDigest.digest());
296 
297         assertEquals(expectedResult, actualResult);
298     }
299 
300     @Test
301     public void testSha1UpdateWithString(){
302         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
303         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
304 
305         MessageDigest messageDigest = DigestUtils.getSha1Digest();
306         messageDigest.update(StringUtils.getBytesUtf8(d1));
307         messageDigest.update(StringUtils.getBytesUtf8(d2));
308         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
309 
310         messageDigest = DigestUtils.getSha1Digest();
311         DigestUtils.updateDigest(messageDigest, d1);
312         DigestUtils.updateDigest(messageDigest, d2);
313         final String actualResult = Hex.encodeHexString(messageDigest.digest());
314 
315         assertEquals(expectedResult, actualResult);
316     }
317 
318     @Test
319     public void testSha224_FileAsHex() throws IOException {
320         assumeJava8();
321         final String expected = "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
322         final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
323         final String algo = MessageDigestAlgorithms.SHA_224;
324         final DigestUtils digestUtils = new DigestUtils(algo);
325         assertEquals(expected, digestUtils.digestAsHex(new File(pathName)));
326         try (final FileInputStream inputStream = new FileInputStream(pathName)) {
327             assertEquals(expected, digestUtils.digestAsHex(inputStream));
328         }
329         final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
330         assertEquals(expected, digestUtils.digestAsHex(allBytes));
331         assertEquals(expected, digestUtils.digestAsHex(ByteBuffer.wrap(allBytes)));
332     }
333 
334     @Test
335     public void testSha224_PathAsHex() throws IOException {
336         assumeJava8();
337         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
338                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(Paths.get("src/test/resources/org/apache/commons/codec/empty.bin")));
339     }
340 
341     @Test
342     public void testSha224_StringAsHex() {
343         assumeJava8();
344         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
345                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(EMPTY_STRING));
346         assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
347                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex("The quick brown fox jumps over the lazy dog"));
348 
349         // Examples from FIPS 180-4?
350     }
351 
352     @Test
353     public void testSha256() throws IOException {
354     // Examples from FIPS 180-2
355     assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
356              DigestUtils.sha256Hex("abc"));
357     assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
358              DigestUtils.sha256Hex(getBytesUtf8("abc")));
359     assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
360              DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
361 
362     assertEquals(DigestUtils.sha256Hex(testData),
363             DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
364     }
365 
366     @Test
367     public void testSha256HexInputStream() throws IOException {
368         assertEquals(DigestUtils.sha256Hex(testData),
369                 DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
370     }
371 
372     @Test
373     public void testSha3_224() {
374         assumeJava9();
375         // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
376         //
377         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-224_Msg0.pdf
378         assertEquals(
379                 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7",
380                 DigestUtils.sha3_224Hex(EMPTY_STRING));
381     }
382 
383     @Test
384     public void testSha3_224HexInputStream() throws IOException {
385         assumeJava9();
386         assertEquals(DigestUtils.sha3_224Hex(testData),
387                 DigestUtils.sha3_224Hex(new ByteArrayInputStream(testData)));
388     }
389 
390     @Test
391     public void testSha3_256() {
392         assumeJava9();
393         // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
394         //
395         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
396         assertEquals(
397                 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
398                 DigestUtils.sha3_256Hex(EMPTY_STRING));
399     }
400 
401     @Test
402     public void testSha3_256HexInputStream() throws IOException {
403         assumeJava9();
404         assertEquals(DigestUtils.sha3_256Hex(testData),
405                 DigestUtils.sha3_256Hex(new ByteArrayInputStream(testData)));
406     }
407 
408     @Test
409     public void testSha3_384() {
410         assumeJava9();
411         // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
412         //
413         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
414         assertEquals(
415                 "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004",
416                 DigestUtils.sha3_384Hex(EMPTY_STRING));
417     }
418 
419     @Test
420     public void testSha3_384HexInputStream() throws IOException {
421         assumeJava9();
422         assertEquals(DigestUtils.sha3_384Hex(testData),
423                 DigestUtils.sha3_384Hex(new ByteArrayInputStream(testData)));
424     }
425 
426     @Test
427     public void testSha3_512() {
428         assumeJava9();
429         // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
430         //
431         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
432         assertEquals(
433                 "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
434                 DigestUtils.sha3_512Hex(EMPTY_STRING));
435     }
436 
437     @Test
438     public void testSha3_512HexInputStream() throws IOException {
439         assumeJava9();
440         assertEquals(DigestUtils.sha3_512Hex(testData),
441                 DigestUtils.sha3_512Hex(new ByteArrayInputStream(testData)));
442     }
443 
444     @Test
445     public void testSha384() throws IOException {
446     // Examples from FIPS 180-2
447     assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" +
448              "8086072ba1e7cc2358baeca134c825a7",
449              DigestUtils.sha384Hex("abc"));
450     assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" +
451              "8086072ba1e7cc2358baeca134c825a7",
452              DigestUtils.sha384Hex(getBytesUtf8("abc")));
453     assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" +
454             "fcc7c71a557e2db966c3e9fa91746039",
455              DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
456                        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
457     assertEquals(DigestUtils.sha384Hex(testData),
458             DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
459     }
460 
461     @Test
462     public void testSha384HexInputStream() throws IOException {
463         assertEquals(DigestUtils.sha384Hex(testData),
464                 DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
465     }
466 
467     @Test
468     public void testSha512() {
469     // Examples from FIPS 180-2
470     assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
471             "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
472              DigestUtils.sha512Hex("abc"));
473     assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
474              "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
475              DigestUtils.sha512Hex(getBytesUtf8("abc")));
476     assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
477              "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
478              DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
479                        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
480     }
481 
482     @Test
483     public void testSha512_224() throws Exception {
484         assumeJava9();
485         // Examples from
486         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
487         final String stringInput = "abc";
488         final byte[] bytesInput = getBytesUtf8(stringInput);
489         final String resultString = "4634270F707B6A54DAAE7530460842E20E37ED265CEEE9A43E8924AA".toLowerCase(Locale.ROOT);
490         final byte[] resultBytes = Hex.decodeHex(resultString);
491         //
492         assertArrayEquals(resultBytes, DigestUtils.sha512_224(bytesInput));
493         assertArrayEquals(resultBytes, DigestUtils.sha512_224(new ByteArrayInputStream(bytesInput)));
494         assertArrayEquals(resultBytes, DigestUtils.sha512_224(stringInput));
495         //
496         assertEquals(resultString, DigestUtils.sha512_224Hex(bytesInput));
497         assertEquals(resultString, DigestUtils.sha512_224Hex(new ByteArrayInputStream(bytesInput)));
498         assertEquals(resultString, DigestUtils.sha512_224Hex(stringInput));
499         // Example 2
500         assertEquals("23FEC5BB94D60B23308192640B0C453335D664734FE40E7268674AF9".toLowerCase(Locale.ROOT),
501             DigestUtils.sha512_224Hex(
502                 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
503     }
504 
505     @Test
506     public void testSha512_256() throws Exception {
507         assumeJava9();
508         // Examples from
509         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
510         final String stringInput = "abc";
511         final byte[] bytesInput = getBytesUtf8(stringInput);
512         final String resultString = "53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23"
513             .toLowerCase(Locale.ROOT);
514         final byte[] resultBytes = Hex.decodeHex(resultString);
515         //
516         assertArrayEquals(resultBytes, DigestUtils.sha512_256(bytesInput));
517         assertArrayEquals(resultBytes, DigestUtils.sha512_256(new ByteArrayInputStream(bytesInput)));
518         assertArrayEquals(resultBytes, DigestUtils.sha512_256(stringInput));
519         //
520         assertEquals(resultString, DigestUtils.sha512_256Hex(bytesInput));
521         assertEquals(resultString, DigestUtils.sha512_256Hex(new ByteArrayInputStream(bytesInput)));
522         assertEquals(resultString, DigestUtils.sha512_256Hex(stringInput));
523         // Example 2
524         assertEquals("3928E184FB8690F840DA3988121D31BE65CB9D3EF83EE6146FEAC861E19B563A".toLowerCase(Locale.ROOT),
525             DigestUtils.sha512_256Hex(
526                 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
527     }
528 
529     @Test
530     public void testSha512HexInputStream() throws IOException {
531         assertEquals(DigestUtils.sha512Hex(testData),
532                 DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
533     }
534 
535     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
536     @Test
537     public void testShaHex() throws IOException {
538         // Examples from FIPS 180-1
539         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
540 
541         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc")));
542 
543         assertEquals(
544             "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
545             DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
546         assertEquals(DigestUtils.shaHex(testData),
547                 DigestUtils.shaHex(new ByteArrayInputStream(testData)));
548     }
549 
550     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
551     @Test
552     public void testShaUpdateWithByteArray(){
553         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
554         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
555 
556         MessageDigest messageDigest = DigestUtils.getShaDigest();
557         messageDigest.update(d1.getBytes());
558         messageDigest.update(d2.getBytes());
559         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
560 
561         messageDigest = DigestUtils.getShaDigest();
562         DigestUtils.updateDigest(messageDigest, d1.getBytes());
563         DigestUtils.updateDigest(messageDigest, d2.getBytes());
564         final String actualResult = Hex.encodeHexString(messageDigest.digest());
565 
566         assertEquals(expectedResult, actualResult);
567     }
568 
569     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
570     @Test
571     public void testShaUpdateWithString(){
572         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
573         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
574 
575         MessageDigest messageDigest = DigestUtils.getShaDigest();
576         messageDigest.update(StringUtils.getBytesUtf8(d1));
577         messageDigest.update(StringUtils.getBytesUtf8(d2));
578         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
579 
580         messageDigest = DigestUtils.getShaDigest();
581         DigestUtils.updateDigest(messageDigest, d1);
582         DigestUtils.updateDigest(messageDigest, d2);
583         final String actualResult = Hex.encodeHexString(messageDigest.digest());
584 
585         assertEquals(expectedResult, actualResult);
586     }
587 
588 }