Android计算文件的MD5和SHA1
来源:互联网 发布:广东房卡麻将源码 编辑:程序博客网 时间:2024/05/22 09:44
项目需要,计算文件的MD5和SHA1值,找了一些代码效率比较低,有的还晦涩难懂,这里给出测试后通过,速度也相对较快的代码。
/** * Get the md5 value of the filepath specified file * @param filePath The filepath of the file * @return The md5 value */public String fileToMD5(String filePath) { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); // Create an FileInputStream instance according to the filepath byte[] buffer = new byte[1024]; // The buffer to read the file MessageDigest digest = MessageDigest.getInstance("MD5"); // Get a MD5 instance int numRead = 0; // Record how many bytes have been read while (numRead != -1) { numRead = inputStream.read(buffer); if (numRead > 0) digest.update(buffer, 0, numRead); // Update the digest } byte [] md5Bytes = digest.digest(); // Complete the hash computing return convertHashToString(md5Bytes); // Call the function to convert to hex digits } catch (Exception e) { return null; } finally { if (inputStream != null) { try { inputStream.close(); // Close the InputStream } catch (Exception e) { } } }}/** * Get the sha1 value of the filepath specified file * @param filePath The filepath of the file * @return The sha1 value */public String fileToSHA1(String filePath) { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); // Create an FileInputStream instance according to the filepath byte[] buffer = new byte[1024]; // The buffer to read the file MessageDigest digest = MessageDigest.getInstance("SHA-1"); // Get a SHA-1 instance int numRead = 0; // Record how many bytes have been read while (numRead != -1) { numRead = inputStream.read(buffer); if (numRead > 0) digest.update(buffer, 0, numRead); // Update the digest } byte [] sha1Bytes = digest.digest(); // Complete the hash computing return convertHashToString(sha1Bytes); // Call the function to convert to hex digits } catch (Exception e) { return null; } finally { if (inputStream != null) { try { inputStream.close(); // Close the InputStream } catch (Exception e) { } } }}/** * Convert the hash bytes to hex digits string * @param hashBytes * @return The converted hex digits string */private static String convertHashToString(byte[] hashBytes) {String returnVal = "";for (int i = 0; i < hashBytes.length; i++) {returnVal += Integer.toString(( hashBytes[i] & 0xff) + 0x100, 16).substring(1);}return returnVal.toLowerCase();}
从StackOverflow上找到的,为了提高速度,可以将buffer开的大一点,还有的使用JNI编写的,可以参考。
参考资料:
点击打开链接
点击打开链接
- Android计算文件的MD5和SHA1
- 【Python】计算文件MD5 和 SHA1
- linux计算文件的MD5 与 SHA1
- Android Studio查看签名文件sha1和MD5的方法
- 获取RSA文件的MD5和SHA1
- Go计算MD5和SHA1
- VB.NET-计算文件MD5值和字符串sha1加密算法
- JAVA计算文件的MD5及SHA1等值
- Android 获取 MD5 和 SHA1
- android studio生成签名文件,以及SHA1和MD5值
- android studio生成签名文件,以及SHA1和MD5值
- android studio生成签名文件,以及SHA1和MD5值
- Android Studio生成签名文件,查看SHA1和MD5值
- android studio生成签名文件,以及SHA1和MD5值
- android studio签名文件,以及查看MD5和SHA1信息
- Linux上计算MD5和SHA1
- PHP计算MD5和SHA1值
- 使用openssl就算文件的MD5和SHA1值
- 【转】linux 终端 There are still stopped jobs
- 主线程和另一个线程互发消息进行处理的例子
- Hadoop HDFS之SequenceFile和MapFile
- 配置标准访问控制列表------允许指定网段的计算机可以出站访问其它网段的计算机
- VB 子窗体被PictureBox控件挡住无法显示
- Android计算文件的MD5和SHA1
- CentOS 6.0 系统 LAMP(Apache+MySQL+PHP)安装步骤
- vim 学习笔记 一
- 属性和协议
- 驱动层SSDT 隐藏进程
- fedora 18 下安装 sublime text 2
- PHPdocument2 安装及使用教程
- 信息or存储
- CvMat的用法详解及实例