• MD5加密算法的Java版本


      网上搜索Java实现MD5的资料很多,错误的也很多。

      之前编写的一个阿里云直播鉴权原理算法需要用到MD5算法,网上找了几个,都是不行,浪费了时间,现在贴一个,做备用。

      

    import java.security.MessageDigest;
    
    /**
         * MD5加密算法
         * @param s
         * @return
         */
        public String MD5(String s) {
            char hexDigits[] = { '0', '1', '2', '3', '4',
                    '5', '6', '7', '8', '9',
                    'a', 'b', 'c', 'd', 'e', 'f' };
            try {
                byte[] btInput = s.getBytes();
                //获得MD5摘要算法的 MessageDigest 对象
                MessageDigest mdInst = MessageDigest.getInstance("MD5");
                //使用指定的字节更新摘要
                mdInst.update(btInput);
                //获得密文
                byte[] md = mdInst.digest();
                //把密文转换成十六进制的字符串形式
                int j = md.length;
                char str[] = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new String(str);
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
  • 相关阅读:
    Redux
    版本控制(.git + .svn + SourceTree)
    前端埋点
    前端IDE:VSCode + WebStorm
    浏览器
    Mutation Observer
    函数节流与函数去抖
    React 初识
    Ajax
    JS
  • 原文地址:https://www.cnblogs.com/liuxiutianxia/p/8376056.html
Copyright © 2020-2023  润新知