Search in sources :

Example 76 with DigestInputStream

use of java.security.DigestInputStream in project ceylon-compiler by ceylon.

the class Main method showClass.

/**
 * Display the location and checksum of a class.
 */
void showClass(String className) {
    out.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        out.println("  class not found");
    else {
        out.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do {
                    n = in.read(buf);
                } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) sb.append(String.format("%02x", b));
            out.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            out.println("  cannot compute digest: " + e);
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) MessageDigest(java.security.MessageDigest) URL(java.net.URL) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException)

Example 77 with DigestInputStream

use of java.security.DigestInputStream in project mycore by MyCoRe-Org.

the class MCRSwordUtil method createTempFileFromStream.

/**
 * Stores stream to temp file and checks md5
 *
 * @param inputStream the stream which holds the File
 * @param checkMd5    the md5 to compare with (or null if no md5 check is needed)
 * @return the path to the temp file
 * @throws IOException if md5 does mismatch or if stream could not be read
 */
public static Path createTempFileFromStream(String fileName, InputStream inputStream, String checkMd5) throws IOException {
    MCRSession currentSession = MCRSessionMgr.getCurrentSession();
    if (currentSession.isTransactionActive()) {
        currentSession.commitTransaction();
    }
    final Path zipTempFile = Files.createTempFile("swordv2_", fileName);
    MessageDigest md5Digest = null;
    if (checkMd5 != null) {
        try {
            md5Digest = MessageDigest.getInstance("MD5");
            inputStream = new DigestInputStream(inputStream, md5Digest);
        } catch (NoSuchAlgorithmException e) {
            currentSession.beginTransaction();
            throw new MCRConfigurationException("No MD5 available!", e);
        }
    }
    Files.copy(inputStream, zipTempFile, StandardCopyOption.REPLACE_EXISTING);
    if (checkMd5 != null) {
        final String md5String = MCRUtils.toHexString(md5Digest.digest());
        if (!md5String.equals(checkMd5)) {
            currentSession.beginTransaction();
            throw new IOException("MD5 mismatch, expected " + checkMd5 + " got " + md5String);
        }
    }
    currentSession.beginTransaction();
    return zipTempFile;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRSession(org.mycore.common.MCRSession) DigestInputStream(java.security.DigestInputStream) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 78 with DigestInputStream

use of java.security.DigestInputStream in project AndroidUtilCode by Blankj.

the class EncryptUtils method encryptMD5File.

/**
 * Return the bytes of file's MD5 encryption.
 *
 * @param file The file.
 * @return the bytes of file's MD5 encryption
 */
public static byte[] encryptMD5File(final File file) {
    if (file == null)
        return null;
    FileInputStream fis = null;
    DigestInputStream digestInputStream;
    try {
        fis = new FileInputStream(file);
        MessageDigest md = MessageDigest.getInstance("MD5");
        digestInputStream = new DigestInputStream(fis, md);
        byte[] buffer = new byte[256 * 1024];
        while (true) {
            if (!(digestInputStream.read(buffer) > 0))
                break;
        }
        md = digestInputStream.getMessageDigest();
        return md.digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 79 with DigestInputStream

use of java.security.DigestInputStream in project zm-mailbox by Zimbra.

the class ContentAddressableStoreManager method stage.

protected StagedBlob stage(InputStream in, long actualSize, Mailbox mbox, String locator) throws ServiceException {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("SHA-256 digest not found", e);
    }
    ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));
    try {
        writeStreamToStore(pin, actualSize, mbox, locator);
        return new ExternalStagedBlob(mbox, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), locator);
    } catch (IOException e) {
        throw ServiceException.FAILURE("unable to stage blob", e);
    }
}
Also used : ByteUtil(com.zimbra.common.util.ByteUtil) DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 80 with DigestInputStream

use of java.security.DigestInputStream in project box-java-sdk by box.

the class LargeFileUpload method generateDigest.

/**
 * Generates the Base64 encoded SHA-1 hash for content available in the stream.
 * It can be used to calculate the hash of a file.
 *
 * @param stream the input stream of the file or data.
 * @return the Base64 encoded hash string.
 */
public String generateDigest(InputStream stream) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(DIGEST_ALGORITHM_SHA1);
    } catch (NoSuchAlgorithmException ae) {
        throw new BoxAPIException("Digest algorithm not found", ae);
    }
    // Calcuate the digest using the stream.
    DigestInputStream dis = new DigestInputStream(stream, digest);
    try {
        int value = dis.read();
        while (value != -1) {
            value = dis.read();
        }
    } catch (IOException ioe) {
        throw new BoxAPIException("Reading the stream failed.", ioe);
    }
    // Get the calculated digest for the stream
    byte[] digestBytes = digest.digest();
    return Base64.encode(digestBytes);
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Aggregations

DigestInputStream (java.security.DigestInputStream)179 MessageDigest (java.security.MessageDigest)138 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)84 IOException (java.io.IOException)74 InputStream (java.io.InputStream)57 FileInputStream (java.io.FileInputStream)41 ByteArrayInputStream (java.io.ByteArrayInputStream)40 File (java.io.File)21 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileOutputStream (java.io.FileOutputStream)8 URL (java.net.URL)8 OutputStream (java.io.OutputStream)7 FileNotFoundException (java.io.FileNotFoundException)5 BigInteger (java.math.BigInteger)5 DigestOutputStream (java.security.DigestOutputStream)5 HashMap (java.util.HashMap)5 Path (java.nio.file.Path)4 CertificateFactory (java.security.cert.CertificateFactory)4 Formatter (java.util.Formatter)4