您现在的位置是:首页 > 博客日记 > Java Java

java获取文件SHA256值

2021-07-15 00:39:10 【Java】 人已围观

方法一:

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.security.MessageDigest;
  5. /**
  6. * 计算文件sha256值
  7. *
  8. * @author tinymneg
  9. */
  10. public class GetFileSHA256 {
  11. public static void main(String[] args) {
  12. File file = new File("D:\\secmcd\\TestApp.apk");
  13. System.out.println("文件 " + file + " SHA256值是:" + getFileSHA1(file));
  14. }
  15. private static String getFileSHA1(File file) {
  16. String str = "";
  17. try {
  18. str = getHash(file, "SHA-256");
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return str;
  23. }
  24. private static String getHash(File file, String hashType) throws Exception {
  25. InputStream fis = new FileInputStream(file);
  26. byte buffer[] = new byte[1024];
  27. MessageDigest md5 = MessageDigest.getInstance(hashType);
  28. for (int numRead = 0; (numRead = fis.read(buffer)) > 0; ) {
  29. md5.update(buffer, 0, numRead);
  30. }
  31. fis.close();
  32. return toHexString(md5.digest());
  33. }
  34. private static String toHexString(byte b[]) {
  35. StringBuilder sb = new StringBuilder();
  36. for (byte aB : b) {
  37. sb.append(Integer.toHexString(aB & 0xFF));
  38. }
  39. return sb.toString();
  40. }
  41. }

方法二:

  1. package com.majiameng.php;
  2. import org.json.JSONException;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.security.MessageDigest;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.InputStream;
  9. import java.security.MessageDigest;
  10. public class fielsha256 {
  11. public static void main(String[] args) throws JSONException {
  12. //d91c22c63c92f773d7ba876a111b8ae76bd0127adbc72f772b7848363f43f47b
  13. String filePath = "C:\\Users\\13146\\Desktop\\tinymeng测试支付.xlsx";
  14. File file = new File(filePath);
  15. System.out.println("方法1 文件 " + file + " SHA256值是:" + getFileSHA256(file));
  16. }
  17. /**
  18. * 获得文件的sha256摘要信息
  19. * @param file 文件
  20. */
  21. public static String getFileSHA256(File file) {
  22. if (!file.exists() || !file.isFile()) {
  23. return null;
  24. }
  25. MessageDigest digest = null;
  26. FileInputStream in = null;
  27. byte buffer[] = new byte[1024];
  28. int len;
  29. try {
  30. digest = MessageDigest.getInstance("SHA-256");
  31. in = new FileInputStream(file);
  32. while ((len = in.read(buffer, 0, 1024)) != -1) {
  33. digest.update(buffer, 0, len);
  34. }
  35. in.close();
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. return null;
  39. }
  40. String resultString = parseByte2HexStrLower(digest.digest());
  41. return resultString;
  42. }
  43. /**
  44. * 将二进制转换成16进制(小写字符串)
  45. *
  46. * @param buf
  47. * @return
  48. */
  49. private static String parseByte2HexStrLower(byte buf[]) {
  50. StringBuffer sb = new StringBuffer();
  51. for (int i = 0; i < buf.length; i++) {
  52. String hex = Integer.toHexString(buf[i] & 0xFF);
  53. if (hex.length() == 1) {
  54. hex = '0' + hex;
  55. }
  56. sb.append(hex);
  57. }
  58. return sb.toString();
  59. }
  60. }


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()

上一篇:大数据分析流程