Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
org.apache.commons.codec.digest.DigestUtils Example
What is org.apache.commons.codec.digest.DigestUtils class? How to use org.apache.commons.codec.digest.DigestUtils class?
✍: FYIcenter.com
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
2017-04-22, 2526🔥, 0💬
Popular Posts:
Jetty provides an HTTP server, HTTP client, and javax.servlet container. These components are open s...
JDK 11 jdk.charsets.jmod is the JMOD file for JDK 11 Charsets module. JDK 11 Charsets module compile...
What is jxl.jar 2.6.12? jxl.jar 2.6.12 is the JAR file for Java Excel API 2.6.12, which is a Java li...
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for m...
JDK 11 java.management.jmod is the JMOD file for JDK 11 Management module. JDK 11 Management module ...