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