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 :
d05a9724479af8a7a20f7acff3629b0dd8aaa447ec821b4b31708bc43eb99841
but when the checksum is calculated for for same file using Linux command ‘sha256sum <file>
‘ then it gives
37f865eb2a3956e3f903eaaf92f0459143cf9b9699a7d1d6bc91bfc89ad5eb8c
Below is what the file contain. You can create file with below content. Am using abc.txt. It has newlines.
dscscsdvcs kkl
Can some suggest what’s going wrong here? File is not corrupted. Contents are identical at source and destination.
Advertisement
Answer
I was able to get both the digests from two different files. See their hexdumps:
d05a9724479af8a7a20f7acff3629b0dd8aaa447ec821b4b31708bc43eb99841:
00000000: 6473 6373 6373 6476 6373 0d0a 0d0a 6b6b dscscsdvcs....kk 00000010: 6c l
37f865eb2a3956e3f903eaaf92f0459143cf9b9699a7d1d6bc91bfc89ad5eb8c:
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.