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.assertNull;
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
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.Arrays;
40  import java.util.Locale;
41  import java.util.Random;
42  import java.util.stream.Stream;
43  
44  import org.apache.commons.codec.binary.Hex;
45  import org.apache.commons.io.RandomAccessFileMode;
46  import org.apache.commons.lang3.JavaVersion;
47  import org.apache.commons.lang3.StringUtils;
48  import org.apache.commons.lang3.SystemUtils;
49  import org.junit.jupiter.api.AfterEach;
50  import org.junit.jupiter.api.BeforeEach;
51  import org.junit.jupiter.api.Test;
52  import org.junit.jupiter.params.ParameterizedTest;
53  import org.junit.jupiter.params.provider.Arguments;
54  import org.junit.jupiter.params.provider.MethodSource;
55  
56  /**
57   * Tests {@link DigestUtils}.
58   */
59  class DigestUtilsTest {
60  
61      private static final int SHAKE128_256_BYTE_LEN = 256 / Byte.SIZE;
62      private static final int SHAKE128_512_BYTE_LEN = 512 / Byte.SIZE;
63      private static final String EMPTY_STRING = "";
64  
65      /**
66       * Tests SHAKE128 sample of 0-bit message.
67       *
68       * See https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
69       *
70       * See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
71       */
72      // @formatter:off
73      // The formatting below is matches the PDF.
74      private static final String SHAKE128_MSG_0 =
75              "7F 9C 2B A4 E8 8F 82 7D 61 60 45 50 76 05 85 3E" +
76              "D7 3B 80 93 F6 EF BC 88 EB 1A 6E AC FA 66 EF 26" +
77              "3C B1 EE A9 88 00 4B 93 10 3C FB 0A EE FD 2A 68" +
78              "6E 01 FA 4A 58 E8 A3 63 9C A8 A1 E3 F9 AE 57 E2" +
79              "35 B8 CC 87 3C 23 DC 62 B8 D2 60 16 9A FA 2F 75" +
80              "AB 91 6A 58 D9 74 91 88 35 D2 5E 6A 43 50 85 B2" +
81              "BA DF D6 DF AA C3 59 A5 EF BB 7B CC 4B 59 D5 38" +
82              "DF 9A 04 30 2E 10 C8 BC 1C BF 1A 0B 3A 51 20 EA" +
83              "17 CD A7 CF AD 76 5F 56 23 47 4D 36 8C CC A8 AF" +
84              "00 07 CD 9F 5E 4C 84 9F 16 7A 58 0B 14 AA BD EF" +
85              "AE E7 EE F4 7C B0 FC A9 76 7B E1 FD A6 94 19 DF" +
86              "B9 27 E9 DF 07 34 8B 19 66 91 AB AE B5 80 B3 2D" +
87              "EF 58 53 8B 8D 23 F8 77 32 EA 63 B0 2B 4F A0 F4" +
88              "87 33 60 E2 84 19 28 CD 60 DD 4C EE 8C C0 D4 C9" +
89              "22 A9 61 88 D0 32 67 5C 8A C8 50 93 3C 7A FF 15" +
90              "33 B9 4C 83 4A DB B6 9C 61 15 BA D4 69 2D 86 19" +
91              "F9 0B 0C DF 8A 7B 9C 26 40 29 AC 18 5B 70 B8 3F" +
92              "28 01 F2 F4 B3 F7 0C 59 3E A3 AE EB 61 3A 7F 1B" +
93              "1D E3 3F D7 50 81 F5 92 30 5F 2E 45 26 ED C0 96" +
94              "31 B1 09 58 F4 64 D8 89 F3 1B A0 10 25 0F DA 7F" +
95              "13 68 EC 29 67 FC 84 EF 2A E9 AF F2 68 E0 B1 70" +
96              "0A FF C6 82 0B 52 3A 3D 91 71 35 F2 DF F2 EE 06" +
97              "BF E7 2B 31 24 72 1D 4A 26 C0 4E 53 A7 5E 30 E7" +
98              "3A 7A 9C 4A 95 D9 1C 55 D4 95 E9 F5 1D D0 B5 E9" +
99              "D8 3C 6D 5E 8C E8 03 AA 62 B8 D6 54 DB 53 D0 9B" +
100             "8D CF F2 73 CD FE B5 73 FA D8 BC D4 55 78 BE C2" +
101             "E7 70 D0 1E FD E8 6E 72 1A 3F 7C 6C CE 27 5D AB" +
102             "E6 E2 14 3F 1A F1 8D A7 EF DD C4 C7 B7 0B 5E 34" +
103             "5D B9 3C C9 36 BE A3 23 49 1C CB 38 A3 88 F5 46" +
104             "A9 FF 00 DD 4E 13 00 B9 B2 15 3D 20 41 D2 05 B4" +
105             "43 E4 1B 45 A6 53 F2 A5 C4 49 2C 1A DD 54 45 12" +
106             "DD A2 52 98 33 46 2B 71 A4 1A 45 BE 97 29 0B 6F";
107     // @formatter:on
108 
109     /**
110      * Tests SHAKE256 sample of 0-bit message.
111      *
112      * See https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
113      *
114      * See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
115      */
116     // @formatter:off
117     // The formatting below is matches the PDF.
118     private static final String SHAKE256_MSG_0 =
119             "46 B9 DD 2B 0B A8 8D 13 23 3B 3F EB 74 3E EB 24" +
120             "3F CD 52 EA 62 B8 1B 82 B5 0C 27 64 6E D5 76 2F" +
121             "D7 5D C4 DD D8 C0 F2 00 CB 05 01 9D 67 B5 92 F6" +
122             "FC 82 1C 49 47 9A B4 86 40 29 2E AC B3 B7 C4 BE" +
123             "14 1E 96 61 6F B1 39 57 69 2C C7 ED D0 B4 5A E3" +
124             "DC 07 22 3C 8E 92 93 7B EF 84 BC 0E AB 86 28 53" +
125             "34 9E C7 55 46 F5 8F B7 C2 77 5C 38 46 2C 50 10" +
126             "D8 46 C1 85 C1 51 11 E5 95 52 2A 6B CD 16 CF 86" +
127             "F3 D1 22 10 9E 3B 1F DD 94 3B 6A EC 46 8A 2D 62" +
128             "1A 7C 06 C6 A9 57 C6 2B 54 DA FC 3B E8 75 67 D6" +
129             "77 23 13 95 F6 14 72 93 B6 8C EA B7 A9 E0 C5 8D" +
130             "86 4E 8E FD E4 E1 B9 A4 6C BE 85 47 13 67 2F 5C" +
131             "AA AE 31 4E D9 08 3D AB 4B 09 9F 8E 30 0F 01 B8" +
132             "65 0F 1F 4B 1D 8F CF 3F 3C B5 3F B8 E9 EB 2E A2" +
133             "03 BD C9 70 F5 0A E5 54 28 A9 1F 7F 53 AC 26 6B" +
134             "28 41 9C 37 78 A1 5F D2 48 D3 39 ED E7 85 FB 7F" +
135             "5A 1A AA 96 D3 13 EA CC 89 09 36 C1 73 CD CD 0F" +
136             "AB 88 2C 45 75 5F EB 3A ED 96 D4 77 FF 96 39 0B" +
137             "F9 A6 6D 13 68 B2 08 E2 1F 7C 10 D0 4A 3D BD 4E" +
138             "36 06 33 E5 DB 4B 60 26 01 C1 4C EA 73 7D B3 DC" +
139             "F7 22 63 2C C7 78 51 CB DD E2 AA F0 A3 3A 07 B3" +
140             "73 44 5D F4 90 CC 8F C1 E4 16 0F F1 18 37 8F 11" +
141             "F0 47 7D E0 55 A8 1A 9E DA 57 A4 A2 CF B0 C8 39" +
142             "29 D3 10 91 2F 72 9E C6 CF A3 6C 6A C6 A7 58 37" +
143             "14 30 45 D7 91 CC 85 EF F5 B2 19 32 F2 38 61 BC" +
144             "F2 3A 52 B5 DA 67 EA F7 BA AE 0F 5F B1 36 9D B7" +
145             "8F 3A C4 5F 8C 4A C5 67 1D 85 73 5C DD DB 09 D2" +
146             "B1 E3 4A 1F C0 66 FF 4A 16 2C B2 63 D6 54 12 74" +
147             "AE 2F CC 86 5F 61 8A BE 27 C1 24 CD 8B 07 4C CD" +
148             "51 63 01 B9 18 75 82 4D 09 95 8F 34 1E F2 74 BD" +
149             "AB 0B AE 31 63 39 89 43 04 E3 58 77 B0 C2 8A 9B" +
150             "1F D1 66 C7 96 B9 CC 25 8A 06 4A 8F 57 E2 7F 2A";
151     // @formatter:on
152 
153     /**
154      * Tests SHAKE128 sample of 1600-bit message.
155      *
156      * See https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
157      *
158      * See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg1600.pdf
159      */
160     // @formatter:off
161     // The formatting below is matches the PDF.
162     private static final String SHAKE128_MSG_1600 =
163             "13 1A B8 D2 B5 94 94 6B 9C 81 33 3F 9B B6 E0 CE" +
164             "75 C3 B9 31 04 FA 34 69 D3 91 74 57 38 5D A0 37" +
165             "CF 23 2E F7 16 4A 6D 1E B4 48 C8 90 81 86 AD 85" +
166             "2D 3F 85 A5 CF 28 DA 1A B6 FE 34 38 17 19 78 46" +
167             "7F 1C 05 D5 8C 7E F3 8C 28 4C 41 F6 C2 22 1A 76" +
168             "F1 2A B1 C0 40 82 66 02 50 80 22 94 FB 87 18 02" +
169             "13 FD EF 5B 0E CB 7D F5 0C A1 F8 55 5B E1 4D 32" +
170             "E1 0F 6E DC DE 89 2C 09 42 4B 29 F5 97 AF C2 70" +
171             "C9 04 55 6B FC B4 7A 7D 40 77 8D 39 09 23 64 2B" +
172             "3C BD 05 79 E6 09 08 D5 A0 00 C1 D0 8B 98 EF 93" +
173             "3F 80 64 45 BF 87 F8 B0 09 BA 9E 94 F7 26 61 22" +
174             "ED 7A C2 4E 5E 26 6C 42 A8 2F A1 BB EF B7 B8 DB" +
175             "00 66 E1 6A 85 E0 49 3F 07 DF 48 09 AE C0 84 A5" +
176             "93 74 8A C3 DD E5 A6 D7 AA E1 E8 B6 E5 35 2B 2D" +
177             "71 EF BB 47 D4 CA EE D5 E6 D6 33 80 5D 2D 32 3E" +
178             "6F D8 1B 46 84 B9 3A 26 77 D4 5E 74 21 C2 C6 AE" +
179             "A2 59 B8 55 A6 98 FD 7D 13 47 7A 1F E5 3E 5A 4A" +
180             "61 97 DB EC 5C E9 5F 50 5B 52 0B CD 95 70 C4 A8" +
181             "26 5A 7E 01 F8 9C 0C 00 2C 59 BF EC 6C D4 A5 C1" +
182             "09 25 89 53 EE 5E E7 0C D5 77 EE 21 7A F2 1F A7" +
183             "01 78 F0 94 6C 9B F6 CA 87 51 79 34 79 F6 B5 37" +
184             "73 7E 40 B6 ED 28 51 1D 8A 2D 7E 73 EB 75 F8 DA" +
185             "AC 91 2F F9 06 E0 AB 95 5B 08 3B AC 45 A8 E5 E9" +
186             "B7 44 C8 50 6F 37 E9 B4 E7 49 A1 84 B3 0F 43 EB" +
187             "18 8D 85 5F 1B 70 D7 1F F3 E5 0C 53 7A C1 B0 F8" +
188             "97 4F 0F E1 A6 AD 29 5B A4 2F 6A EC 74 D1 23 A7" +
189             "AB ED DE 6E 2C 07 11 CA B3 6B E5 AC B1 A5 A1 1A" +
190             "4B 1D B0 8B A6 98 2E FC CD 71 69 29 A7 74 1C FC" +
191             "63 AA 44 35 E0 B6 9A 90 63 E8 80 79 5C 3D C5 EF" +
192             "32 72 E1 1C 49 7A 91 AC F6 99 FE FE E2 06 22 7A" +
193             "44 C9 FB 35 9F D5 6A C0 A9 A7 5A 74 3C FF 68 62" +
194             "F1 7D 72 59 AB 07 52 16 C0 69 95 11 64 3B 64 39";
195     // @formatter:on
196 
197     /**
198      * Tests SHAKE256 sample of 1600-bit message.
199      *
200      * See https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
201      *
202      * See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg1600.pdf
203      */
204     // @formatter:off
205     // The formatting below is matches the PDF.
206     private static final String SHAKE256_MSG_1600 =
207             "CD 8A 92 0E D1 41 AA 04 07 A2 2D 59 28 86 52 E9" +
208             "D9 F1 A7 EE 0C 1E 7C 1C A6 99 42 4D A8 4A 90 4D" +
209             "2D 70 0C AA E7 39 6E CE 96 60 44 40 57 7D A4 F3" +
210             "AA 22 AE B8 85 7F 96 1C 4C D8 E0 6F 0A E6 61 0B" +
211             "10 48 A7 F6 4E 10 74 CD 62 9E 85 AD 75 66 04 8E" +
212             "FC 4F B5 00 B4 86 A3 30 9A 8F 26 72 4C 0E D6 28" +
213             "00 1A 10 99 42 24 68 DE 72 6F 10 61 D9 9E B9 E9" +
214             "36 04 D5 AA 74 67 D4 B1 BD 64 84 58 2A 38 43 17" +
215             "D7 F4 7D 75 0B 8F 54 99 51 2B B8 5A 22 6C 42 43" +
216             "55 6E 69 6F 6B D0 72 C5 AA 2D 9B 69 73 02 44 B5" +
217             "68 53 D1 69 70 AD 81 7E 21 3E 47 06 18 17 80 01" +
218             "C9 FB 56 C5 4F EF A5 FE E6 7D 2D A5 24 BB 3B 0B" +
219             "61 EF 0E 91 14 A9 2C DB B6 CC CB 98 61 5C FE 76" +
220             "E3 51 0D D8 8D 1C C2 8F F9 92 87 51 2F 24 BF AF" +
221             "A1 A7 68 77 B6 F3 71 98 E3 A6 41 C6 8A 7C 42 D4" +
222             "5F A7 AC C1 0D AE 5F 3C EF B7 B7 35 F1 2D 4E 58" +
223             "9F 7A 45 6E 78 C0 F5 E4 C4 47 1F FF A5 E4 FA 05" +
224             "14 AE 97 4D 8C 26 48 51 3B 5D B4 94 CE A8 47 15" +
225             "6D 27 7A D0 E1 41 C2 4C 78 39 06 4C D0 88 51 BC" +
226             "2E 7C A1 09 FD 4E 25 1C 35 BB 0A 04 FB 05 B3 64" +
227             "FF 8C 4D 8B 59 BC 30 3E 25 32 8C 09 A8 82 E9 52" +
228             "51 8E 1A 8A E0 FF 26 5D 61 C4 65 89 69 73 D7 49" +
229             "04 99 DC 63 9F B8 50 2B 39 45 67 91 B1 B6 EC 5B" +
230             "CC 5D 9A C3 6A 6D F6 22 A0 70 D4 3F ED 78 1F 5F" +
231             "14 9F 7B 62 67 5E 7D 1A 4D 6D EC 48 C1 C7 16 45" +
232             "86 EA E0 6A 51 20 8C 0B 79 12 44 D3 07 72 65 05" +
233             "C3 AD 4B 26 B6 82 23 77 25 7A A1 52 03 75 60 A7" +
234             "39 71 4A 3C A7 9B D6 05 54 7C 9B 78 DD 1F 59 6F" +
235             "2D 4F 17 91 BC 68 9A 0E 9B 79 9A 37 33 9C 04 27" +
236             "57 33 74 01 43 EF 5D 2B 58 B9 6A 36 3D 4E 08 07" +
237             "6A 1A 9D 78 46 43 6E 4D CA 57 28 B6 F7 60 EE F0" +
238             "CA 92 BF 0B E5 61 5E 96 95 9D 76 71 97 A0 BE EB";
239     // @formatter:on
240 
241     static Stream<Arguments> testShake128_256() {
242         // @formatter:off
243         return Stream.of(
244                 // empty
245                 Arguments.of("", 0, SHAKE128_MSG_0),
246                 Arguments.of(
247                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
248                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
249                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
250                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
251                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
252                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
253                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
254                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
255                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
256                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
257                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
258                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
259                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
260                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
261                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
262                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
263                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
264                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
265                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
266                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
267                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
268                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
269                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
270                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
271                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
272                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
273                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
274                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
275                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
276                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
277                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
278                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
279                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
280                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
281                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
282                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
283                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
284                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
285                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
286                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
287                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
288                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
289                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
290                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
291                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
292                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
293                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
294                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
295                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
296                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1", 1_600, SHAKE128_MSG_1600));
297         // @formatter:on
298     }
299 
300     static Stream<Arguments> testShake256_512() {
301         // @formatter:off
302         return Stream.of(
303                 // empty
304                 Arguments.of("", 0, SHAKE256_MSG_0),
305                 Arguments.of(
306                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
307                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
308                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
309                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
310                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
311                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
312                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
313                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
314                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
315                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
316                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
317                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
318                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
319                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
320                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
321                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
322                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
323                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
324                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
325                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
326                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
327                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
328                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
329                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
330                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
331                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
332                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
333                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
334                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
335                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
336                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
337                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
338                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
339                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
340                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
341                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
342                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
343                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
344                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
345                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
346                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
347                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
348                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
349                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
350                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
351                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
352                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
353                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
354                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1" +
355                         "1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1", 1_600, SHAKE256_MSG_1600));
356         // @formatter:on
357     }
358 
359     private final byte[] testData = new byte[DigestUtils.BUFFER_SIZE * DigestUtils.BUFFER_SIZE];
360     private Path testFile;
361     private Path testRandomAccessFile;
362     private RandomAccessFile testRandomAccessFileWrapper;
363 
364     private String clean(final String input) {
365         return StringUtils.replaceChars(input, " ", "").toLowerCase(Locale.ROOT);
366     }
367 
368     byte[] getTestData() {
369         return testData;
370     }
371 
372     Path getTestPath() {
373         return testFile;
374     }
375 
376     RandomAccessFile getTestRandomAccessFile() {
377         return testRandomAccessFileWrapper;
378     }
379 
380     private boolean isJava25Plus() {
381         return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_25);
382     }
383 
384     private boolean isJava9Plus() {
385         return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9);
386     }
387 
388     @BeforeEach
389     void setUp() throws Exception {
390         new Random().nextBytes(testData);
391         testFile = Files.createTempFile(DigestUtilsTest.class.getName(), ".dat");
392         try (OutputStream fos = Files.newOutputStream(testFile)) {
393             fos.write(testData);
394         }
395         testRandomAccessFile = Files.createTempFile(DigestUtilsTest.class.getName(), ".dat");
396         try (OutputStream fos = Files.newOutputStream(testRandomAccessFile)) {
397             fos.write(testData);
398         }
399         testRandomAccessFileWrapper = RandomAccessFileMode.READ_WRITE.create(testRandomAccessFile);
400     }
401 
402     @AfterEach
403     void tearDown() throws IOException {
404         if (testRandomAccessFileWrapper != null) {
405             testRandomAccessFileWrapper.close();
406         }
407         Files.deleteIfExists(testFile);
408         Files.deleteIfExists(testRandomAccessFile);
409     }
410 
411     @SuppressWarnings("deprecation")
412     @Test
413     void testDigestDeprecated() {
414         assertNull(new DigestUtils().getMessageDigest());
415     }
416 
417     @Test
418     void testDigestFileAs() throws IOException {
419         final String expected = "d41d8cd98f00b204e9800998ecf8427e";
420         final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
421         final String algo = MessageDigestAlgorithms.MD5;
422         final Path path = Paths.get(pathName);
423         assertEquals(expected, new DigestUtils(algo).digestAsHex(path.toFile()));
424         try (InputStream inputStream = Files.newInputStream(path)) {
425             assertEquals(expected, new DigestUtils(algo).digestAsHex(inputStream));
426         }
427         final byte[] allBytes = Files.readAllBytes(path);
428         assertEquals(expected, new DigestUtils(algo).digestAsHex(allBytes));
429         assertEquals(expected, new DigestUtils(algo).digestAsHex(ByteBuffer.wrap(allBytes)));
430     }
431 
432     @Test
433     void testGetDigests() {
434         assertEquals("MD2", DigestUtils.getMd2Digest().getAlgorithm());
435         assertEquals("MD5", DigestUtils.getMd5Digest().getAlgorithm());
436         assertEquals("SHA-1", DigestUtils.getSha1Digest().getAlgorithm());
437         assertEquals("SHA-256", DigestUtils.getSha256Digest().getAlgorithm());
438         assertEquals("SHA-384", DigestUtils.getSha384Digest().getAlgorithm());
439         assertEquals("SHA-512/224", DigestUtils.getSha512_224Digest().getAlgorithm());
440         assertEquals("SHA-512/256", DigestUtils.getSha512_256Digest().getAlgorithm());
441         assertEquals("SHA-512", DigestUtils.getSha512Digest().getAlgorithm());
442         assertEquals("SHA-1", DigestUtils.getShaDigest().getAlgorithm());
443     }
444 
445     @Test
446     void testGetDigestsJava25() {
447         if (isJava25Plus()) {
448             assertEquals("SHAKE128-256", DigestUtils.getShake128_256Digest().getAlgorithm());
449             assertEquals("SHAKE256-512", DigestUtils.getShake256_512Digest().getAlgorithm());
450         } else {
451             assertThrows(IllegalArgumentException.class, DigestUtils::getShake128_256Digest);
452             assertThrows(IllegalArgumentException.class, DigestUtils::getShake256_512Digest);
453         }
454     }
455 
456     @Test
457     void testGetDigestsJava9() {
458         if (isJava9Plus()) {
459             assertEquals("SHA3-224", DigestUtils.getSha3_224Digest().getAlgorithm());
460             assertEquals("SHA3-256", DigestUtils.getSha3_256Digest().getAlgorithm());
461             assertEquals("SHA3-384", DigestUtils.getSha3_384Digest().getAlgorithm());
462             assertEquals("SHA3-512", DigestUtils.getSha3_512Digest().getAlgorithm());
463         } else {
464             assertThrows(IllegalArgumentException.class, DigestUtils::getSha3_224Digest);
465             assertThrows(IllegalArgumentException.class, DigestUtils::getSha3_256Digest);
466             assertThrows(IllegalArgumentException.class, DigestUtils::getSha3_384Digest);
467             assertThrows(IllegalArgumentException.class, DigestUtils::getSha3_512Digest);
468         }
469     }
470 
471     @Test
472     void testGetMessageDigest() {
473         final DigestUtils digestUtils = new DigestUtils(MessageDigestAlgorithms.MD5);
474         assertNotNull(digestUtils.getMessageDigest());
475         assertEquals(MessageDigestAlgorithms.MD5, digestUtils.getMessageDigest().getAlgorithm());
476     }
477 
478     @Test
479     void testInternalNoSuchAlgorithmException() {
480         assertThrows(IllegalArgumentException.class, () -> DigestUtils.getDigest("Bogus Bogus"));
481     }
482 
483     @Test
484     void testIsAvailable() {
485         assertTrue(DigestUtils.isAvailable(MessageDigestAlgorithms.MD5));
486         assertFalse(DigestUtils.isAvailable("FOO"));
487         assertFalse(DigestUtils.isAvailable(null));
488     }
489 
490     @Test
491     void testMd2Hex() throws IOException {
492         // Examples from RFC 1319
493         assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex(EMPTY_STRING));
494         assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a"));
495         assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc"));
496         assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest"));
497         assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz"));
498         // @formatter:off
499         assertEquals("da33def2a42df13975352846c30338cd", DigestUtils.md2Hex(
500                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
501                 "abcdefghijklmnopqrstuvwxyz" +
502                 "0123456789"));
503         assertEquals("d5976f79d83d3a0dc9806c3c66f3efd8",
504                 DigestUtils.md2Hex(
505                         "1234567890123456789012345678901234567890" +
506                         "1234567890123456789012345678901234567890"));
507         // @formatter:on
508         assertEquals(DigestUtils.md2Hex(testData), DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
509     }
510 
511     /**
512      * An MD2 hash converted to hexadecimal should always be 32 characters.
513      */
514     @Test
515     void testMd2HexLength() {
516         String hashMe = "this is some string that is longer than 32 characters";
517         String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
518         assertEquals(32, hash.length());
519         hashMe = "length < 32";
520         hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
521         assertEquals(32, hash.length());
522     }
523 
524     /**
525      * An MD2 hash should always be a 16 element byte[].
526      */
527     @Test
528     void testMd2Length() {
529         String hashMe = "this is some string that is longer than 16 characters";
530         byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe));
531         assertEquals(16, hash.length);
532         hashMe = "length < 16";
533         hash = DigestUtils.md2(getBytesUtf8(hashMe));
534         assertEquals(16, hash.length);
535     }
536 
537     @Test
538     void testMd5Hex() throws IOException {
539         // Examples from RFC 1321
540         assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(EMPTY_STRING));
541         assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a"));
542         assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc"));
543         assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest"));
544         assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz"));
545         // @formatter:off
546         assertEquals("d174ab98d277d9f5a5611c2c9f419d9f", DigestUtils.md5Hex(
547               "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
548               "abcdefghijklmnopqrstuvwxyz" +
549               "0123456789"));
550 
551         assertEquals("57edf4a22be3c955ac49da2e2107b67a",
552                 DigestUtils.md5Hex(
553                       "1234567890123456789012345678901234567890" +
554                       "1234567890123456789012345678901234567890"));
555         // @formatter:on
556         assertEquals(DigestUtils.md5Hex(testData), DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
557     }
558 
559     /**
560      * An MD5 hash converted to hexadecimal should always be 32 characters.
561      */
562     @Test
563     void testMd5HexLengthForBytes() {
564         String hashMe = "this is some string that is longer than 32 characters";
565         String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
566         assertEquals(32, hash.length());
567         hashMe = "length < 32";
568         hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
569         assertEquals(32, hash.length());
570     }
571 
572     /**
573      * An MD5 hash should always be a 16 element byte[].
574      */
575     @Test
576     void testMd5LengthForBytes() {
577         String hashMe = "this is some string that is longer than 16 characters";
578         byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe));
579         assertEquals(16, hash.length);
580         hashMe = "length < 16";
581         hash = DigestUtils.md5(getBytesUtf8(hashMe));
582         assertEquals(16, hash.length);
583     }
584 
585     @Test
586     void testSha1Hex() throws IOException {
587         // Examples from FIPS 180-1
588         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc"));
589         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc")));
590         assertEquals("84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
591         assertEquals(DigestUtils.sha1Hex(testData), DigestUtils.sha1Hex(new ByteArrayInputStream(testData)));
592     }
593 
594     @Test
595     void testSha1UpdateWithByteArray() {
596         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
597         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
598         MessageDigest messageDigest = DigestUtils.getSha1Digest();
599         messageDigest.update(d1.getBytes());
600         messageDigest.update(d2.getBytes());
601         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
602         messageDigest = DigestUtils.getSha1Digest();
603         DigestUtils.updateDigest(messageDigest, d1.getBytes());
604         DigestUtils.updateDigest(messageDigest, d2.getBytes());
605         final String actualResult = Hex.encodeHexString(messageDigest.digest());
606         assertEquals(expectedResult, actualResult);
607     }
608 
609     @Test
610     void testSha1UpdateWithByteBuffer() {
611         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
612         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
613         MessageDigest messageDigest = DigestUtils.getSha1Digest();
614         messageDigest.update(d1.getBytes());
615         messageDigest.update(d2.getBytes());
616         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
617         messageDigest = DigestUtils.getSha1Digest();
618         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d1.getBytes()));
619         DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d2.getBytes()));
620         final String actualResult = Hex.encodeHexString(messageDigest.digest());
621         assertEquals(expectedResult, actualResult);
622     }
623 
624     @Test
625     void testSha1UpdateWithString() {
626         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
627         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
628         MessageDigest messageDigest = DigestUtils.getSha1Digest();
629         messageDigest.update(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(d1));
630         messageDigest.update(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(d2));
631         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
632         messageDigest = DigestUtils.getSha1Digest();
633         DigestUtils.updateDigest(messageDigest, d1);
634         DigestUtils.updateDigest(messageDigest, d2);
635         final String actualResult = Hex.encodeHexString(messageDigest.digest());
636         assertEquals(expectedResult, actualResult);
637     }
638 
639     @Test
640     void testSha224_FileAsHex() throws IOException {
641         final String expected = "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
642         final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
643         final String algo = MessageDigestAlgorithms.SHA_224;
644         final DigestUtils digestUtils = new DigestUtils(algo);
645         final Path path = Paths.get(pathName);
646         assertEquals(expected, digestUtils.digestAsHex(path.toFile()));
647         try (InputStream inputStream = Files.newInputStream(path)) {
648             assertEquals(expected, digestUtils.digestAsHex(inputStream));
649         }
650         final byte[] allBytes = Files.readAllBytes(path);
651         assertEquals(expected, digestUtils.digestAsHex(allBytes));
652         assertEquals(expected, digestUtils.digestAsHex(ByteBuffer.wrap(allBytes)));
653     }
654 
655     @Test
656     void testSha224_PathAsHex() throws IOException {
657         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
658                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(Paths.get("src/test/resources/org/apache/commons/codec/empty.bin")));
659     }
660 
661     @Test
662     void testSha224_StringAsHex() {
663         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(EMPTY_STRING));
664         assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
665                 new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex("The quick brown fox jumps over the lazy dog"));
666         // Examples from FIPS 180-4?
667     }
668 
669     @Test
670     void testSha256() throws IOException {
671         // Examples from FIPS 180-2
672         assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex("abc"));
673         assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex(getBytesUtf8("abc")));
674         assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
675                 DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
676         assertEquals(DigestUtils.sha256Hex(testData), DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
677     }
678 
679     @Test
680     void testSha256HexInputStream() throws IOException {
681         assertEquals(DigestUtils.sha256Hex(testData), DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
682     }
683 
684     @Test
685     void testSha3_224() {
686         if (isJava9Plus()) {
687             // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
688             //
689             // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-224_Msg0.pdf
690             assertEquals("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", DigestUtils.sha3_224Hex(EMPTY_STRING));
691         } else {
692             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_224Hex(EMPTY_STRING));
693         }
694     }
695 
696     @Test
697     void testSha3_224HexInputStream() throws IOException {
698         if (isJava9Plus()) {
699             assertEquals(DigestUtils.sha3_224Hex(testData), DigestUtils.sha3_224Hex(new ByteArrayInputStream(testData)));
700         } else {
701             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_224Hex(new ByteArrayInputStream(testData)));
702         }
703     }
704 
705     @Test
706     void testSha3_256() {
707         if (isJava9Plus()) {
708             // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
709             //
710             // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
711             assertEquals("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", DigestUtils.sha3_256Hex(EMPTY_STRING));
712         } else {
713             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_256Hex(EMPTY_STRING));
714         }
715     }
716 
717     @Test
718     void testSha3_256HexInputStream() throws IOException {
719         if (isJava9Plus()) {
720             assertEquals(DigestUtils.sha3_256Hex(testData), DigestUtils.sha3_256Hex(new ByteArrayInputStream(testData)));
721         } else {
722             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_256Hex(new ByteArrayInputStream(testData)));
723         }
724     }
725 
726     @Test
727     void testSha3_384() {
728         if (isJava9Plus()) {
729             // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
730             //
731             // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
732             assertEquals("0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004",
733                     DigestUtils.sha3_384Hex(EMPTY_STRING));
734         } else {
735             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_384Hex(EMPTY_STRING));
736         }
737     }
738 
739     @Test
740     void testSha3_384HexInputStream() throws IOException {
741         if (isJava9Plus()) {
742             assertEquals(DigestUtils.sha3_384Hex(testData), DigestUtils.sha3_384Hex(new ByteArrayInputStream(testData)));
743         } else {
744             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_384Hex(new ByteArrayInputStream(testData)));
745         }
746     }
747 
748     @Test
749     void testSha3_512() {
750         if (isJava9Plus()) {
751             // Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
752             //
753             // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
754             assertEquals("a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
755                     DigestUtils.sha3_512Hex(EMPTY_STRING));
756         } else {
757             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_512Hex(EMPTY_STRING));
758         }
759     }
760 
761     @Test
762     void testSha3_512HexInputStream() throws IOException {
763         if (isJava9Plus()) {
764             assertEquals(DigestUtils.sha3_512Hex(testData), DigestUtils.sha3_512Hex(new ByteArrayInputStream(testData)));
765         } else {
766             assertThrows(IllegalArgumentException.class, () -> DigestUtils.sha3_512Hex(new ByteArrayInputStream(testData)));
767         }
768     }
769 
770     @Test
771     void testSha384() throws IOException {
772         // Examples from FIPS 180-2
773         assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", DigestUtils.sha384Hex("abc"));
774         assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
775                 DigestUtils.sha384Hex(getBytesUtf8("abc")));
776         assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039",
777                 DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
778         assertEquals(DigestUtils.sha384Hex(testData), DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
779     }
780 
781     @Test
782     void testSha384HexInputStream() throws IOException {
783         assertEquals(DigestUtils.sha384Hex(testData), DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
784     }
785 
786     @Test
787     void testSha512() {
788         // Examples from FIPS 180-2
789         assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
790                 DigestUtils.sha512Hex("abc"));
791         assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
792                 DigestUtils.sha512Hex(getBytesUtf8("abc")));
793         assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
794                 DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
795     }
796 
797     @Test
798     void testSha512_224() throws Exception {
799         // Examples from
800         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
801         final String stringInput = "abc";
802         final byte[] bytesInput = getBytesUtf8(stringInput);
803         final String resultString = "4634270F707B6A54DAAE7530460842E20E37ED265CEEE9A43E8924AA".toLowerCase(Locale.ROOT);
804         final byte[] resultBytes = Hex.decodeHex(resultString);
805         assertArrayEquals(resultBytes, DigestUtils.sha512_224(bytesInput));
806         assertArrayEquals(resultBytes, DigestUtils.sha512_224(new ByteArrayInputStream(bytesInput)));
807         assertArrayEquals(resultBytes, DigestUtils.sha512_224(stringInput));
808         //
809         assertEquals(resultString, DigestUtils.sha512_224Hex(bytesInput));
810         assertEquals(resultString, DigestUtils.sha512_224Hex(new ByteArrayInputStream(bytesInput)));
811         assertEquals(resultString, DigestUtils.sha512_224Hex(stringInput));
812         // Example 2
813         assertEquals("23FEC5BB94D60B23308192640B0C453335D664734FE40E7268674AF9".toLowerCase(Locale.ROOT),
814                 DigestUtils.sha512_224Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
815     }
816 
817     @Test
818     void testSha512_256() throws Exception {
819         // Examples from
820         // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
821         final String stringInput = "abc";
822         final byte[] bytesInput = getBytesUtf8(stringInput);
823         final String resultString = "53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23".toLowerCase(Locale.ROOT);
824         final byte[] resultBytes = Hex.decodeHex(resultString);
825         assertArrayEquals(resultBytes, DigestUtils.sha512_256(bytesInput));
826         assertArrayEquals(resultBytes, DigestUtils.sha512_256(new ByteArrayInputStream(bytesInput)));
827         assertArrayEquals(resultBytes, DigestUtils.sha512_256(stringInput));
828         //
829         assertEquals(resultString, DigestUtils.sha512_256Hex(bytesInput));
830         assertEquals(resultString, DigestUtils.sha512_256Hex(new ByteArrayInputStream(bytesInput)));
831         assertEquals(resultString, DigestUtils.sha512_256Hex(stringInput));
832         // Example 2
833         assertEquals("3928E184FB8690F840DA3988121D31BE65CB9D3EF83EE6146FEAC861E19B563A".toLowerCase(Locale.ROOT),
834                 DigestUtils.sha512_256Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
835     }
836 
837     @Test
838     void testSha512HexInputStream() throws IOException {
839         assertEquals(DigestUtils.sha512Hex(testData), DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
840     }
841 
842     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
843     @Test
844     void testShaHex() throws IOException {
845         // Examples from FIPS 180-1
846         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
847         assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc")));
848         assertEquals("84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.shaHex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
849         assertEquals(DigestUtils.shaHex(testData), DigestUtils.shaHex(new ByteArrayInputStream(testData)));
850     }
851 
852     @ParameterizedTest
853     @MethodSource
854     void testShake128_256(final String binaryInputText, final int inputBitSize, final String hexOutputText) throws Exception {
855         final String cleanBinary = clean(binaryInputText);
856         final String cleanBinaryInput = StringUtils.reverse(cleanBinary);
857         final byte[] bytesInput = Binary.toByteArray(cleanBinaryInput);
858         // sanity check on the test fixture
859         assertEquals(inputBitSize, bytesInput.length * Byte.SIZE);
860         final String resultString = clean(hexOutputText).substring(0, SHAKE128_256_BYTE_LEN * 2);
861         final byte[] resultBytes = Arrays.copyOf(Hex.decodeHex(resultString), SHAKE128_256_BYTE_LEN);
862         if (isJava25Plus()) {
863             // byte[]
864             assertArrayEquals(resultBytes, DigestUtils.shake128_256(bytesInput));
865             assertArrayEquals(resultBytes, DigestUtils.shake128_256(new ByteArrayInputStream(bytesInput)));
866             // Hex string
867             assertEquals(resultString, DigestUtils.shake128_256Hex(bytesInput));
868             assertEquals(resultString, DigestUtils.shake128_256Hex(new ByteArrayInputStream(bytesInput)));
869         } else {
870             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256(bytesInput));
871             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256(new ByteArrayInputStream(bytesInput)));
872             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256Hex(bytesInput));
873             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256Hex(new ByteArrayInputStream(bytesInput)));
874         }
875     }
876 
877     @Test
878     void testShake128_256_String() throws Exception {
879         if (isJava25Plus()) {
880             // String
881             assertArrayEquals(new byte[] { 88, -127, 9, 45, -40, 24, -65, 92, -8, -93, -35, -73, -109, -5, -53, -89, 64, -105, -43, -59, 38, -90, -45, 95, -105,
882                     -72, 51, 81, -108, 15, 44, -56 }, DigestUtils.shake128_256("abc"));
883             // Hex string
884             assertEquals("5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8", DigestUtils.shake128_256Hex("abc"));
885         } else {
886             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256("abc"));
887             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake128_256Hex("abc"));
888         }
889     }
890 
891     @ParameterizedTest
892     @MethodSource
893     void testShake256_512(final String binaryInputText, final int inputBitSize, final String hexOutputText) throws Exception {
894         final String cleanBinary = clean(binaryInputText);
895         final String cleanBinaryInput = StringUtils.reverse(cleanBinary);
896         final byte[] bytesInput = Binary.toByteArray(cleanBinaryInput);
897         // sanity check on the test fixture
898         assertEquals(inputBitSize, bytesInput.length * Byte.SIZE);
899         final String resultString = clean(hexOutputText).substring(0, SHAKE128_512_BYTE_LEN * 2);
900         final byte[] resultBytes = Arrays.copyOf(Hex.decodeHex(resultString), SHAKE128_512_BYTE_LEN);
901         if (isJava25Plus()) {
902             // byte[]
903             assertArrayEquals(resultBytes, DigestUtils.shake256_512(bytesInput));
904             assertArrayEquals(resultBytes, DigestUtils.shake256_512(new ByteArrayInputStream(bytesInput)));
905             // Hex string
906             assertEquals(resultString, DigestUtils.shake256_512Hex(bytesInput));
907             assertEquals(resultString, DigestUtils.shake256_512Hex(new ByteArrayInputStream(bytesInput)));
908         } else {
909             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake256_512(bytesInput));
910             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake256_512(new ByteArrayInputStream(bytesInput)));
911             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake256_512Hex(bytesInput));
912             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake256_512Hex(new ByteArrayInputStream(bytesInput)));
913         }
914     }
915 
916     @Test
917     void testShake256_512_String() throws Exception {
918         if (isJava25Plus()) {
919             // String
920             assertArrayEquals(new byte[] { 72, 51, 102, 96, 19, 96, -88, 119, 28, 104, 99, 8, 12, -60, 17, 77, -115, -76, 69, 48, -8, -15, -31, -18, 79, -108,
921                     -22, 55, -25, -117, 87, 57, -43, -95, 91, -17, 24, 106, 83, -122, -57, 87, 68, -64, 82, 126, 31, -86, -97, -121, 38, -28, 98, -95, 42, 79,
922                     -21, 6, -67, -120, 1, -25, 81, -28 }, DigestUtils.shake256_512("abc"));
923             // Hex string
924             assertEquals("483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739d5a15bef186a5386c75744c0527e1faa9f8726e462a12a4feb06bd8801e751e4",
925                     DigestUtils.shake256_512Hex("abc"));
926         } else {
927             assertThrows(IllegalArgumentException.class, () -> DigestUtils.shake256_512Hex("abc"));
928         }
929     }
930 
931     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
932     @Test
933     void testShaUpdateWithByteArray() {
934         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
935         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
936         MessageDigest messageDigest = DigestUtils.getShaDigest();
937         messageDigest.update(d1.getBytes());
938         messageDigest.update(d2.getBytes());
939         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
940         messageDigest = DigestUtils.getShaDigest();
941         DigestUtils.updateDigest(messageDigest, d1.getBytes());
942         DigestUtils.updateDigest(messageDigest, d2.getBytes());
943         final String actualResult = Hex.encodeHexString(messageDigest.digest());
944         assertEquals(expectedResult, actualResult);
945     }
946 
947     @SuppressWarnings("deprecation") // deliberate tests of deprecated code
948     @Test
949     void testShaUpdateWithString() {
950         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
951         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'";
952         MessageDigest messageDigest = DigestUtils.getShaDigest();
953         messageDigest.update(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(d1));
954         messageDigest.update(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(d2));
955         final String expectedResult = Hex.encodeHexString(messageDigest.digest());
956         messageDigest = DigestUtils.getShaDigest();
957         DigestUtils.updateDigest(messageDigest, d1);
958         DigestUtils.updateDigest(messageDigest, d2);
959         final String actualResult = Hex.encodeHexString(messageDigest.digest());
960         assertEquals(expectedResult, actualResult);
961     }
962 }