33

What is the best way to create a SHA-1 for a very large file in pure Java6? How to implement this method:

public abstract String createSha1(java.io.File file);
Witek
  • 6,160
  • 7
  • 43
  • 63

3 Answers3

42

Use the MessageDigest class and supply data piece by piece. The example below ignores details like turning byte[] into string and closing the file, but should give you the general idea.

public byte[] createSha1(File file) throws Exception  {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    InputStream fis = new FileInputStream(file);
    int n = 0;
    byte[] buffer = new byte[8192];
    while (n != -1) {
        n = fis.read(buffer);
        if (n > 0) {
            digest.update(buffer, 0, n);
        }
    }
    return digest.digest();
}
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
24

Op requested the function to return a String of the SHA1, so I took @jeffs answer and added the missing conversion to String:

/**
 * Read the file and calculate the SHA-1 checksum
 * 
 * @param file
 *            the file to read
 * @return the hex representation of the SHA-1 using uppercase chars
 * @throws FileNotFoundException
 *             if the file does not exist, is a directory rather than a
 *             regular file, or for some other reason cannot be opened for
 *             reading
 * @throws IOException
 *             if an I/O error occurs
 * @throws NoSuchAlgorithmException
 *             should never happen
 */
private static String calcSHA1(File file) throws FileNotFoundException,
        IOException, NoSuchAlgorithmException {

    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    try (InputStream input = new FileInputStream(file)) {

        byte[] buffer = new byte[8192];
        int len = input.read(buffer);

        while (len != -1) {
            sha1.update(buffer, 0, len);
            len = input.read(buffer);
        }

        return new HexBinaryAdapter().marshal(sha1.digest());
    }
}
user2084795
  • 704
  • 1
  • 7
  • 20
3
public static String computeFileSHA1( File file ) throws IOException
{
    String sha1 = null;
    MessageDigest digest;
    try
    {
        digest = MessageDigest.getInstance( "SHA-1" );
    }
    catch ( NoSuchAlgorithmException e1 )
    {
        throw new IOException( "Impossible to get SHA-1 digester", e1 );
    }
    try (InputStream input = new FileInputStream( file );
         DigestInputStream digestStream = new DigestInputStream( input, digest ) )
    {
        while(digestStream.read() != -1){
            // read file stream without buffer
        }
        MessageDigest msgDigest = digestStream.getMessageDigest();
        sha1 = new HexBinaryAdapter().marshal( msgDigest.digest() );
    }
    return sha1;
}
bighux
  • 31
  • 1
  • 1
    Although your answer may be correct, it would be helpful to the OP if you could add some commentary explaining why it is. This way it will be useful to others. – rrd Feb 15 '19 at 15:43
  • Initially request was related to Java6. Your code provided will not been compiled under Java6! – user12377884 Nov 16 '21 at 13:22