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