As the title says, I had the opportunity to "find the MD5 checksum of a file in Java and get the result as a character string in hexadecimal notation", and I struggled to implement it, so this is a memo. Basically, it is recommended to use some kind of external library (´ ・ ω ・ `)
First, generate the file for which you want the md5 checksum using the following one-liner.
seq 5 | while read x; do cat /dev/urandom | tr -cd 'a-zA-Z0-9' | fold -s5 | xargs -n5 | head -n5 > "sample${x}.txt"; done
This creates 5 files with randomly arranged alphanumeric characters, and the contents are as follows.
$ find sample*.txt | while read path; do echo "===> ${path} <==="; cat ${path}; done
===> sample1.txt <===
U959b Xxs05 jZ0XH 7Wrew MRmZB
cJlj3 dCEZm qnjbH yy9QM ntuEy
VXKle Se9uc i5UxP mppHQ LnOL2
SNAXl oPGWk JYK7S ovkov JXnJ6
GBlSy frjGp 1wSZ4 Zoiqx ynuaM
===> sample2.txt <===
aSl1p ZNcQt EecBl ie3ox wihqz
svMsD 88Lar BzxXw IfdQT 0QNHI
W3SD4 NQM76 5bZh5 BFhQs G7lmt
LkNq4 cCfMR yOMqX c4AxQ LL7KG
OuOPK GMbH4 IjiGf OFfUK McT3b
===> sample3.txt <===
Eqmhh ioKSo AEMb8 Kdgo8 deFxc
ASilF deSpC 0TIzI ZGM17 OA932
hSGXL LCy8q DDYAC 9hTiF nsDqH
LQRZb k53VV 03NCY RhKjF tVggB
JHmOx wilgw QZov5 StZ2g 9PGjn
===> sample4.txt <===
0Dx9Z 0Md5q w2cUT hp8gq rHIYB
WD6X5 yjyiX DNNyY ZN9Yu IEEkc
P6LnO Zlxwq wSopY Ke4KH oBAWh
3REUX sMPCU 50VYL X14A4 UWuMy
6bttJ 7DuHi i1zxV WlbAS LFANI
===> sample5.txt <===
8wOUf 80uxv zAHmA k9Yml knvKl
o61Cs CREZm APeOv 0wL1w mNIVs
gWELy HtwT4 MWdgT 8VRqs drYhR
6UJvA koZSX qNL9w elNTS CKKne
r2Jop WlMWG 1Qwep 5yvBS 2dtCx
Then, for the 5 generated files, use the md5sum
command to find the MD5 checksum, and you should see something like this:
$ md5sum sample*.txt
6777566a705dd85f1f0c2968bfcab2c8 sample1.txt
be520478d070207d07a3335d0ee2e2a4 sample2.txt
4c47ca20b16046a13dbe50869bba368e sample3.txt
48ca253e12841800cf898ab090a04a0e sample4.txt
b378511f4b8878c338d602e5376f8426 sample5.txt
Although the introduction has become long, the Java code that asks for the md5 checksum of the main subject is as follows.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
var filenames = List.of(
"sample1.txt",
"sample2.txt",
"sample3.txt",
"sample4.txt",
"sample5.txt"
);
for (var filename : filenames) {
var md5 = md5sum(Paths.get(filename));
System.out.printf("%s %s%n", md5, filename);
}
}
public static String md5sum(Path path) throws NoSuchAlgorithmException, IOException {
var digest = MessageDigest.getInstance("MD5");
try (var input = new DigestInputStream(Files.newInputStream(path), digest)) {
while (input.read() != -1) {
// PASS;
}
var hash = new StringBuilder();
for (byte b : digest.digest()) {
hash.append(String.format("%02x", b));
}
return hash.toString();
}
}
}
If you do this, you will get output similar to the following: Since it matches the result of the md5sum
command, it can be said that it was able to be obtained correctly (´ ・ ω ・ `).
6777566a705dd85f1f0c2968bfcab2c8 sample1.txt
be520478d070207d07a3335d0ee2e2a4 sample2.txt
4c47ca20b16046a13dbe50869bba368e sample3.txt
48ca253e12841800cf898ab090a04a0e sample4.txt
b378511f4b8878c338d602e5376f8426 sample5.txt
Also unconfirmed, the part of the above Java code that says var digest = MessageDigest.getInstance ("MD5");
is replaced withvar digest = MessageDigest.getInstance ("SHA-1");
andvar digest = MessageDigest.getInstance. If you change it to ("SHA-256");
, you can also get the SHA-1 and SHA-2 checksums.
Recommended Posts