Java SHA1 sha1Hash(InputStream in)

Here you can find the source of sha1Hash(InputStream in)

Description

sha Hash

License

Open Source License

Declaration

public static String sha1Hash(InputStream in) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String sha1Hash(InputStream in) throws IOException {
        return hash(in, "SHA1");
    }/*from w  w  w  .j  ava2  s. c  om*/

    public static String hash(File f, String algo) throws IOException {
        return hash(new BufferedInputStream(new FileInputStream(f), 65536),
                algo);
    }

    public static String hash(File f, MessageDigest md) throws IOException {
        return hash(new BufferedInputStream(new FileInputStream(f), 65536),
                md);
    }

    public static String hash(InputStream in, String algo)
            throws IOException {
        try {
            MessageDigest md = MessageDigest.getInstance(algo);
            return hash(in, md);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public final static String hash(final InputStream in,
            final MessageDigest md) throws IOException {
        try {
            boolean done = false;
            final byte[] buf = new byte[65536];
            while (!done) {
                final int length = in.read(buf);
                if (length <= 0) {
                    done = true;
                } else {
                    md.update(buf, 0, length);
                }
            }
            return convertToHex(md.digest());
        } finally {
            in.close();
        }
    }

    public static String convertToHex(final byte[] buf) {
        if (buf == null) {
            return null;
        }
        final StringBuffer out = new StringBuffer();
        final int n = buf.length;
        for (int i = 0; i < n; i++) {
            out.append(convertLowerBitsToHex((buf[i] >> 4) & 0x0f));
            out.append(convertLowerBitsToHex(buf[i] & 0x0f));
        }
        return out.toString();
    }

    private static char convertLowerBitsToHex(int b) {
        switch (b) {
        case 0:
            return '0';
        case 1:
            return '1';
        case 2:
            return '2';
        case 3:
            return '3';
        case 4:
            return '4';
        case 5:
            return '5';
        case 6:
            return '6';
        case 7:
            return '7';
        case 8:
            return '8';
        case 9:
            return '9';
        case 10:
            return 'a';
        case 11:
            return 'b';
        case 12:
            return 'c';
        case 13:
            return 'd';
        case 14:
            return 'e';
        case 15:
            return 'f';
        }
        throw new IllegalArgumentException(
                "can't convert to hex character: " + b);
    }
}

Related

  1. sha1Hash(byte[] data)
  2. sha1hash(Collection nquads)
  3. sha1hash(File f)
  4. sha1Hash(File file)
  5. sha1Hash(final String tohash)
  6. sha1Hash(String content)
  7. sha1Hash(String input)
  8. sha1Hash(String input)
  9. sha1hash(String key)