Example usage for java.security DigestInputStream close

List of usage examples for java.security DigestInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;// www .j  av  a2  s  . c o m
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
        i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
}

From source file:MainClass.java

public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;//from ww  w . jav  a2  s . co m
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
        i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
}

From source file:org.rhq.gui.content.YumMetadata.java

protected static String getSHA(StringBuffer primaryXML) throws NoSuchAlgorithmException, IOException {
    // Note:  For RHEL6 this may need to change to "SHA-256", 
    // going for SHA for now, as it's a lowest common denominator so it can work with older clients.
    MessageDigest md = MessageDigest.getInstance("SHA");
    ByteArrayInputStream bis = new ByteArrayInputStream(primaryXML.toString().getBytes());
    DigestInputStream mdistr = new DigestInputStream(bis, md);
    while (mdistr.read() != -1) {
        ;//from  w  w  w  .  j ava 2 s.  c  o  m
    }
    mdistr.close();
    return Hex.encodeHexString(md.digest());
}

From source file:com.stimulus.archiva.domain.EmailID.java

public static synchronized String generateUniqueID(Email email) {
    try {/*from   w w w  .  j  a  v  a 2s. c  om*/
        MimeMessage raw = email;
        // we need a backup plan here
        if (raw == null) {
            return DateUtil.convertDatetoString(new Date());
        }
        Enumeration<Header> headers = raw.getAllHeaders();
        LinkedList<String> orderedHeaders = new LinkedList<String>();
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();

            if (Compare.equalsIgnoreCase(header.getName(), "Date")
                    || Compare.equalsIgnoreCase(header.getName(), "CC")
                    || Compare.equalsIgnoreCase(header.getName(), "BCC")
                    || Compare.equalsIgnoreCase(header.getName(), "Subject")
                    || Compare.equalsIgnoreCase(header.getName(), "To")
                    || Compare.equalsIgnoreCase(header.getName(), "From"))
                orderedHeaders.add(header.getName() + header.getValue());

        }
        Collections.sort(orderedHeaders);
        StringBuffer allHeaders = new StringBuffer();
        for (String header : orderedHeaders)
            allHeaders.append(header);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] bytes = allHeaders.toString().getBytes();
        InputStream is = new ByteArrayInputStream(bytes);
        DigestInputStream dis = new DigestInputStream(is, sha);
        while (dis.read() != -1)
            ;
        dis.close();
        byte[] digest = sha.digest();
        return toHex(digest);
    } catch (Exception e) {
        logger.error("failed to generate a uniqueid for a message");
        return null;
    }
}

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

public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {/*from  ww w.  j  a v a2 s. 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:com.codemarvels.ant.aptrepotask.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * //from  w  w w  .j  av a 2s .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.m1theo.apt.repo.utils.Utils.java

/**
 * Compute the given message digest for a file.
 * /*from   w w  w . j  a v  a2s  . co  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 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:hudson.plugins.ec2.EC2AxisPrivateKey.java

static String digest(PrivateKey k) throws IOException {
    try {//  w  ww. j a v  a2  s .c  o  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:hudson.plugins.ec2.EC2PrivateKey.java

static String digestOpt(Key k, String dg) throws IOException {
    try {/*w ww  . ja v a 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:org.abstracthorizon.proximity.metadata.FileDigest.java

/**
 * Gets the file digest.//w  w w .  j a  v  a  2  s  .c  om
 * 
 * @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;
    }
}