Search in sources :

Example 56 with DigestInputStream

use of java.security.DigestInputStream in project robovm by robovm.

the class DigestInputStreamTest method testRead02.

/**
     * Test #2 for <code>read()</code> method<br>
     *
     * Assertion: returns -1 if EOS had been
     * reached but not read before method call<br>
     *
     * Assertion: must not update digest if EOS had been
     * reached but not read before method call<br>
     */
public final void testRead02() throws IOException {
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            for (int i = 0; i < MY_MESSAGE_LEN; i++) {
                dis.read();
            }
            // check that subsequent read() calls return -1 (eos)
            assertEquals("retval1", -1, dis.read());
            assertEquals("retval2", -1, dis.read());
            assertEquals("retval3", -1, dis.read());
            // check that 3 previous read() calls did not update digest
            assertTrue("update", Arrays.equals(dis.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[ii])));
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 57 with DigestInputStream

use of java.security.DigestInputStream in project routerkeygenAndroid by routerkeygen.

the class HashUtils method checkDicMD5.

// Check RouterKeygen.dic file through md5
public static boolean checkDicMD5(String dicFile, final byte[] expected) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(dicFile);
        try {
            is = new DigestInputStream(is, md);
            while (is.read() != -1) {
            }
        } finally {
            is.close();
        }
        final byte[] hash = md.digest();
        return Arrays.equals(hash, expected);
    } catch (Exception e) {
        return false;
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) DigestInputStream(java.security.DigestInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 58 with DigestInputStream

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

the class FileUtils method getFileMD5.

/**
     * 获取文件的MD5校验码
     *
     * @param file 文件
     * @return 文件的MD5校验码
     */
public static byte[] getFileMD5(File file) {
    if (file == null)
        return null;
    DigestInputStream dis = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        MessageDigest md = MessageDigest.getInstance("MD5");
        dis = new DigestInputStream(fis, md);
        byte[] buffer = new byte[1024 * 256];
        while (dis.read(buffer) > 0) ;
        md = dis.getMessageDigest();
        return md.digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    } finally {
        CloseUtils.closeIO(dis);
    }
    return null;
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 59 with DigestInputStream

use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.

the class TestDigestIOStream method testMDShare.

/**
     * Test DigestInputStream and DigestOutputStream digest function when use
     * same message digest object.
     *
     * @param algo
     *            Message Digest algorithm
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testMDShare(String algo, int dataLength) throws Exception {
    MessageDigest mdCommon = MessageDigest.getInstance(algo);
    // Generate the DigestInputStream/DigestOutputStream object
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, mdCommon);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, mdCommon)) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[10];
        // use both read() and read(byte[], int, int)
        while (k < data.length) {
            int len = dis.read(buffer, 0, buffer.length);
            if (len != -1) {
                k += len;
                if (k < data.length) {
                    dos.write(data[k]);
                    k++;
                    dis.skip(1);
                }
            }
        }
        // Get the output and the "correct" digest values
        byte[] output = mdCommon.digest();
        byte[] standard = md.digest(data);
        // Compare generated digest values
        return MessageDigest.isEqual(output, standard);
    } catch (Exception ex) {
        out.println("TestMDShare failed at:" + algo + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest)

Example 60 with DigestInputStream

use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.

the class CipherStreamClose method streamDecrypt.

public static Object streamDecrypt(byte[] data, SecretKey key, MessageDigest digest) throws Exception {
    Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    decCipher.init(Cipher.DECRYPT_MODE, key);
    digest.reset();
    try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
        try (ObjectInputStream ois = new ObjectInputStream(cis)) {
            return ois.readObject();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) CipherInputStream(javax.crypto.CipherInputStream) Cipher(javax.crypto.Cipher)

Aggregations

DigestInputStream (java.security.DigestInputStream)75 MessageDigest (java.security.MessageDigest)54 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 InputStream (java.io.InputStream)26 IOException (java.io.IOException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 FileInputStream (java.io.FileInputStream)12 File (java.io.File)11 BufferedInputStream (java.io.BufferedInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DigestOutputStream (java.security.DigestOutputStream)5 HashMap (java.util.HashMap)5 ByteUtil (com.zimbra.common.util.ByteUtil)3 BigInteger (java.math.BigInteger)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Entry (java.util.Map.Entry)3 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)2