方法一:


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * 计算文件sha256值
 *
 * @author tinymneg
 */
public class GetFileSHA256 {
    public static void main(String[] args) {
        File file = new File("D:\\secmcd\\TestApp.apk");
        System.out.println("文件 " + file + " SHA256值是:" + getFileSHA1(file));
    }
    private static String getFileSHA1(File file) {
        String str = "";
        try {
            str = getHash(file, "SHA-256");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
    private static String getHash(File file, String hashType) throws Exception {
        InputStream fis = new FileInputStream(file);
        byte buffer[] = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance(hashType);
        for (int numRead = 0; (numRead = fis.read(buffer)) > 0; ) {
            md5.update(buffer, 0, numRead);
        }
        fis.close();
        return toHexString(md5.digest());
    }
    private static String toHexString(byte b[]) {
        StringBuilder sb = new StringBuilder();
        for (byte aB : b) {
            sb.append(Integer.toHexString(aB & 0xFF));
        }
        return sb.toString();
    }
}

方法二:

package com.majiameng.php;

import org.json.JSONException;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class fielsha256 {

    public static void main(String[] args) throws JSONException {
        //d91c22c63c92f773d7ba876a111b8ae76bd0127adbc72f772b7848363f43f47b
        String filePath = "C:\\Users\\13146\\Desktop\\tinymeng测试支付.xlsx";
        File file = new File(filePath);

        System.out.println("方法1 文件 " + file + " SHA256值是:" + getFileSHA256(file));
    }


    /**
     * 获得文件的sha256摘要信息
     * @param file  文件
     */
    public static String getFileSHA256(File file) {

        if (!file.exists() || !file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("SHA-256");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        String resultString = parseByte2HexStrLower(digest.digest());
        return resultString;
    }

    /**
     * 将二进制转换成16进制(小写字符串)
     *
     * @param buf
     * @return
     */
    private static String parseByte2HexStrLower(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }


}

Last modification:July 15th, 2021 at 12:41 am
如果觉得我的文章对你有用,请随意赞赏