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