Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, 3064🔥, 0💬
Popular Posts:
The Web Services Description Language for Java Toolkit (WSDL4J), Release 1.6.2, allows the creation,...
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms, it was develo...
What Is poi-3.5.jar - Part 2? poi-3.5.jar is one of the JAR files for Apache POI 3.5, which provides...
commons-fileupload-1.3.3 -sources.jaris the source JAR file for Apache Commons FileUpload 1.3., whic...
xml-commons Resolver Source Code Files are provided in the source package file, xml-commons-resolver...