How to calculate SHA512 of a file?
Asked Answered
A

4

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

Audette answered 30/7, 2015 at 19:14 Comment(5)
Well, how do you calculate an SHA512 hash (or a 'message digest') in Java? Apply it to a file.Eustashe
Or, with a little searching .. #16051327 , #304768Eustashe
@Eustashe - 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.Audette
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.Eustashe
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.Audette
P
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().

Punke answered 30/7, 2015 at 19:28 Comment(0)
B
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.

Bathometer answered 30/7, 2015 at 19:21 Comment(1)
getInstance("SHA512") doesn't work for me. getInstance("SHA-512") does. (Note the dash)Dominick
S
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));
}
Subjection answered 8/5, 2020 at 10:18 Comment(0)
Z
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

Zounds answered 8/2, 2019 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.