Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate hash value of a file in Java? [duplicate]

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.

like image 265
Ebrahim Ghasemi Avatar asked Aug 16 '15 07:08

Ebrahim Ghasemi


People also ask

Can you use hashing to identify duplicate files?

This function uses an MD5 hash to rapidly detect and delete duplicate files in a directory. This function rapidly compares large numbers of files for identical content by computing the MD5 hash of each file and detecting duplicates.


1 Answers

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);
    }
    bis.close();

    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.

like image 52
user207421 Avatar answered Sep 23 '22 01:09

user207421