Apache Commons Codec and message digest algorithm (hash algorithm), commonshash

Source: Internet
Author: User
Tags md5 digest types of functions

Apache Commons Codec and message digest algorithm (hash algorithm), commonshash

First, we need to understand what Codec means. It is Coder + decoder = Codec, that is, the encoder decoder. It is the encoder and decoder.

Address: http://commons.apache.org/proper/commons-codec/

Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.

It mainly provides Base64, Hex, Phonetic and URLs encoding and decryption.

Impetus

Codec was formed as an attempt to focus development effort on one definitive implementation of the Base64 encoder. at the time of Codec's proposal, there were approximately 34 different Java classes that dealt with Base64 encoding spread over the Foundation's CVS repository. developers in the Jakarta Tomcat project had implemented an original version of the Base64 codec which had been copied by the Commons HttpClient and Apache XML project's XML-RPC subproject. after almost one year, the two forked versions of Base64 had significantly diverged from one another. XML-RPC had applied numerous fixes and patches which were not applied to the Commons HttpClient Base64. Different subprojects had differing implementations at various levels of compliance with the RFC 2045.

Out of that confusing duplication of effort sprang this simple attempt to encourage code reuse among various projects. while this package contains a abstract framework for the creation of encoders and decoders, Codec itself is primarily focused on providing functional utilities for working with common encodings.

The reason for creating a project is:Promote the standardization and unification of Base64 and Other encoding algorithms. Because Base64 has many different implementations and is incompatible with each other.

In fact, the main reason we use Apache Commons Codec is not to use its encoding and decoding function. We generally look at it for a complete implementation."Message Digest" Algorithm, Also known as the hash algorithm.

The Hex and Base64 encoding and decoding functions are only used to assist the "Message Digest" algorithm.

The implementation of the message digest algorithm is mainly as follows:DigestUtils

static MessageDigest     getDigest(String algorithm)  Returns a MessageDigest for the given algorithm.static MessageDigest     getMd2Digest()          Returns an MD2 MessageDigest.static MessageDigest     getMd5Digest()          Returns an MD5 MessageDigest.static MessageDigest     getSha1Digest()         Returns an SHA-1 digest.static MessageDigest     getSha256Digest()        Returns an SHA-256 digest.static MessageDigest     getSha384Digest()        Returns an SHA-384 digest.static MessageDigest     getSha512Digest()        Returns an SHA-512 digest.
static byte[] md2(byte[] data)      Calculates the MD2 digest and returns the value as a 16 element byte[].static byte[] md2(InputStream data)   Calculates the MD2 digest and returns the value as a 16 element byte[].static byte[] md2(String data)      Calculates the MD2 digest and returns the value as a 16 element byte[].static String md2Hex(byte[] data)    Calculates the MD2 digest and returns the value as a 32 character hex string.static String md2Hex(InputStream data) Calculates the MD2 digest and returns the value as a 32 character hex string.static String md2Hex(String data)    Calculates the MD2 digest and returns the value as a 32 character hex string.static byte[] md5(byte[] data)      Calculates the MD5 digest and returns the value as a 16 element byte[].static byte[] md5(InputStream data)   Calculates the MD5 digest and returns the value as a 16 element byte[].static byte[] md5(String data)      Calculates the MD5 digest and returns the value as a 16 element byte[].static String md5Hex(byte[] data)    Calculates the MD5 digest and returns the value as a 32 character hex string.static String md5Hex(InputStream data) Calculates the MD5 digest and returns the value as a 32 character hex string.static String md5Hex(String data) Calculates the MD5 digest and returns the value as a 32 character hex string.
static byte[] sha1(byte[] data)     Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha1(InputStream data)  Calculates the SHA-1 digest and returns the value as a byte[].static byte[] sha1(String data)     Calculates the SHA-1 digest and returns the value as a byte[].static String sha1Hex(byte[] data)   Calculates the SHA-1 digest and returns the value as a hex string.static String sha1Hex(InputStream data)  Calculates the SHA-1 digest and returns the value as a hex string.static String sha1Hex(String data)    Calculates the SHA-1 digest and returns the value as a hex string.static byte[] sha256(byte[] data)     Calculates the SHA-256 digest and returns the value as a byte[].static byte[] sha256(InputStream data)  Calculates the SHA-256 digest and returns the value as a byte[].static byte[] sha256(String data)     Calculates the SHA-256 digest and returns the value as a byte[].static String sha256Hex(byte[] data)   Calculates the SHA-256 digest and returns the value as a hex string.static String sha256Hex(InputStream data)  Calculates the SHA-256 digest and returns the value as a hex string.static String sha256Hex(String data)     Calculates the SHA-256 digest and returns the value as a hex string.static byte[] sha384(byte[] data)     Calculates the SHA-384 digest and returns the value as a byte[].static byte[] sha384(InputStream data)  Calculates the SHA-384 digest and returns the value as a byte[].static byte[] sha384(String data)     Calculates the SHA-384 digest and returns the value as a byte[].static String sha384Hex(byte[] data)   Calculates the SHA-384 digest and returns the value as a hex string.static String sha384Hex(InputStream data)  Calculates the SHA-384 digest and returns the value as a hex string.static String sha384Hex(String data)     Calculates the SHA-384 digest and returns the value as a hex string.static byte[] sha512(byte[] data)       Calculates the SHA-512 digest and returns the value as a byte[].static byte[] sha512(InputStream data)    Calculates the SHA-512 digest and returns the value as a byte[].static byte[] sha512(String data)     Calculates the SHA-512 digest and returns the value as a byte[].static String sha512Hex(byte[] data)   Calculates the SHA-512 digest and returns the value as a hex string.static String sha512Hex(InputStream data)  Calculates the SHA-512 digest and returns the value as a hex string.static String sha512Hex(String data)   Calculates the SHA-512 digest and returns the value as a hex string.

static MessageDigest updateDigest(MessageDigest messageDigest, byte[] valueToDigest) Updates the given MessageDigest.
static MessageDigest updateDigest(MessageDigest digest, InputStream data) Reads through an InputStream and updates the digest for the datastatic MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) Updates the given MessageDigest.

In view of how many functions related to the digest algorithm are described above, they actually exist for ease of use.

We can see theseFunction parameters include byte [], InputStream, and String.. The message digest algorithm is divided into the MD series and SHA series.

In fact, the most important way to differentiate them is to look at their return value type:

1)If the return type is byte [], they are encryption functions, or Hash. That is, Calculates the digest, which is used to calculate the abstract.

2)If the return type is String, they include both the encryption process, that is, the process of calculating the digest, and convert the calculation result to a hex encoded String, to facilitate the storage and comparison of results. One step in place.

3)There are two types of functions that return MessageDigest:Multi-iteration "Message Digest" algorithm for adding salt. The following is an example:

import java.security.MessageDigest;import org.apache.commons.codec.digest.DigestUtils;import org.apache.commons.codec.binary.Hex;import org.apache.shiro.crypto.hash.SimpleHash;import org.junit.Test;public class CodecTest {        @Test    public void test(){         MessageDigest digest = DigestUtils.getSha256Digest();         digest.update("salt".getBytes());         byte[] rs = digest.digest("just a test".getBytes());         int iterations = 10 - 1;         for (int i = 0; i < iterations; i++) {                digest.reset();                rs = digest.digest(rs);         }                  System.out.println(Hex.encodeHex(rs));                  System.out.println(new SimpleHash("sha-256", "just a test", "salt", 10).toString());                  MessageDigest digest2 = DigestUtils.getSha256Digest();         DigestUtils.updateDigest(digest2, "salt");         byte[] rs2 = digest2.digest("just a test".getBytes());         for (int i = 0; i < 9; i++) {                digest2.reset();                rs2 = digest2.digest(rs2);         }         System.out.println(Hex.encodeHex(rs2));    }}

Output result:

8cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead498cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead498cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead49

The above three methods are used to achieve: For the string "just a test" to benefit the algorithm "SHA-256", the salt is "salt", the number of iterations is 10, the algorithm. It also demonstrates the usage of DigestUtils and Hex in Apache Commons Codec.

For more information about the Hex

 

The Hex class converts hexadecimal strings, byte [], and original strings:

@ Test public void testHex () throws DecoderException {String str = "hello, world. "; Char [] c = Hex. encodeHex (str. getBytes (); String hexStr = new String (c); System. out. println (hexStr); hexStr = Hex. encodeHexString (str. getBytes (); System. out. println (hexStr); String originalStr = new String (Hex. decodeHex (hexStr. toCharArray (); System. out. println (originalStr );}

Result:

68656c6c6f2c20e4b896e7958ce4408268656c6c6f2c20e4b896e7958ce10982hello, world.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.