commons-checksum
![Coverage](https://camo.githubusercontent.com/89a7c8431fd7d497abc86e2d163f6daedac934adc904cdc88724866b50e50434/68747470733a2f2f636f6465636f762e696f2f67682f716c6566657672652f636f6d6d6f6e732d636865636b73756d2f6272616e63682f6d61737465722f67726170682f62616467652e737667)
Commons Checksum provides functional utilities for working with common checksums and hashes. Project relies on the great utilities providing by the DigestUtils class of Apache Commons Codec project.
The class org.apache.commons.codec.digest.DigestUtils was improved to handle new algorithms such as:
- MD2
- MD4
- MD5
- RIPEMD-128
- RIPEMD-160
- RIPEMD-256
- RIPEMD-320
- SHA-1
- SHA-224
- SHA-256
- SHA-384
- SHA-512
- Tiger
- Whirlpool
A new class ChecksumUtils which extends DigestUtils was added. It handles checksum algorithms such as :
- Adler-32
- CRC-32
- Fletcher-32
Installation
To use the latest release of commons-checksum, please use the following snippet in your pom.xml.
<dependency>
<groupId>com.github.qlefevre</groupId>
<artifactId>commons-checksum</artifactId>
<version>1.0.0</version>
</dependency>
Two Minute Tutorial
There are six method signatures for each algorithm as described below :
public static byte[] md2(byte[] data)
public static byte[] md2(InputStream data) throws IOException
public static byte[] md2(String data)
public static String md2Hex(byte[] data)
public static String md2Hex(InputStream data) throws IOException
public static String md2Hex(String data)
Calculating the checksum of a byte array
public static final byte[] HELLO_WORLD_BYTE_ARRAY = "Hello World".getBytes();
String crc32Hex = ChecksumUtils.crc32Hex(HELLO_WORLD_BYTE_ARRAY);
String sha512Hex = ChecksumUtils.sha512Hex(HELLO_WORLD_BYTE_ARRAY);
Calculating the checksum of a String
public static final String HELLO_WORLD_STRING = "Hello World";
String md5Hex = ChecksumUtils.md5Hex(HELLO_WORLD_STRING);
String whirlpoolHex = ChecksumUtils.whirlpoolHex(HELLO_WORLD_STRING);
Note
Oracle JDK 8 offers 7 digest algorithms : MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512.
Digest | Oracle JDK 1.8> | Oracle JDK 1.6> | Bouncy Castle |
---|---|---|---|
MD2 | yes | yes | yes |
MD4 | no | no | yes |
MD5 | yes | yes | yes |
RIPEMD-128 | no | no | yes |
RIPEMD-160 | no | no | yes |
RIPEMD-256 | no | no | yes |
RIPEMD-320 | no | no | yes |
SHA-1 | yes | yes | yes |
SHA-224 | yes | no | yes |
SHA-256 | yes | yes | yes |
SHA-384 | yes | yes | yes |
SHA-512 | yes | yes | yes |
Tiger | no | no | yes |
Whirlpool | no | no | yes |
See:
- Bouncy Castle : org.bouncycastle.jce.provider.JDKMessageDigest
- Sun : sun.security.provider.Sun
Configuring Bouncy Castle
If you have a mavenized project, you can add this dependency :
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk16</artifactId>
<version>1.46</version>
</dependency>
You can now use the Whirlpool digest. You just have to register the provider programatically
Security.addProvider(new BouncyCastleProvider());
String whirlpool = whirlpool2Hex("Hello world !");