1. 程式人生 > >java計算MD5值

java計算MD5值

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 (int
i = 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(final
File 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值