集册 Java实例教程 获取文件SHA 384

获取文件SHA 384

欢马劈雪     最近更新时间:2020-01-02 10:19:05

449
获取文件SHA 384

// Licensed under the Apache License, Version 2.0 (the "License");

//package com.nowjava;

import java.io.File;
/* 
 来自 
*nowjava - 时代Java*/

import java.io.FileInputStream;

import java.io.InputStream;

import java.security.MessageDigest;


public class Main {

    public static String getFileSHA384(File file) {

        String str = "";

        try {

            str = getHash(file, "SHA-384");

        } catch (Exception e) {

            e.printStackTrace();/*NowJava.com - 时代Java*/

        }

        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[]) 
展开阅读全文