MD5 compression algorithm
package cn.com.test; /* incoming parameter: an array of bytes * Outgoing parameter: MD5 result character set of the byte array */ public class MD5 { public static String getMD5 (byte[] source){ String s = null; // used to convert bytes to hexadecimal representation of characters char hexDigits[] = {'0' ,'1', '2', '3', '4' ,'5' ,'6', '7','8','9','a','b','c','d','e','f'}; try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); md.update(source); The result of the //MD5 calculation is a 128-bit long integer, which in bytes is 16 bytes byte tmp[] = md.digest(); // Each byte is represented in hexadecimal using two characters, so 32 characters are needed to represent it in hexadecimal char str[] = new char[16*2]; // indicates the position of the corresponding character in the conversion result int k = 0; for (int i= 0;i< 16;i++){ // Convert each byte of the MD5 to hexadecimal characters, starting with the first byte //fetch the i-th byte byte byte0 = tmp[i]; // Take the high of the byte4 Bitwise digital conversion,>>> is a logical right shift, Shift the sign bits together to the right str[k++] = hexDigits[byte0 >>> 4 & 0xf]; //de-byte the lower 4 bits of the digital conversion str[k++] = hexDigits[byte0 & 0xf]; } s = new String(str); } catch (Exception e) { e.printStackTrace(); } return s; } public static void main(String[] args) { byte byarr[] = {'1','2','3','!','@','#'}; System.out.println(MD5.getMD5(byarr)); } }