Summary algorithm test MD5 or SHA-1

Source: Internet
Author: User
Tags md5 digest

Package Com.fenghao.other;import Java.security.messagedigest;import java.security.nosuchalgorithmexception;/** * * <P> Summary Tool class using MD5 or SHA-1 digest algorithm </P> * @ClassName: messagedigestutils * @author Feng Hao November 29, 2016 PM 3:15:54 * @see TODO * /public class Messagedigestutils {public string digestmessage (String context,string hashtype) throws Nosuchalgorithmexception{messagedigest in = Messagedigest.getinstance (Hashtype); byte[] Digest = In.digest ( Context.getbytes ()); return Bytes2hex (Digest);} Private String bytes2hex (byte[] bytes) {StringBuilder str=new StringBuilder (); for (int i = 0; i < bytes.length; i++) { BYTE b=bytes[i]; Boolean negative=false;//to determine positive and negative if (b<0) negative=true; int line = Math.Abs (b);//Take absolute if (negative) line=line|0x80; 0x represents the hexadecimal 8*16=128 binary representation: 10000000 Negative negative is the positive highest position minus sign becomes a numeric value to calculate the String tmp = integer.tohexstring (LINE&AMP;0XFF); if (Tmp.length () ==1) {str.append ("0");} str.append (Tmp.tolowercase ());} return str.tostring ();} /** * * <p> method Test </p> * <p>description:todo</p> * @author Feng Hao November 29, 2016 PM 3:41:13 * * @org. junit.testpublic void Test () {int a=3;//3int b=-3;//3int c=128 ;//128int d=-128;//128int e=129;//129int f=-129;//129 int i = Math.Abs (-199);//Take absolute value int n=d|0x80; System.out.println ("\ n is" +n); public static void Main (string[] args) {messagedigestutils message=new messagedigestutils (); String context= "Fenghao"; try {String result = Message.digestmessage (context, "MD5"); System.out.println ("\ n result is" +result);} catch (NoSuchAlgorithmException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Use the MD5 Digest to encode the byte array in 16!

Summary algorithm test MD5 or SHA-1

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.