首页>代码>ssm+mysql开发企业调味品进销存支持管理系统>/CondimentMIS/src/com/design/base/utils/MD5Helper.java
package com.design.base.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 类说明: 简单的MD5的加密获取
 *
 * @Author: Return
 * @Create: 2018-04-24 
 * 
 */

public final class MD5Helper {

    private MD5Helper(){}
    
    protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    protected static MessageDigest messagedigest = null;
    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * MD5字符串
     *
     * @param s
     * @return
     */
    public static String MD5(String s) {
        try {
            byte[] btInput = s.getBytes();
            messagedigest.update(btInput);
            byte[] md = messagedigest.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * MD5 文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileMD5String(File file) throws IOException {
        
        InputStream fis;
        fis = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int numRead = 0;
        while ((numRead = fis.read(buffer)) > 0) {
            messagedigest.update(buffer, 0, numRead);
        }
        fis.close();
        return bufferToHex(messagedigest.digest());
    }
    
    /**
     * 对象HashCode
     *
     * @param object
     * @return
     * @throws IOException
     */
    public static String getHashCode(Object object) throws IOException {
        if (object == null){
            return "";
        }
        
        String ss = null;
        ObjectOutputStream s = null;
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            s = new ObjectOutputStream(bo);
            s.writeObject(object);
            s.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (s != null) {
                s.close();
                s = null;
            }
        }
        ss = MD5(bo.toString());
        return ss;
    }
    
    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }
    
    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }
    
    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }
    
}
最近下载更多
微信网友_6602457430806528  LV6 2023年9月21日
sjl821120  LV6 2023年6月10日
qazwer11  LV5 2022年12月25日
184445428  LV6 2022年12月3日
小猿猿  LV6 2022年9月18日
591231555  LV20 2022年4月18日
叶子2022  LV1 2022年3月11日
扬帆起航  LV2 2022年3月3日
clf123456  LV12 2021年8月9日
xxxdragon  LV10 2021年6月29日
最近浏览更多
1134116035 2月28日
暂无贡献等级
取个啥名字好呢  LV6 1月25日
天天健身房 1月13日
暂无贡献等级
Gin19960217  LV4 1月12日
baozhenghua  LV2 2023年12月11日
xiao小果  LV12 2023年11月27日
haotzy  LV3 2023年10月8日
微信网友_6602457430806528  LV6 2023年9月21日
000000000000000000 2023年9月21日
暂无贡献等级
17347672250  LV1 2023年6月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友