Skip to content

Commit c66e7c8

Browse files
authored
Added BAEL-3604 code (eugenp#9020)
* Added BAEL-3604 code
1 parent 5b39f14 commit c66e7c8

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

core-java-modules/core-java-security-2/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</parent>
1818

1919
<dependencies>
20+
2021
<dependency>
2122
<groupId>commons-codec</groupId>
2223
<artifactId>commons-codec</artifactId>
@@ -36,6 +37,15 @@
3637
<version>${assertj-core.version}</version>
3738
<scope>test</scope>
3839
</dependency>
40+
41+
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
42+
<dependency>
43+
<groupId>javax.xml.bind</groupId>
44+
<artifactId>jaxb-api</artifactId>
45+
<version>2.3.1</version>
46+
</dependency>
47+
48+
3949
</dependencies>
4050

4151
<properties>
@@ -46,4 +56,5 @@
4656
<!-- testing -->
4757
<assertj-core.version>3.10.0</assertj-core.version>
4858
</properties>
59+
4960
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.checksums;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.zip.CRC32;
6+
import java.util.zip.CheckedInputStream;
7+
import java.util.zip.Checksum;
8+
9+
public class ChecksumUtils {
10+
11+
public static long getChecksumCRC32(byte[] bytes) {
12+
Checksum crc32 = new CRC32();
13+
crc32.update(bytes, 0, bytes.length);
14+
return crc32.getValue();
15+
}
16+
17+
public static long getChecksumCRC32(InputStream stream, int bufferSize) throws IOException {
18+
CheckedInputStream checkedInputStream = new CheckedInputStream(stream, new CRC32());
19+
byte[] buffer = new byte[bufferSize];
20+
while (checkedInputStream.read(buffer, 0, buffer.length) >= 0) {}
21+
return checkedInputStream.getChecksum().getValue();
22+
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.checksums;
2+
3+
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class ChecksumUtilsUnitTest {
14+
15+
byte[] arr;
16+
17+
@Before
18+
void setUp() {
19+
arr = new byte[]{0,10,21,20,35,40,120,56,72,22};
20+
}
21+
22+
@Test
23+
void givenByteArray_whenChecksumCreated_checkCorrect() {
24+
25+
long checksum = ChecksumUtils.getChecksumCRC32(arr);
26+
27+
assertEquals(3915397664L, checksum);
28+
}
29+
30+
@Test
31+
void givenTwoDifferentStrings_whenChecksumCreated_checkCollision() {
32+
33+
String plumless = "plumless";
34+
String buckeroo = "buckeroo";
35+
36+
long plumlessChecksum = ChecksumUtils.getChecksumCRC32(plumless.getBytes());
37+
long buckerooChecksum = ChecksumUtils.getChecksumCRC32(buckeroo.getBytes());
38+
39+
assertEquals(plumlessChecksum, buckerooChecksum);
40+
}
41+
42+
@Test
43+
void givenInputString_whenChecksumCreated_checkCorrect() throws IOException {
44+
45+
InputStream inputStream = new ByteArrayInputStream(arr);
46+
long checksum = ChecksumUtils.getChecksumCRC32(inputStream, 10);
47+
48+
assertEquals(3915397664L, checksum);
49+
50+
}
51+
}

0 commit comments

Comments
 (0)