Skip to content

Sha256 checksum difference between Java vs Linux

Am writing a application which calculate the checksum of file before sending it to downstream. At downstream application, linux shell script are used to process the files. It is noticed that checksum calculated from Java application doesn’t matches that calculated using Linux command ‘sha256sum’.

Below is code snippet in JAVA:

public static String calculateChecksum(File file) throws IOException {
    try (InputStream inputStream = FileUtils.openInputStream(file)) {
        return DigestUtils.sha256Hex(inputStream); //from org.apache.commons.codec.digest

Using java api i get checksum value as :


but when the checksum is calculated for for same file using Linux command ‘sha256sum <file>‘ then it gives


Below is what the file contain. You can create file with below content. Am using abc.txt. It has newlines.



Can some suggest what’s going wrong here? File is not corrupted. Contents are identical at source and destination.


I was able to get both the digests from two different files. See their hexdumps:


00000000: 6473 6373 6373 6476 6373 0d0a 0d0a 6b6b  dscscsdvcs....kk
00000010: 6c                                       l


00000000: 6473 6373 6373 6476 6373 0a0a 6b6b 6c    dscscsdvcs..kkl

The difference is end of line type, *nix versus MSWin.

The size of the files is not the same, it’s 15 versus 17 bytes.

Java gives the same results.