Search in sources :

Example 26 with DigestInputStream

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

the class DigestInputStream2Test method test_onZ.

/**
     * java.security.DigestInputStream#on(boolean)
     */
public void test_onZ() throws Exception {
    // Test for method void java.security.DigestInputStream.on(boolean)
    MessageDigest originalDigest = (MessageDigest) (digest.clone());
    MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
    DigestInputStream dis = new DigestInputStream(inStream, noChangeDigest);
    // turn off processing
    dis.on(false);
    // read some data
    int c = dis.read();
    assertEquals('T', c);
    // make sure the digest for the part where it was off has not
    // changed
    assertTrue("MessageDigest changed even though processing was off", MessageDigest.isEqual(noChangeDigest.digest(), originalDigest.digest()));
    MessageDigest changeDigest = (MessageDigest) (digest.clone());
    dis = new DigestInputStream(inStream, digest);
    // turn on processing
    dis.on(true);
    c = dis.read();
    assertEquals('h', c);
    // make sure the digest has changed
    assertTrue("MessageDigest did not change with processing on", !MessageDigest.isEqual(digest.digest(), changeDigest.digest()));
}
Also used : DigestInputStream(java.security.DigestInputStream) MessageDigest(java.security.MessageDigest)

Example 27 with DigestInputStream

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

the class DigestInputStream2Test method test_setMessageDigestLjava_security_MessageDigest.

/**
     * java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
     */
public void test_setMessageDigestLjava_security_MessageDigest() {
    // Test for method void
    // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
    DigestInputStream dis = new DigestInputStream(inStream, null);
    // make sure the digest is null when it's not been set
    assertNull("Uninitialised MessageDigest should have been returned as null", dis.getMessageDigest());
    dis.setMessageDigest(digest);
    assertEquals("Wrong MessageDigest was returned.", digest, dis.getMessageDigest());
}
Also used : DigestInputStream(java.security.DigestInputStream)

Example 28 with DigestInputStream

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

the class DigestInputStreamTest method testDigestInputStream02.

/**
     * Test #2 for <code>DigestInputStream</code> constructor<br>
     *
     * Assertion: creates new <code>DigestInputStream</code> instance
     * using valid parameters (both <code>null</code>)
     */
public final void testDigestInputStream02() {
    InputStream dis = new DigestInputStream(null, null);
    assertTrue(dis instanceof DigestInputStream);
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream)

Example 29 with DigestInputStream

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

the class SQLiteLibraryLoader method md5sum.

private String md5sum(InputStream input) throws IOException {
    BufferedInputStream in = new BufferedInputStream(input);
    try {
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        DigestInputStream digestInputStream = new DigestInputStream(in, digest);
        while (digestInputStream.read() >= 0) ;
        ByteArrayOutputStream md5out = new ByteArrayOutputStream();
        md5out.write(digest.digest());
        return new BigInteger(md5out.toByteArray()).toString();
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("MD5 algorithm is not available: " + e);
    } finally {
        in.close();
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) BufferedInputStream(java.io.BufferedInputStream) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 30 with DigestInputStream

use of java.security.DigestInputStream in project AndroidUtilLib by SiberiaDante.

the class SDFileUtil 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 (true) {
            if (!(dis.read(buffer) > 0))
                break;
        }
        md = dis.getMessageDigest();
        return md.digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    } finally {
        SDCloseUtil.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)

Aggregations

DigestInputStream (java.security.DigestInputStream)161 MessageDigest (java.security.MessageDigest)124 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 IOException (java.io.IOException)62 InputStream (java.io.InputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)38 FileInputStream (java.io.FileInputStream)34 File (java.io.File)19 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileOutputStream (java.io.FileOutputStream)8 URL (java.net.URL)7 OutputStream (java.io.OutputStream)6 BigInteger (java.math.BigInteger)5 DigestOutputStream (java.security.DigestOutputStream)5 HashMap (java.util.HashMap)5 FileNotFoundException (java.io.FileNotFoundException)4 Formatter (java.util.Formatter)4 ByteUtil (com.zimbra.common.util.ByteUtil)3 Path (java.nio.file.Path)3