org.apache.commons.codec.digest.DigestUtils Example

Q

What is org.apache.commons.codec.digest.DigestUtils class? How to use org.apache.commons.codec.digest.DigestUtils class?

✍: FYIcenter.com

A

org.apache.commons.codec.digest.DigestUtils class is a Java class offered in commons-codec.jar that provides operations to simplify common MessageDigest tasks.

Two commonly used static methods in org.apache.commons.codec.digest.DigestUtils class are:

1. The MD5 digest method:

public static String md5Hex(byte[] data)

Calculates the MD5 digest and returns the value as a 32 character hex string.

Parameters:
    data - Data to digest
Returns:
    MD5 digest as a hex string

2. The SHA1 digest method:

public static String sha1Hex(byte[] data)

Calculates the SHA-1 digest and returns the value as a hex string.

Parameters:
    data - Data to digest
Returns:
    SHA-1 digest as a hex string

Here is a simple example of org.apache.commons.codec.digest.DigestUtils class:

// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.digest.DigestUtils;

// Example of using the DigestUtils class
public class DigestUtilsExample {
   public static void main(String[] args) throws Exception {
      
      System.out.println("md5Hex() Example:");
      String inputString = "The quick brown fox jumps over the lazy dog";
      byte[] inputBytes = inputString.getBytes("UTF8");
      String outputString = DigestUtils.md5Hex(inputBytes);
      String expectedString = "9e107d9d372bb6826bd81d3542a419d6";
      System.out.println("   Input string: "+inputString);
      System.out.println("   Encoded string: "+outputString);
      System.out.println("   Expected string: "+expectedString);

      System.out.println("sha1Hex() Example:");
      inputString = "The quick brown fox jumps over the lazy dog";
      inputBytes = inputString.getBytes("UTF8");
      outputString = DigestUtils.sha1Hex(inputBytes);
      expectedString = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12";
      System.out.println("   Input string: "+inputString);
      System.out.println("   Encoded string: "+outputString);
      System.out.println("   Expected string: "+expectedString);
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   HexExample.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   HexExample
   
md5Hex() Example:
   Input string: The quick brown fox jumps over the lazy dog
   Encoded string: 9e107d9d372bb6826bd81d3542a419d6
   Expected string: 9e107d9d372bb6826bd81d3542a419d6
sha1Hex() Example:
   Input string: The quick brown fox jumps over the lazy dog
   Encoded string: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
   Expected string: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12   

 

org.apache.commons.codec.net.URLCodec Example

org.apache.commons.codec.binary.Hex Example

Using commons-codec.jar in Java Programs

⇑⇑ FAQ for Apache Commons Codec JAR Library

2017-04-22, 2783🔥, 0💬