java計算MD5值
阿新 • • 發佈:2017-12-07
int con ger for 計算 utf-8 string md5值 get
package com.linusiyu.common; import java.io.File; import java.io.FileInputStream; import java.math.BigInteger; import java.security.MessageDigest; public class MD5Util { public static String MD5(String content){ byte[] result = new byte[]{}; try { MessageDigest md= MessageDigest.getInstance("MD5"); md.update(content.getBytes("UTF-8")); result = md.digest(); } catch (Exception e) { e.printStackTrace(); } // 使用StringBuilder或者BigInteger都行. StringBuilder sb = new StringBuilder(32); for (inti = 0; i < result.length; i++) { int val = result[i] & 0xff; if (val <= 0xf) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString().toLowerCase(); } public static String MD5(finalFile file) throws Exception { FileInputStream fis = new FileInputStream(file); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] buf = new byte[1024]; int len = -1; while((len = fis.read(buf, 0, 1024)) != -1){ md.update(buf, 0, len); } fis.close(); byte[] result = md.digest(); return new BigInteger(1, result).toString(16).toLowerCase(); } }
java計算MD5值