码迷,mamicode.com
首页 > 移动开发 > 详细

android 使用MD5加密文件

时间:2015-04-23 19:20:35      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

private static final int STREAM_BUFFER_LENGTH = 1024;
private static final String ALGORITHM_MD5 = "MD5";

public static String md5File(File file) { if (null == file) { return ""; } String result = ""; try { MessageDigest digest = MessageDigest.getInstance(ALGORITHM_MD5); byte[] digests = updateDigest(digest, new FileInputStream(file)).digest(); BigInteger bi = new BigInteger(1, digests); result = bi.toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException { final byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); while (read > -1) { digest.update(buffer, 0, read); read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); } return digest; }

  

android 使用MD5加密文件

标签:

原文地址:http://www.cnblogs.com/ivan-aldrich/p/4451304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!