How to get a file as a checksum MD5, and SHA1 at the same time when downloading a new file?

I am working on a storage system. Users upload files to the server.

On the server side, I want to implement a program to obtain file checksums using both MD5 and SHA1.

I know how to calculate checksums using the DigestInputStream functions, but it seems to support only one method (either MD5 or SHA1). How can I calculate both MD5 and SHA1 at the same time while working with the load stream in JAVA?

Thanks guys,

+2
source share
2 answers

Use two instances of MessageDigest (one for MD5 and one for SHA1) and load the bytes you read into both.

+3
source

as the java-ish pseudo-code, since you can independently search the API for OpenSSL or BSafe or the Java Crypto API ...

  Buffered reader = ...;
 char [MY_ARRAY_SIZE] buf = ...;

 while (true) {
   int count = reader.read (buf, 0, buf.length);
   if (count == -1) {break};

   / * You'll need to check for the right API and handle errors yourself * /
   md5.add (buf, count);
   sha256.add (buf, count);
 }

 String md5sum = base64 (md5.finalize ());  // assumes an appropriate base64 method
 String sha256sum = base64 (sha256.finalize ());

+1
source

All Articles