Android计算文件的MD5和SHA1
2013-11-27 21:21
429 查看
项目需要,计算文件的MD5和SHA1值,找了一些代码效率比较低,有的还晦涩难懂,这里给出测试后通过,速度也相对较快的代码。
从StackOverflow上找到的,为了提高速度,可以将buffer开的大一点,还有的使用JNI编写的,可以参考。
参考资料:
点击打开链接
点击打开链接
/** * 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】debug 状态下其签名文件 debug.keystore 相关(如何获得该文件,其密码,获取其sha1、MD5等)
- Android开发之查看签名文件信息,查看MD5,SHA1信息
- 小巧好用的哈希计算器,支持文件拖放,速度很快,可以计算文件的 MD5、SHA1、CRC32 的值。
- 获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值
- JAVA计算文件的MD5及SHA1等值
- 获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值
- linux计算文件的MD5 与 SHA1
- 文件 MD5、SHA1、SHA256 计算
- 获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值
- 【Python】计算文件MD5 和 SHA1
- 获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值
- Doc命令查看Android前面文件SHA1,MD5,SHA256值
- 如何计算文件MD5 sha1 -- 微软MD5/SHA1 校验工具 Microsoft File Checksum Integrity Verifier
- Android 计算文件 MD5 遇到的问题
- Java计算字符串和文件的MD5与SHA1
- 【转】Android获取调试和正式的SHA1和MD5的值
- 文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3
- C#遍历文件夹下所有文件通过MD5函数计算文件的MD5
- (转)Android SDCard操作(文件读写,容量计算)
- python 文件MD5 SHA1校验计算