zoukankan      html  css  js  c++  java
  • java生成字符串md5函数类

    import java.security.MessageDigest;
    
    /**
     * Md5 工具
     */
    public class Md5Util {
    
        private static MessageDigest md5 = null;
        static {
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    
        /**
         * 用于获取一个String的md5值
         * @param string
         * @return
         */
        public static String getMd5(String str) {
            byte[] bs = md5.digest(str.getBytes());
            StringBuilder sb = new StringBuilder(40);
            for(byte x:bs) {
                if((x & 0xff)>>4 == 0) {
                    sb.append("0").append(Integer.toHexString(x & 0xff));
                } else {
                    sb.append(Integer.toHexString(x & 0xff));
                }
            }
            return sb.toString();
        }
    
        public static void main(String[] args) {
            System.out.println(getMd5("hello world"));
        }
    }
    

      

  • 相关阅读:
    简单题
    bzoj2131
    bzoj1706
    bzoj3531
    bzoj3744
    bzoj2724
    bzoj3343
    bzoj1005
    编程中、遇到问题、bug多思考
    线上系统奇怪问题总结,性能问题不能依赖经验
  • 原文地址:https://www.cnblogs.com/koal/p/4394033.html
Copyright © 2011-2022 走看看