http://www.flexiprovider.de/examples/ExamplesIntro.html


Download code

Message Digest: Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. They are used to produce unique and reliable identifiers of data and are sometimes called the "digital fingerprints" of data. Different algorithms can be used to compute the hash value (MD5, SHA-1, etc.).

In this example, we use the MD5 algorithm to compute the hash value of a file stored on the local disk.

First, we do the necessary imports:

01 import java.io.File;
02 import java.io.FileInputStream;
03 import java.security.MessageDigest;
04 import java.security.Security;
05 
06 import de.flexiprovider.common.util.ByteUtils;
06 import de.flexiprovider.core.FlexiCoreProvider;
08 
09 public class ExampleDigest {
10 
11 public static void main(String[] args) throws Exception {
12 
13 Security.addProvider(new FlexiCoreProvider());

As mentioned above, we want to compute the hash value of a local file, so we create a FileInputStream to read the file (make sure that the local file can be found by your Java environment):

15 File file = new File("data.txt");
16 byte[] buffer = new byte[(int) file.length()];
17 FileInputStream fis = new FileInputStream(file);
18 fis.read(buffer);
19 fis.close();

The file is now contained in a byte array, and can be processed by the hash function.

Now comes the main part of the code. We create an instance of the JCA class MessageDigest. The MessageDigest class is designed to provide the functionality of cryptographically secure message digests.

21 MessageDigest md = MessageDigest.getInstance("MD5""FlexiCore");

The first parameter for the static getInstance() method is the standard name of the digest algorithm we want to use (here: "MD5", other algorithm names are possible, like "MD4", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512", "RIPEMD128", "RIPEMD160", "RIPEMD256", "RIPEMD320", "Tiger", "DHA256", and "FORK256"). The second parameter is the name of the security provider implementing the desired algorithm. Since we want to use the previously registered FlexiCoreProvider, we put "FlexiCore" in here.

Now we have instantiated a valid MessageDigest object and can use its methods:

23 md.update(buffer);
24 byte[] digest = md.digest();

First, we process data through the MessageDigest using the update(byte[] input) method. This method can be called multiple times to update data in the MessageDigest object. Then we complete the hash computation with digest(). This returns the desired value as a byte array and resets the MessageDigest object so that it can be used for other computations. (A manual reset could be done with reset() at any time, as well we could have called digest(byte[] input) with our file contents without using the update() method.)

Finally, we convert the byte array to the corresponding hex string for output by using the static method toHexString() from the FlexiProvider package and write the computed value to standard out:

26 System.out.println("MD5 fingerprint: " + ByteUtils.toHexString(digest));
27 }
28 
29 }