MD5加密(字符串、文件)工具类,直接用!!!
2016-04-06 16:31
507 查看
public static String getFileMD5(File file) { if (!file.isFile()) { return null; } MessageDigest digest; FileInputStream in = null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } BigInteger bigint = new BigInteger(1, digest.digest()); StringBuilder md5 = new StringBuilder(bigint.toString(16)); return md5.toString(); } public static String MD5(String sourceStr) { String result = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(sourceStr.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } result = buf.toString(); } catch (NoSuchAlgorithmException e) { System.out.println(e); } return result; }
相关文章推荐
- http://blog.csdn.net/lmj623565791/article/details/39121377
- 别的项目导入myeclipese出现struts problem report JDBC Exception:can not open connection解决办法
- 仿函数和模版的模板参数
- cocos2d 在ios上设置横竖屏
- JS 数组去重
- 【LeetCode-239】Sliding Window Maximum
- Android 之 Animation
- 当uitextview 或uitextfield 含有 表情符号 emoji 时 使用 NSMakeRange 可能会出现得异常!
- LintCode_175_翻转二叉树
- 第六周书面项目(2):程序的理解
- 如何解决ul无法撑开div高度的情况
- 公理系统与有限几何(2)
- 我的NopCommerce之旅(6): 应用启动
- 有趣的sql
- 使用系统的照相机和录像机
- yaml文件
- EL表达式获取地址栏地址以及jsp如何获取服务器信息
- nodejs之promise书写规范
- caffe中对6种激活函数类的封装--TanH
- EL表达式获取地址栏地址以及jsp如何获取服务器信息