zoukankan      html  css  js  c++  java
  • Java 通用md5工具类

    java 封装的MD5工具包,兼容PHP的MD5函数,代码如下:

    package main.blog.utils;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class Md5Util 
    {
        public static String md5(String buffer)
        {
            String string       = null;
            char hexDigist[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            MessageDigest md;
            try {
                md = MessageDigest.getInstance("MD5");
                md.update(buffer.getBytes());
                byte[] datas = md.digest(); //16个字节的长整数
                
                char[] str = new char[2*16];
                int k = 0;
                
                for(int i=0;i<16;i++)
                {
                  byte b   = datas[i];
                  str[k++] = hexDigist[b>>>4 & 0xf];//高4位
                  str[k++] = hexDigist[b & 0xf];//低4位
                }
                string = new String(str);
            } catch (NoSuchAlgorithmException e) 
            {
                e.printStackTrace();
            }
            return string;
        }
    }
  • 相关阅读:
    bzoj 1503
    bzoj 1193 贪心+bfs
    bzoj 1798 线段树
    Codeforces 804D Expected diameter of a tree
    bzoj 1208
    bzoj 3224
    HDU 5115 区间dp
    hihocoder #1162 矩阵加速dp
    分块入门
    bzoj 1036 树链剖分
  • 原文地址:https://www.cnblogs.com/huxiaoguang/p/10809750.html
Copyright © 2011-2022 走看看