android典型代码系列(八)------传递一个String进行MD5编码

5_传递一个String进行MD5编码 :

package cn.itcast.antivirus;
public class MD5Encoder {
    public static String encode(String pwd) {
        try {
            MessageDigest  digest = MessageDigest.getInstance("MD5");
            byte[]  bytes = digest.digest(pwd.getBytes());
            StringBuffer sb = new  StringBuffer();
            for(int i = 0;i<bytes.length;i++){
                String s = Integer.toHexString(0xff&bytes[i]);
                if(s.length()==1){
                    sb.append("0"+s);
                }else{
                    sb.append(s);
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("buhuifasheng");
        }
    }
}

你可能感兴趣的:(android)