4

I have a file, and I need to calculate the SHA512 value for it. I've found plenty of sites offering to do it for me, but I'd like to do it programmatically in Java (well, Groovy, but it's the same thing).

For those curious, I'm running Oracle's TZUpdater tool and pointing it at a local file. This requires a file that contains the SHA512 value for that file. http://www.oracle.com/technetwork/java/javase/tzupdater-readme-136440.html

Steve
  • 4,457
  • 12
  • 48
  • 89
  • Well, how do you calculate an SHA512 hash (or a 'message digest') in Java? Apply it to a file. – user2864740 Jul 30 '15 at 19:16
  • Or, with a little searching .. http://stackoverflow.com/questions/16050827/filechannel-bytebuffer-and-hashing-files , http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java – user2864740 Jul 30 '15 at 19:16
  • @user2864740 - That one hadn't come up in my search. While it would have been a potential solution, Louis Wasserman and mzc's answers below are much cleaner. – Steve Jul 30 '15 at 19:40
  • Please *try* a search for "guava hash file". None of those words were .. tricky to pick. Several of the links are to the Guava File.hash API, another to one of the linked questions (which in addition to native Java answers references said File.hash method) and yet another is an excerpt from a book .. etc. – user2864740 Jul 30 '15 at 19:44
  • I mean now that I know that Guava has a solution for this I could do that. I didn't know to search Guava specifically before (hence, very tricky to pick). My mistake was all my searches were for sha512 specifically, which definitely narrowed me out of most solutions. – Steve Jul 30 '15 at 20:21

4 Answers4

6

If third-party libraries are fair game, Guava's Files.hash could make this as simple as

Files.hash(new File(fileName), Hashing.sha512()).toString();

...which would also potentially be more efficient; if the file is large, it not need be stored in memory all at once as in the Files.readAllBytes solution. This will also output a proper hash in hexadecimal; if you need it in bytes just use asBytes() instead of toString().

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
5

You can calculate the SHA-512 digest of a file with this code snippet:

MessageDigest.getInstance("SHA-512").digest(Files.readAllBytes(Paths.get("/path/file.txt")))

For this code to work you will need JDK7 or higher.

Note: if the file is too big to fit in memory you should probably go with Guava as proposed.

mzc
  • 3,265
  • 1
  • 20
  • 25
2

You could also use Apache Commons Codec.

Maven Repository: https://mvnrepository.com/artifact/commons-codec/commons-codec

Code example:

public static String calcSha512Hex(File file) throws FileNotFoundException, IOException {
    return org.apache.commons.codec.digest.DigestUtils.sha512Hex(new FileInputStream(file));
}
S. Doe
  • 685
  • 1
  • 6
  • 25
0

Simplest solution, no external libs, no problems with big files:

public static String hashFile(File file)
        throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    // Set your algorithm
    // "MD2","MD5","SHA","SHA-1","SHA-256","SHA-384","SHA-512"
    MessageDigest md = MessageDigest.getInstance("SHA-512");
    FileInputStream fis = new FileInputStream(file);
    byte[] dataBytes = new byte[1024];

    int nread = 0;
    while ((nread = fis.read(dataBytes)) != -1) {
        md.update(dataBytes, 0, nread);
    }

    byte[] mdbytes = md.digest();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mdbytes.length; i++) {
        sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

src: https://www.quora.com/How-do-I-get-the-SHA-256-hash-value-of-a-file-in-Java