package com.cpifeng.top.security;

/**
 * MD5算法 
 */
public class MD5 {  
  
    /** 
     * 获取MD5 结果字符串 
     * String text = "20080711000000000000000424221111112010-01-20";
        System.out.println(digest(text));
     * 3acbf4dd991a5d9b111b5ddd13e84e4d
     * 3acbf4dd991a5d9b111b5ddd13e84e4d
     *  
     * @param source 
     * @return 
     */  
    public static String encode(byte[] source) {  
        String s = null;  
        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);  
            byte tmp[] = md.digest();   
            char str[] = new char[16 * 2];   
            int k = 0;   
            for (int i = 0; i < 16; i++) {   
                byte byte0 = tmp[i];   
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];   
                str[k++] = hexDigits[byte0 & 0xf];   
            }  
            s = new String(str);   
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return s;  
    }  
      
    public static String getMD5(String source) {  
        return (source == null || "".equals(source)) ? "" : encode(source.getBytes());  
    }  
    
    public static void main(String[] args) {
        String text = "20080711000000000000000424221111112010-01-20";
        System.out.println(encode(text.getBytes()));
        System.out.println(getMD5(text));
        System.out.println(getMD5(getMD5("222222"))); //4804910e6052bf32bd8b078b5f21fddc
    }
}  
最近下载更多
曦雨aisa  LV2 2020年12月12日
blueskyroof  LV9 2019年11月28日
a986527557  LV6 2019年11月19日
wa8021110  LV9 2019年7月16日
jiajia91323  LV23 2019年4月22日
青春是首歌-pla  LV6 2019年4月19日
文心雕龙  LV6 2019年4月6日
梦蝶  LV18 2019年2月5日
zhangguanghui  LV7 2019年1月11日
aisc1314  LV14 2018年12月28日
最近浏览更多
iiiiiiixiiiiii  LV1 1月5日
f22m1a2b2  LV16 2023年6月16日
刘君88888 2022年8月6日
暂无贡献等级
许文欣  LV2 2022年3月26日
rookandfuze 2022年2月18日
暂无贡献等级
别碰我被窝  LV9 2021年10月25日
我家大宝贝  LV4 2021年6月10日
JIeless  LV2 2021年2月5日
2252536772  LV21 2021年2月4日
曦雨aisa  LV2 2020年12月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友