Search in sources :

Example 91 with DigestInputStream

use of java.security.DigestInputStream in project UltimateAndroid by cymcsg.

the class CryptographyUtils method getMd5FromFile.

/**
     * Get the MD5 of the file
     *
     * @param filePath
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
public static String getMd5FromFile(String filePath) throws IOException, NoSuchAlgorithmException {
    int bufferSize = 256 * 1024;
    FileInputStream fileInputStream = null;
    DigestInputStream digestInputStream = null;
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        fileInputStream = new FileInputStream(filePath);
        digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0) ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        return byteArrayToHex(resultByteArray);
    } catch (NoSuchAlgorithmException e) {
        throw e;
    } finally {
        if (digestInputStream != null) {
            digestInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 92 with DigestInputStream

use of java.security.DigestInputStream in project android_frameworks_base by ParanoidAndroid.

the class ManifestDigest method fromInputStream.

static ManifestDigest fromInputStream(InputStream fileIs) {
    if (fileIs == null) {
        return null;
    }
    final MessageDigest md;
    try {
        md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(DIGEST_ALGORITHM + " must be available", e);
    }
    final DigestInputStream dis = new DigestInputStream(new BufferedInputStream(fileIs), md);
    try {
        byte[] readBuffer = new byte[8192];
        while (dis.read(readBuffer, 0, readBuffer.length) != -1) {
        // not using
        }
    } catch (IOException e) {
        Slog.w(TAG, "Could not read manifest");
        return null;
    } finally {
        IoUtils.closeQuietly(dis);
    }
    final byte[] digest = md.digest();
    return new ManifestDigest(digest);
}
Also used : DigestInputStream(java.security.DigestInputStream) BufferedInputStream(java.io.BufferedInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 93 with DigestInputStream

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

the class DigestInputStreamTest method testReadbyteArrayintint01.

/**
     * Test #1 for <code>read(byte[],int,int)</code> method<br>
     *
     * Assertion: returns the number of bytes read<br>
     *
     * Assertion: put bytes read into specified array at specified offset<br>
     *
     * Assertion: updates associated digest<br>
     */
public final void testReadbyteArrayintint01() 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);
            byte[] bArray = new byte[MY_MESSAGE_LEN];
            // check that read(byte[],int,int) returns valid value
            assertTrue("retval", dis.read(bArray, 0, bArray.length) == MY_MESSAGE_LEN);
            // check that bArray has been filled properly
            assertTrue("bArray", Arrays.equals(myMessage, bArray));
            // check that associated digest has been updated properly
            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 94 with DigestInputStream

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

the class DigestInputStreamTest method testRead05.

/**
     * Test #5 for <code>read()</code> method<br>
     *
     * Assertion: broken <code>DigestInputStream</code>instance:
     * associated <code>MessageDigest</code> not set.
     * <code>read()</code> must not work when digest
     * functionality is on
     */
public final void testRead05() {
    InputStream is = new ByteArrayInputStream(myMessage);
    DigestInputStream dis = new DigestInputStream(is, null);
    // must result in an exception
    try {
        for (int i = 0; i < MY_MESSAGE_LEN; i++) {
            dis.read();
        }
        fail("read() must not work when digest functionality is on");
    } catch (Exception e) {
    // Expected.
    }
}
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) IOException(java.io.IOException)

Example 95 with DigestInputStream

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

the class DigestInputStreamTest method testRead04.

/**
     * Test #4 for <code>read()</code> method<br>
     *
     * Assertion: broken <code>DigestInputStream</code>instance:
     * <code>InputStream</code> not set. <code>read()</code> must
     * not work
     */
public final void testRead04() throws IOException {
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            DigestInputStream dis = new DigestInputStream(null, md);
            // must result in an exception
            try {
                for (int i = 0; i < MY_MESSAGE_LEN; i++) {
                    dis.read();
                }
            } catch (Exception e) {
                // Expected.
                return;
            }
            fail("InputStream not set. read() must not work");
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

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