zoukankan      html  css  js  c++  java
  • MD5加密

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class MD5 {
        private static final Logger LOG = LoggerFactory.getLogger(MD5.class);
        /**
         * MD5加密函数
         * @param str 需要加密的字符串
         * @return 返回加密后的信息
         */
        public static String crypt(String str) {
            if (str == null || str.length() == 0) {
             throw new IllegalArgumentException("String to encript cannot be null or zero length");
            }
            StringBuffer hexString = new StringBuffer();
            try {
             MessageDigest md = MessageDigest.getInstance("MD5");
             md.update(str.getBytes());
             byte[] hash = md.digest();
             for (int i = 0; i < hash.length; i++) {
              if ((0xff & hash[i]) < 0x10) {
               hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
              } else {
               hexString.append(Integer.toHexString(0xFF & hash[i]));
              }
             }
            } catch (NoSuchAlgorithmException e) {
                LOG.error("md5加密出现异常", e);
            }
            return hexString.toString();
           }
    }
    版权声明:如需转载,请注明!PS:如是转载随便,请忽略
  • 相关阅读:
    codevs1076 排序
    codevs1075 明明的随机数
    codevs1205 单词翻转
    codevs1204 寻找子串位置
    codevs2235 机票打折
    codevs1206 保留两位小数
    codevs1203 判断浮点数是否相等
    codevs1202 求和
    codevs1201 最小数和最大数
    Static Sushi AtCoder
  • 原文地址:https://www.cnblogs.com/zwdx/p/8043927.html
Copyright © 2011-2022 走看看