Example usage for java.security DigestInputStream read

List of usage examples for java.security DigestInputStream read

Introduction

In this page you can find the example usage for java.security DigestInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static String calcHash(File file, String algo) throws Exception {
    MessageDigest digester = MessageDigest.getInstance(algo);

    FileInputStream is = new FileInputStream(file);
    DigestInputStream dis;
    try {/*from  w w  w . j  a  v a2 s.  c o m*/
        dis = new DigestInputStream(is, digester);

        for (byte[] buffer = new byte[1024 * 4]; dis.read(buffer) >= 0;) {
            // just read it
        }
    } finally {
        is.close();
    }

    byte[] digest = digester.digest();

    StringBuffer hash = new StringBuffer(digest.length * 2);

    for (int i = 0; i < digest.length; i++) {
        int b = digest[i] & 0xFF;

        if (b < 0x10) {
            hash.append('0');
        }

        hash.append(Integer.toHexString(b));
    }

    return hash.toString();
}

From source file:Main.java

public static String getMD5(String file) {
    String md5 = "";

    try {/*from w  ww  .  ja  v a2  s  .  com*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is;
        is = new FileInputStream(file);

        DigestInputStream dis = new DigestInputStream(is, md);
        byte data[] = new byte[1024];
        @SuppressWarnings("unused")
        int count;
        while ((count = dis.read(data)) != -1) {

        }
        byte[] digest = md.digest();

        for (int i = 0; i < digest.length; i++) {
            md5 += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
        }
        return md5;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return md5;
}

From source file:cz.muni.fi.xklinec.zipstream.Utils.java

/**
 * Computes SHA256 hash of a given file.
 * @param b/*from  w  w w . j av a  2 s  .  co  m*/
 * @return 
 */
public static String sha256(byte[] b) {
    try {
        MessageDigest sha = MessageDigest.getInstance("SHA-256");
        InputStream fis = new ByteArrayInputStream(b);
        DigestInputStream dis = new DigestInputStream(fis, sha);

        byte[] buffer = new byte[65536]; // 64kB buffer
        while (dis.read(buffer) != -1) {
        }

        byte[] hash = sha.digest();
        return new String(Base64.encode(hash));

    } catch (Exception e) {
        throw new IllegalArgumentException("Cannot compute SHA256 digest of the file", e);
    }
}

From source file:com.amazonaws.encryptionsdk.internal.TestIOUtils.java

public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {//from w ww . ja  va  2  s.co m
        final FileInputStream fis = new FileInputStream(fileName);
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        final DigestInputStream dis = new DigestInputStream(fis, md);

        final int readLen = 128;
        final byte[] readBytes = new byte[readLen];
        while (dis.read(readBytes) != -1) {
        }
        dis.close();

        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        // shouldn't get here since we hardcode the algorithm.
    }

    return null;
}

From source file:hudson.plugins.ec2.EC2AxisPrivateKey.java

static String digest(PrivateKey k) throws IOException {
    try {//from ww w.j  a  v a2  s. co m
        MessageDigest md5 = MessageDigest.getInstance("SHA1");

        DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
        try {
            while (in.read(new byte[128]) > 0)
                ; // simply discard the input
        } finally {
            in.close();
        }
        StringBuilder buf = new StringBuilder();
        char[] hex = Hex.encodeHex(md5.digest());
        for (int i = 0; i < hex.length; i += 2) {
            if (buf.length() > 0)
                buf.append(':');
            buf.append(hex, i, 2);
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

public static boolean checkMd5sum(File file, String checkCode) throws IOException {
    DigestInputStream dInput = null;
    try {//from w w w  . j  a v  a  2 s.c  om
        FileInputStream fInput = new FileInputStream(file);
        dInput = new DigestInputStream(fInput, getMd5Instance());
        byte[] buf = new byte[8192];
        while (dInput.read(buf) > 0) {
        }
        byte[] bytes = dInput.getMessageDigest().digest();
        return bytes2hex(bytes).equals(checkCode);
    } finally {
        closeQuietly(dInput);
    }
}

From source file:Main.java

private static String computeHash(InputStream dataStream) throws IOException, NoSuchAlgorithmException {
    MessageDigest messageDigest = null;
    DigestInputStream digestInputStream = null;
    try {//from www.j a va  2 s . c  o  m
        messageDigest = MessageDigest.getInstance("SHA-256");
        digestInputStream = new DigestInputStream(dataStream, messageDigest);
        byte[] byteBuffer = new byte[1024 * 8];
        while (digestInputStream.read(byteBuffer) != -1)
            ;
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
            if (dataStream != null)
                dataStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    byte[] hash = messageDigest.digest();
    return String.format("%064x", new java.math.BigInteger(1, hash));
}

From source file:hudson.plugins.ec2.EC2PrivateKey.java

static String digestOpt(Key k, String dg) throws IOException {
    try {/*from   w  w  w.j  a va  2  s. c o  m*/
        MessageDigest md5 = MessageDigest.getInstance(dg);

        DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
        try {
            while (in.read(new byte[128]) > 0)
                ; // simply discard the input
        } finally {
            in.close();
        }
        StringBuilder buf = new StringBuilder();
        char[] hex = Hex.encodeHex(md5.digest());
        for (int i = 0; i < hex.length; i += 2) {
            if (buf.length() > 0)
                buf.append(':');
            buf.append(hex, i, 2);
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

public static String md5(InputStream in) {
    int bufferSize = 256 * 1024;
    DigestInputStream digestInputStream = null;
    try {/* w  w  w .  java 2s  .c o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        digestInputStream = new DigestInputStream(in, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0)
            ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String encode(InputStream in) {
    int bufferSize = 256 * 1024;
    DigestInputStream digestInputStream = null;
    try {//from w ww .  j a va  2 s. c  o m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        digestInputStream = new DigestInputStream(in, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0)
            ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}