/**
     * @param str
     * @return
     */
    public static String stringToMD5(String str, String encode) {
        try {
            byte[] strTemp = str.getBytes(encode);
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            return toHexString(mdTemp.digest());
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return null;
    }

 

    /**
     * @param md
     * @return
     */
    private static String toHexString(byte[] md) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        int j = md.length;
        char str[] = new char[j * 2];
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[2 * i] = hexDigits[byte0 >>> 4 & 0xf];
            str[i * 2 + 1] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    }

 

posted on 2013-09-02 15:38  爱生活的夜  阅读(149)  评论(0编辑  收藏  举报