Java SHA1 sha1sum(InputStream file)

Here you can find the source of sha1sum(InputStream file)

Description

shasum

License

Open Source License

Declaration

public static String sha1sum(InputStream file) 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.math.BigInteger;

import java.security.MessageDigest;

public class Main {
    public static String sha1sum(InputStream file) {
        InputStream in = file;/*from   w w  w .  j  av a2 s  .c  o  m*/

        try {
            MessageDigest digest = MessageDigest.getInstance("SHA1");
            byte[] buffer = new byte[8192];
            int read = 0;

            while ((read = in.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }

            byte[] sha1sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, sha1sum);
            return bigInt.toString(16);
        } catch (Exception e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
                }
        }
    }
}

Related

  1. sha1sum(byte[] input, int length)
  2. sha1sum(File file)
  3. sha1sum(File file)
  4. SHA1Sum(final @Nonnull byte[] bytes)
  5. sha1sum(final String data)
  6. sha1ToHex(String input, String encoding)
  7. sha1Utf8(String clearText)