package com.bonait.bnframework.common.image.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Md5加密
 * 已经加密的就不在加密
 */
public class MD5Encoder {
    /**
     * 加密
     * @param pwd
     * @return
     */
    public static String encode(String pwd) {
        try {
            if(pwd.contains("heiboluo|"))
            {
                return pwd;
            }
            else
            {
                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 "heiboluo|"+sb.toString();
            }
        } catch (NoSuchAlgorithmException e) {
            return "buhuifasheng";
        }
    }
}
