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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5
















This question already has an answer here:




  • Getting a File's MD5 Checksum in Java

    21 answers




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.










share|improve this question















marked as duplicate by Chris Martin, user207421 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 16 '15 at 7:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    5
















    This question already has an answer here:




    • Getting a File's MD5 Checksum in Java

      21 answers




    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.










    share|improve this question















    marked as duplicate by Chris Martin, user207421 java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Aug 16 '15 at 7:42


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      5












      5








      5


      5







      This question already has an answer here:




      • Getting a File's MD5 Checksum in Java

        21 answers




      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.










      share|improve this question

















      This question already has an answer here:




      • Getting a File's MD5 Checksum in Java

        21 answers




      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.





      This question already has an answer here:




      • Getting a File's MD5 Checksum in Java

        21 answers








      java arrays file-io hash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 16 '15 at 7:34









      Rohit Gupta

      2,19891935




      2,19891935










      asked Aug 16 '15 at 7:32









      AbrahamAbraham

      3,22972775




      3,22972775




      marked as duplicate by Chris Martin, user207421 java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Aug 16 '15 at 7:42


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Chris Martin, user207421 java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Aug 16 '15 at 7:42


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          11














          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.






          share|improve this answer


























          • Does the above program work for all big files? (1 GigaByte) for example!

            – Abraham
            Aug 16 '15 at 7:46






          • 1





            @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

            – user207421
            Aug 16 '15 at 7:47













          • I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

            – Abraham
            Aug 16 '15 at 7:50











          • Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

            – Abraham
            Aug 16 '15 at 7:51








          • 1





            @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

            – user207421
            Aug 16 '15 at 7:51




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          11














          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.






          share|improve this answer


























          • Does the above program work for all big files? (1 GigaByte) for example!

            – Abraham
            Aug 16 '15 at 7:46






          • 1





            @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

            – user207421
            Aug 16 '15 at 7:47













          • I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

            – Abraham
            Aug 16 '15 at 7:50











          • Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

            – Abraham
            Aug 16 '15 at 7:51








          • 1





            @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

            – user207421
            Aug 16 '15 at 7:51


















          11














          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.






          share|improve this answer


























          • Does the above program work for all big files? (1 GigaByte) for example!

            – Abraham
            Aug 16 '15 at 7:46






          • 1





            @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

            – user207421
            Aug 16 '15 at 7:47













          • I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

            – Abraham
            Aug 16 '15 at 7:50











          • Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

            – Abraham
            Aug 16 '15 at 7:51








          • 1





            @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

            – user207421
            Aug 16 '15 at 7:51
















          11












          11








          11







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 '18 at 5:15









          gabe870

          318113




          318113










          answered Aug 16 '15 at 7:39









          user207421user207421

          264k26216365




          264k26216365













          • Does the above program work for all big files? (1 GigaByte) for example!

            – Abraham
            Aug 16 '15 at 7:46






          • 1





            @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

            – user207421
            Aug 16 '15 at 7:47













          • I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

            – Abraham
            Aug 16 '15 at 7:50











          • Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

            – Abraham
            Aug 16 '15 at 7:51








          • 1





            @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

            – user207421
            Aug 16 '15 at 7:51





















          • Does the above program work for all big files? (1 GigaByte) for example!

            – Abraham
            Aug 16 '15 at 7:46






          • 1





            @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

            – user207421
            Aug 16 '15 at 7:47













          • I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

            – Abraham
            Aug 16 '15 at 7:50











          • Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

            – Abraham
            Aug 16 '15 at 7:51








          • 1





            @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

            – user207421
            Aug 16 '15 at 7:51



















          Does the above program work for all big files? (1 GigaByte) for example!

          – Abraham
          Aug 16 '15 at 7:46





          Does the above program work for all big files? (1 GigaByte) for example!

          – Abraham
          Aug 16 '15 at 7:46




          1




          1





          @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

          – user207421
          Aug 16 '15 at 7:47







          @Abraham Sure, why not? It only needs 8192 bytes of memory plus whatever the digest uses. You can play with the buffer size but don't make it much smaller.

          – user207421
          Aug 16 '15 at 7:47















          I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

          – Abraham
          Aug 16 '15 at 7:50





          I was thought in the digest.update() method we append the read data at the end of previous data and so we need a lot of memory(RAM) for big files.

          – Abraham
          Aug 16 '15 at 7:50













          Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

          – Abraham
          Aug 16 '15 at 7:51







          Excuse me, what is data in count = bis.read(data) refer to? the same data in my program?

          – Abraham
          Aug 16 '15 at 7:51






          1




          1





          @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

          – user207421
          Aug 16 '15 at 7:51







          @Abraham No, we recalculate the hash progressively. See the Javadoc. Nothing there about appending data. data was a typo.

          – user207421
          Aug 16 '15 at 7:51







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud