java MD5

来源:互联网 发布:专业写作软件 编辑:程序博客网 时间:2024/06/07 21:11
[code="java"]
public final static String MD5(String s) {
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = null;
        try {
            mdInst = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException:md5");
            e.printStackTrace();
            return null;
        }
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (byte i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16) sb.append("0");
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    }
[/code]
原创粉丝点击