All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.frameworkset.wx.common.util.MD5Util Maven / Gradle / Ivy

There is a newer version: 5.6.4
Show newest version
package org.frameworkset.wx.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
	public static String toMD5(String str){
		try {
		      MessageDigest messageDigest =MessageDigest.getInstance("MD5");  
		      byte[] inputByteArray = str.getBytes();  
		      messageDigest.update(inputByteArray);  
		      byte[] resultByteArray = messageDigest.digest();  
		      return byteArrayToHex(resultByteArray); 
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "false";
		}
	}
	
	   public static String byteArrayToHex(byte[] byteArray) {

		      // 首先初始化一个字符数组,用来存放每个16进制字符

		      char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };

		 

		      // new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))

		      char[] resultCharArray =new char[byteArray.length * 2];

		 

		      // 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去

		      int index = 0;

		      for (byte b : byteArray) {

		         resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];

		         resultCharArray[index++] = hexDigits[b& 0xf];

		      }

		 

		      // 字符数组组合成字符串返回

		      return new String(resultCharArray);

		}
	   
	   
	   public static String toMD51(String str){
			try {
			      MessageDigest messageDigest =MessageDigest.getInstance("MD5");  
			      byte[] inputByteArray = str.getBytes();  
			      messageDigest.update(inputByteArray);  
			      byte[] resultByteArray = messageDigest.digest();  
			      return byteArrayToHex1(resultByteArray); 
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
				return "false";
			}
		}
		
		   public static String byteArrayToHex1(byte[] byteArray) {

			      // 首先初始化一个字符数组,用来存放每个16进制字符

			      char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f' };

			 

			      // new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))

			      char[] resultCharArray =new char[byteArray.length * 2];

			 

			      // 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去

			      int index = 0;

			      for (byte b : byteArray) {

			         resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];

			         resultCharArray[index++] = hexDigits[b& 0xf];

			      }

			 

			      // 字符数组组合成字符串返回

			      return new String(resultCharArray);

			}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy