Example usage for java.security DigestInputStream DigestInputStream

List of usage examples for java.security DigestInputStream DigestInputStream

Introduction

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

Prototype

public DigestInputStream(InputStream stream, MessageDigest digest) 

Source Link

Document

Creates a digest input stream, using the specified input stream and message digest.

Usage

From source file:MainClass.java

static private String computeDigest(MessageDigest algorithm, String filename) {
    String returnValue = "";
    try {/*from   w ww . ja v a 2 s  . co m*/
        algorithm.reset();
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DigestInputStream dis = new DigestInputStream(bis, algorithm);
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        StringBuffer hexString = new StringBuffer();
        byte digest[] = algorithm.digest();
        int digestLength = digest.length;
        for (int i = 0; i < digestLength; i++) {
            hexString.append(hexDigit(digest[i]));
            hexString.append(" ");
        }
        returnValue = hexString.toString();
    } catch (IOException e) {
        System.err.println("Error generating digest for: " + filename);
    }
    return returnValue;
}

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;/*w w  w.  j  a  v a 2s.c  o m*/
    try {
        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

private static String computeHash(InputStream dataStream) throws IOException, NoSuchAlgorithmException {
    MessageDigest messageDigest = null;
    DigestInputStream digestInputStream = null;
    try {//from  w w  w .j a  v a 2s .c om
        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:org.m1theo.apt.repo.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * // w  ww  .  j a  v a2s .com
 * @param hashType algorithm to be used (as {@code String})
 * @param file File to compute the digest for (as {@code File}).
 * @return A {@code String} for the hex encoded digest.
 * @throws AptRepoException
 */
public static String getDigest(String hashType, File file) throws AptRepoException {
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        MessageDigest digest = MessageDigest.getInstance(hashType);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        @SuppressWarnings("unused")
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        String hex = new String(Hex.encodeHex(digest.digest()));
        fis.close();
        bis.close();
        dis.close();
        return hex;
    } catch (NoSuchAlgorithmException e) {
        throw new AptRepoException("could not create digest", e);
    } catch (FileNotFoundException e) {
        throw new AptRepoException("could not create digest", e);
    } catch (IOException e) {
        throw new AptRepoException("could not create digest", e);
    }
}

From source file:com.codemarvels.ant.aptrepotask.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * //from   ww w  .  jav  a  2 s  . c o  m
 * @param hashType algorithm to be used (as {@code String})
 * @param file File to compute the digest for (as {@code File}).
 * @return A {@code String} for the hex encoded digest.
 * @throws MojoExecutionException
 */
public static String getDigest(String hashType, File file) {
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        MessageDigest digest = MessageDigest.getInstance(hashType);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        @SuppressWarnings("unused")
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        String hex = new String(Hex.encodeHex(digest.digest()));
        fis.close();
        bis.close();
        dis.close();
        return hex;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("could not create digest", e);
    } catch (IOException e) {
        throw new RuntimeException("could not create digest", e);
    }
}

From source file:org.apache.sling.distribution.util.impl.DigestUtils.java

public static DigestInputStream openDigestInputStream(InputStream mainInputStream, String digestAlgorithm) {
    return new DigestInputStream(mainInputStream, getDigest(digestAlgorithm));
}

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

public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {//from  ww w. ja v a2s  .  c om
        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:org.abstracthorizon.proximity.metadata.FileDigest.java

/**
 * Gets the file digest./*w w  w.j  a v  a2 s.c  o m*/
 * 
 * @param file the file
 * @param alg the alg
 * 
 * @return the file digest
 */
public static byte[] getFileDigest(File file, String alg) {
    byte[] result;
    try {
        MessageDigest dalg = MessageDigest.getInstance(alg);
        FileInputStream fis = null;
        DigestInputStream dis = null;
        try {
            fis = new FileInputStream(file);
            DevNullOutputStream fos = new DevNullOutputStream();
            dis = new DigestInputStream(fis, dalg);
            IOUtils.copy(dis, fos);
            result = dalg.digest();
        } finally {
            if (dis != null) {
                dis.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return result;
    } catch (Exception ex) {
        return null;
    }
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.MD5Checksum.java

/**
 * @param file// w  w  w  .j  av a2 s.c  om
 * @return
 * @throws Exception
 */
public static String getMD5Checksum(final File file) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    InputStream fis = new FileInputStream(file);
    try {
        //ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis);
        DigestInputStream digestInputStream = new DigestInputStream(fis, md);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = digestInputStream.read()) >= 0) {
            byteArrayOutputStream.write(ch);
        }

        byte[] newInput = byteArrayOutputStream.toByteArray();
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(newInput);
    } finally {
        fis.close();
    }
}

From source file:pl.nask.hsn2.service.Md5HashGenerator.java

private static String md5hashFromFile(BufferedInputStream bufferedInputStream) throws IOException {
    bufferedInputStream.reset();/*  ww w. j a  v  a2 s.  c o m*/
    String result = null;
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.reset();
        try (InputStream dis = new DigestInputStream(new WhiteListFileInputStream(bufferedInputStream), md)) {
            while (dis.read() != -1) {
                // Nothing to do.
            }
            char[] md5 = Hex.encodeHex(md.digest());
            result = String.valueOf(md5);
        }
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("Could not create MD5 hash for whitelisting.\n{}", e);
        result = "";
    }
    return result;
}