MessageDigest can use the encryption method has md2\md5\sha-1\sha-256\sha-384\sha-512, use time only to replace the corresponding parameter value can be
MessageDigest MD5 = messagedigest.getinstance ("MD5");
MessageDigest MD5 = messagedigest.getinstance ("MD2");
MessageDigest MD5 = messagedigest.getinstance ("SHA-1");
````````````````````````````````````````````````
Code that uses MD5 encryption as an example, in which the code is converted to hexadecimal format data output in two ways
@Test Public voidTest ()throwsnosuchalgorithmexception, unsupportedencodingexception {String str= "a"; MessageDigest MD5= Messagedigest.getinstance ("MD5"); byte[] bytes = Md5.digest (str.getbytes ("Utf-8")); StringBuilder StringBuilder=NewStringBuilder (); for(inti = 0; i < bytes.length; i++) { //0x is hexadecimal, using &0xff is a byte to int, do the complement processing, to ensure the consistency of binary data, the computer stores the complement//integer to hexadecimal using the Tohexstring method of integerString TempStr = integer.tohexstring (Bytes[i] & 0xFF); if(tempstr.length () = = 1) //if the length is 1 complement 0Stringbuilder.append ("0"). Append (TEMPSTR); Elsestringbuilder.append (TEMPSTR); //delimiters such as: 0c:c1:75:b9:c0:f1:b6:a8:31:c3:99:e2:69:77:26:61 if(I < Bytes.length-1) Stringbuilder.append (":"); } System.out.println (Stringbuilder.tostring ()); StringBuilder=NewStringBuilder (); for(inti = 0; i < bytes.length; i++) { intChartindex = bytes[i] & 0xff; //integer to hex, less than 16 for 0~f, 0 output if(Chartindex < 16) {stringbuilder.append ("0"); } stringbuilder.append (Integer.tohexstring (Chartindex)); if(I < Bytes.length-1) Stringbuilder.append (":"); } System.out.println (Stringbuilder.tostring ()); }
MessageDigest class provides encryption algorithms such as MD5 or Sha