getting java code for hashing files

Solutions on MaxInterview for getting java code for hashing files by the best coders in the world

showing results for - "getting java code for hashing files"
Caterina
20 Jun 2017
1private static String getFileChecksum(MessageDigest digest, File file) throws IOException
2{
3    //Get file input stream for reading the file content
4    FileInputStream fis = new FileInputStream(file);
5     
6    //Create byte array to read data in chunks
7    byte[] byteArray = new byte[1024];
8    int bytesCount = 0; 
9      
10    //Read file data and update in message digest
11    while ((bytesCount = fis.read(byteArray)) != -1) {
12        digest.update(byteArray, 0, bytesCount);
13    };
14     
15    //close the stream; We don't need it now.
16    fis.close();
17     
18    //Get the hash's bytes
19    byte[] bytes = digest.digest();
20     
21    //This bytes[] has bytes in decimal format;
22    //Convert it to hexadecimal format
23    StringBuilder sb = new StringBuilder();
24    for(int i=0; i< bytes.length ;i++)
25    {
26        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
27    }
28     
29    //return complete hash
30   return sb.toString();
31}
32
similar questions
queries leading to this page
getting java code for hashing files