Apache Commons CODEC and Message digest algorithm (hash algorithm)

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

First of all we need to understand what CODEC means. It is Coder + Decoder = Codec, which is the encoder decoder. That is, the encoder is also the decoder.

Website 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.

The main supply is Base64, Hex, Phonetic and URLs, such as encoding and decryption.

Impetus

Codec was formed as a attempt to focus development effort on one definitive implementation of the BASE64 encoder. At the time of Codec ' s proposal, there were approximately-different Java classes that dealt with Base64 encoding spread Over the Foundation ' s CVS repository. Developers in the Jakarta Tomcat project had implemented a 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 other forked versions of BASE64 had significantly diver Ged 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 FOCU SED on providing functional utilities for working with common encodings.

The reason for establishing the project is to promote standardization and unification of coding algorithms such as BASE64. Because there are many different implementations of Base64, they are incompatible .

In fact, we use Apache Commons Codec, the main reason is not to use its encoding decoding function, generally we are fancy it implements a very complete "message digest" algorithm , also known as hash algorithm.

The hex,base64 wait encoding decoding function is only auxiliary to the "Message digest" algorithm.

The implementation of the message digest algorithm is mainly one of the following:digestutils

Staticmessagedigest getdigest (String algorithm) Returns a messagedigest forThe given algorithm.Staticmessagedigest getmd2digest () Returns an MD2 messagedigest.Staticmessagedigest getmd5digest () Returns an MD5 messagedigest.Staticmessagedigest getsha1digest () Returns an SHA-1Digest.Staticmessagedigest getsha256digest () Returns an SHA-256Digest.Staticmessagedigest getsha384digest () Returns an SHA-384Digest.Staticmessagedigest getsha512digest () Returns an SHA-512Digest.
Static byte[] MD2 (byte[] data) Calculates the MD2 digest and returns the value as aElementbyte[].Static byte[] MD2 (InputStream data) calculates the MD2 digest and returns the value as aElementbyte[].Static byte[] MD2 (String data) calculates the MD2 digest and returns the value as aElementbyte[].StaticString Md2hex (byte[] data) Calculates the MD2 digest and returns the value as a32character hex string.StaticString Md2hex (inputstream data) calculates the MD2 digest and returns the value as a32character hex string.Staticstring Md2hex (String data) calculates the MD2 digest and returns the value as a32character hex string.Static byte[] MD5 (byte[] data) Calculates the MD5 digest and returns the value as aElementbyte[].Static byte[] MD5 (InputStream data) calculates the MD5 digest and returns the value as aElementbyte[].Static byte[] MD5 (String data) calculates the MD5 digest and returns the value as aElementbyte[].StaticString Md5hex (byte[] data) Calculates the MD5 digest and returns the value as a32character hex string.StaticString Md5hex (inputstream data) calculates the MD5 digest and returns the value as a32character hex string.Staticstring Md5hex (String data) calculates the MD5 digest and returns the value as a32character Hex string.
Static byte[] SHA1 (byte[] data) Calculates the SHA-1 Digest and returns the value as abyte[].
Static byte[] SHA1 (InputStream data) calculates the SHA-1 Digest and returns the value as abyte[].Static byte[] SHA1 (String data) calculates the SHA-1 Digest and returns the value as abyte[].StaticString Sha1hex (byte[] data) Calculates the SHA-1Digest and returns the value as a hex string.StaticString Sha1hex (inputstream data) calculates the SHA-1Digest and returns the value as a hex string.Staticstring Sha1hex (String data) calculates the SHA-1Digest and returns the value as a hex string.Static byte[] sha256 (byte[] data) Calculates the SHA-256 Digest and returns the value as abyte[].Static byte[] sha256 (InputStream data) calculates the SHA-256 Digest and returns the value as abyte[].Static byte[] sha256 (String data) calculates the SHA-256 Digest and returns the value as abyte[].StaticString Sha256hex (byte[] data) Calculates the SHA-256Digest and returns the value as a hex string.StaticString Sha256hex (inputstream data) calculates the SHA-256Digest and returns the value as a hex string.Staticstring Sha256hex (String data) calculates the SHA-256Digest and returns the value as a hex string.Static byte[] sha384 (byte[] data) Calculates the SHA-384 Digest and returns the value as abyte[].Static byte[] sha384 (InputStream data) calculates the SHA-384 Digest and returns the value as abyte[].Static byte[] sha384 (String data) calculates the SHA-384 Digest and returns the value as abyte[].StaticString Sha384hex (byte[] data) Calculates the SHA-384Digest and returns the value as a hex string.StaticString Sha384hex (inputstream data) calculates the SHA-384Digest and returns the value as a hex string.Staticstring Sha384hex (String data) calculates the SHA-384Digest and returns the value as a hex string.Static byte[] sha512 (byte[] data) Calculates the SHA-512 Digest and returns the value as abyte[].Static byte[] sha512 (InputStream data) calculates the SHA-512 Digest and returns the value as abyte[].Static byte[] sha512 (String data) calculates the SHA-512 Digest and returns the value as abyte[].StaticString Sha512hex (byte[] data) Calculates the SHA-512Digest and returns the value as a hex string.StaticString Sha512hex (inputstream data) calculates the SHA-512Digest and returns the value as a hex string.Staticstring Sha512hex (String data) calculates the SHA-512Digest and returns the value as a hex string.

StaticMessageDigest updatedigest (MessageDigest messagedigest,byte[] valuetodigest) Updates to the given messagedigest.
Staticmessagedigest updatedigest (MessageDigest Digest, InputStream data) Reads through an inputstream and updates the Dige St forThe dataStaticmessagedigest updatedigest (messagedigest messagedigest, String valuetodigest) Updates the given messagedigest.

For the above and the "Digest algorithm" related functions, in fact, their existence is for the convenience of use.

We see that the parameters of these functions are divided into three types: byte[], InputStream, String, for ease of use . the message digest algorithm is divided into 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[], then they are cryptographic functions, or hash. that is calculates the digest, calculates the function of the digest.

2) If the return type is string, then they contain both the encryption process, which is the process of calculating the digest, as well as converting the computed result into a 16-encoded string to facilitate storing the results and comparing the results. One step

3) There are two types of functions that return MessageDigest , both of which are intended to be implemented: the "message digest" algorithm for multiple iterations of salt . Let's look at an example:

Importjava.security.MessageDigest;Importorg.apache.commons.codec.digest.DigestUtils;ImportOrg.apache.commons.codec.binary.Hex;ImportOrg.apache.shiro.crypto.hash.SimpleHash;Importorg.junit.Test; Public classcodectest {@Test Public voidTest () {MessageDigest Digest=digestutils.getsha256digest (); Digest.update ("Salt". GetBytes ()); byte[] rs = Digest.digest ("Just a Test". GetBytes ()); intiterations = 10-1;  for(inti = 0; I < iterations; i++) {digest.reset (); RS=Digest.digest (RS);                  } System.out.println (Hex.encodehex (RS)); System.out.println (NewSimplehash ("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(inti = 0; I < 9; i++) {digest2.reset (); RS2=digest2.digest (RS2);    } System.out.println (Hex.encodehex (RS2)); }}

Output Result:

8cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead498cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed 63ead498cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead49

The above uses three methods to achieve: The string "Just a test" is beneficial to the algorithm "sha-256", salt is "salted", the number of iterations is 10, the algorithm . Also demonstrates the use of digestutils and Hex in Apache Commons Codec.

For information on why Hex 16 encoding and related issues, see Cryptographic decryption Basics: byte arrays and string conversions

Hex class in 16 binary string and byte[] and the conversion of the original string:

    @Test    publicvoidthrows  decoderexception{        = "Hello, world." ";         Char [] C = Hex.encodehex (str.getbytes ());         New String (c);        System.out.println (HEXSTR);                 = hex.encodehexstring (str.getbytes ());        System.out.println (HEXSTR);                 New String (Hex.decodehex (Hexstr.tochararray ()));        System.out.println (ORIGINALSTR);    }

Results:

68656c6c6f2c20e4b896e7958ce3808268656c6c6f2c20e4b896e7958ce38082hello, the world.

Apache Commons CODEC and Message digest algorithm (hash algorithm)

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.