如何在Java中计算文件的哈希值? [英] How to calculate hash value of a file in Java?

查看:342
本文介绍了如何在Java中计算文件的哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下程序来计算Java中字符串的SHA-256哈希值:

  public class ToHash {

public static void main(String [] args){

byte [] data =test.getBytes(UTF8);
MessageDigest digest = MessageDigest.getInstance(SHA-256);
byte [] hash = digest.digest(data);
System.out.println(new BASE64Encoder()。encode(hash));





那么,这工作正常。在接下来的步骤中,我想以一种接受文件并计算其散列值的方式进行开发。我的解决方案是读取字符串数组中的整个文件,并调用该字符串数组上的 digest()方法。但有两个问题:


  1. 我不知道如何将整个文件读入数组?目前我认为我必须一行一行地读取它,并在数组中添加新行!

  2. 上面的方法需要大量文件的内存!






这是我目前读取档案的程式:



pre $ public $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {
// TODO代码应用程序逻辑在这里

//要打开的文件的名称。
String fileName =C:\\ Users \\\\\\\\\\\\\
BufferedReader br = null;

尝试{

String sCurrentLine;
br = new BufferedReader(new FileReader(fileName)); ((sCurrentLine = br.readLine())!= null){
byte [] data = sCurrentLine.getBytes(UTF8);
System.out.println(new BASE64Encoder()。encode(data));

$ b $ catch(IOException e){
e.printStackTrace();
} finally {
try {
if(br!= null){
br.close();
}
} catch(IOException ex){
ex.printStackTrace();









看来,没有方法可以用 BufferedReader 对象来读取整个文件。



 您可以阅读文件并计算哈希的值。

byte [] buffer = new byte [8192];
int count;
MessageDigest digest = MessageDigest.getInstance(SHA-256);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName)); ((count = bis.read(buffer))> 0){
digest.update(buffer,0,count);

}
byte [] hash = digest.digest();
System.out.println(new BASE64Encoder()。encode(hash));

这并不假定任何关于字符集或关于文件拟合到内存中的事情,你也可以使用 DigestInputStream。


I wrote the following program to calculate SHA-256 hash value of a string in Java:

public class ToHash {

    public static void main(String[] args)  {

        byte[] data = "test".getBytes("UTF8");
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(data);
        System.out.println(new BASE64Encoder().encode(hash));

    }
}

Well, that works fine. In the next step I want to develop it in a way to accept a file and calculate its hash value. My solution is to read whole the file in a string array and the call the digest() method on that string array. But there are two problems :

  1. I don't have any idea how to read whole the file into an array? Currently I think I must read it line by line and append an array with the new lines!

  2. Above methodology need a lot of memory for big files!


This is my current program to read a file:

public class ToHash {

    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, FileNotFoundException, IOException {
        // TODO code application logic here

        // The name of the file to open.
        String fileName = "C:\\Users\\ghasemi\\Desktop\\1.png";
        BufferedReader br = null;

        try {

            String sCurrentLine;
            br = new BufferedReader(new FileReader(fileName));
            while ((sCurrentLine = br.readLine()) != null) {
                byte[] data = sCurrentLine.getBytes("UTF8");
                System.out.println(new BASE64Encoder().encode(data));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

It seems that there is no method for BufferedReader object to read whole the file with one call.

解决方案

You can read the file and calculate the value of the hash as you go.

    byte[] buffer= new byte[8192];
    int count;
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
    while ((count = bis.read(buffer)) > 0) {
        digest.update(buffer, 0, count);
    }
    byte[] hash = digest.digest();
    System.out.println(new BASE64Encoder().encode(hash));

This doesn't assume anything about character sets or about the file fitting into memory, and it doesn't ignore line terminators either.

Or you can use a DigestInputStream.

这篇关于如何在Java中计算文件的哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆