MD5加密

package com.score.common.util;


import java.security.MessageDigest;

/** *//**
 * @discription:A tool of Secutity by SCORE
 * @author     :golon
 * @time       :2009-1-25
 * @version    :1.0
 * @see        :no
 */
public class Security {
    private synchronized static byte[] encode(String origin,String code) {
        byte[] hash = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            hash = md.digest(origin.getBytes(code));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return hash;
    }

    public synchronized static String getPassword(String origin,String code) {
        String result = "";
        byte[] hash = Security.encode(origin,code);
        for (int i = 0; i < hash.length; i++) {
            int itemp = hash[i]&0xFF;
            if(itemp<16) result += "0";
            result += Integer.toString(itemp, 16).toLowerCase();
        }
        return result;
    }

    public synchronized static boolean isPassword(String origin, String result,String code) {
        if (Security.getPassword(origin,code).equals(result)) {
            return true;
        }
        return false;
    }
    public static void main(String[] args){
        String result = "";
        
        result = Security.getPassword("tjfae13021117501;1349933222","UTF-8");
        
        System.out.println(result.toLowerCase());
        
        
    }
}

posted @ 2012-05-04 13:50  中国聚龙  阅读(188)  评论(0编辑  收藏  举报