Commons-codec Usage Introduction

Source: Internet
Author: User
Tags md5 digest

commons-codecis a package provided by the Apache Open source organization for digest operations and encoding. In this package is divided into four types of encryption: Binaryencoders, Digestencoders, Languageencoders, Networkencoders.

Today I would like to introduce you how to use commons-codec the package to complete the common coding, digest operations.

Base64

Example code:

@TestpublicvoidtestBase64(){   System.out.println("==============Base64================");   byte"jianggujin".getBytes();   new Base64();   String encode = base64.encodeAsString(data);   System.out.println(encode);   System.out.println(new String(base64.decode(encode)));}

Operation Result:

==============Base64================amlhbmdndWppbg==jianggujin
MD5 Digest Operations

Example code:

@TestpublicvoidtestMD5(){   System.out.println("==============MD5================");   String result = DigestUtils.md5Hex("jianggujin");   System.out.println(result);}

Operation Result:

acab4efdfd3b8efcdec37fe160d7be0e

Summary operations such as SHA and MD5 are similar.

Urlcodec

Example code:

@TestpublicvoidtestURLCodecthrows Exception{   System.out.println("==============URLCodec================");   new URLCodec();   "蒋固金";   "UTF-8");   System.out.println(encode);   "UTF-8"));}

Operation Result:

==============URLCodec================%E8%92%8B%E5%9B%BA%E9%87%91蒋固金

Complete Sample code:

 PackageCom.gujin.codec;ImportOrg.apache.commons.codec.binary.Base64;ImportOrg.apache.commons.codec.digest.DigestUtils;ImportOrg.apache.commons.codec.net.URLCodec;ImportOrg.junit.Test; Public  class codectest{   @Test    Public void testBase64() {System.out.println ("==============base64================");byte[] data ="Jianggujin". GetBytes (); Base64 base64 =NewBase64 ();      String encode = base64.encodeasstring (data);      System.out.println (encode); System.out.println (NewString (Base64.decode (encode))); }@Test    Public void testMD5() {System.out.println ("==============md5================"); String result = Digestutils.md5hex ("Jianggujin");   SYSTEM.OUT.PRINTLN (result); }@Test    Public void Testurlcodec()throwsException {System.out.println ("==============urlcodec================"); Urlcodec codec =NewUrlcodec (); String data ="Shung"; String encode = codec.encode (data,"UTF-8");      System.out.println (encode); System.out.println (Codec.decode (Encode,"UTF-8")); }}

Commons-codec Usage Introduction

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.