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&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