博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记录一下两个比较常用的md5加密算法
阅读量:6910 次
发布时间:2019-06-27

本文共 1932 字,大约阅读时间需要 6 分钟。

第一个,计算字符串的md5值

public static String getMD5(String s){        String newString  = null;        byte[] inputByteArray = s.getBytes();        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',                'A', 'B', 'C', 'D', 'E', 'F' };        try {            MessageDigest md = MessageDigest.getInstance("MD5");            md.update(inputByteArray);            byte[] mdByte = md.digest();            int j = mdByte.length;            char str[] = new char[j * 2];            int k = 0;            for (int i = 0; i < j; i++) {                byte byte0 = mdByte[i];                str[k++] = hexDigits[byte0 >>> 4 & 0xf];                str[k++] = hexDigits[byte0 & 0xf];            }            newString = new String(str);        } catch (Exception e) {            e.printStackTrace();        }        return newString;    }

第二个,计算文件(给出路径)的md5值

public static String getFileMd5(String path) {        try {                        File file = new File(path);            // md5            MessageDigest digest = MessageDigest.getInstance("md5");            FileInputStream fis = new FileInputStream(file);            byte[] buffer = new byte[1024];            int len = -1;            while ((len = fis.read(buffer)) != -1) {                digest.update(buffer, 0, len);            }            byte[] result = digest.digest();            StringBuffer sb = new StringBuffer();            for (byte b : result) {                                int number = b & 0xff;//避免byte转int时出现负数                String str = Integer.toHexString(number);                // System.out.println(str);                if (str.length() == 1) {                    sb.append("0");                }                sb.append(str);            }            return sb.toString();        } catch (Exception e) {            e.printStackTrace();            return "";        }    }

 

转载于:https://www.cnblogs.com/BlogCommunicator/p/4971502.html

你可能感兴趣的文章
认证 (authentication) 和授权 (authorization) 的区别
查看>>
Linux查看磁盘空间大小命令
查看>>
计算机软件著作权查询网址
查看>>
一起谈.NET技术,.Net4.0 Parallel编程(四)Task 上
查看>>
自定义Status Bar的基本方法
查看>>
react动画难写?试试react版transformjs
查看>>
Chrome(12)中使用getComputedStyle获取透明度(opacity)返回字符串不同于其它浏览器...
查看>>
【汉字乱码】IE下GET形式传递汉字。
查看>>
SmartImageView
查看>>
《FineUI秘密花园》在线阅读与完整PDF版
查看>>
android 混淆相关 proguard
查看>>
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
查看>>
android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用...
查看>>
2013年8月第2个周结
查看>>
(转)C的代码是如何变成程序的
查看>>
Udp SocketAsyncEventArgs SocketAsyncDataHandler
查看>>
音频处理平台
查看>>
jQuery(function(){})与(function(){})(jQuery)的区别
查看>>
android widget 开发实例 : 桌面便签程序的实现具体解释和源代码 (上)
查看>>
为什么需要在TypedArray后调用recycle
查看>>