Search in sources :

Example 6 with DigestOutputStream

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

the class CipherStreamClose method streamEncrypt.

public static byte[] streamEncrypt(String message, SecretKey key, MessageDigest digest) throws Exception {
    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }
    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DigestOutputStream(java.security.DigestOutputStream) Cipher(javax.crypto.Cipher)

Example 7 with DigestOutputStream

use of java.security.DigestOutputStream in project jackrabbit-oak by apache.

the class ConsolidatedDataStoreStatsTest method getIdForInputStream.

private String getIdForInputStream(final InputStream in) throws Exception {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    OutputStream output = new DigestOutputStream(new NullOutputStream(), digest);
    try {
        IOUtils.copyLarge(in, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
    }
    return encodeHexString(digest.digest());
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 8 with DigestOutputStream

use of java.security.DigestOutputStream in project jackrabbit-oak by apache.

the class CachingDataStoreTest method getIdForInputStream.

private String getIdForInputStream(File f) throws Exception {
    FileInputStream in = null;
    OutputStream output = null;
    try {
        in = new FileInputStream(f);
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        output = new DigestOutputStream(new NullOutputStream(), digest);
        IOUtils.copyLarge(in, output);
        return encodeHexString(digest.digest());
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 9 with DigestOutputStream

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

the class DigestOutputStreamTest method test_write$BII_2.

/**
     * Test #2 for <code>write(byte[],int,int)</code> method<br>
     *
     * Assertion: put bytes into output stream<br>
     *
     * Assertion: updates associated digest<br>
     */
public final void test_write$BII_2() throws IOException {
    // check precondition
    assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
    for (int k = 0; k < algorithmName.length; k++) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
            MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
            DigestOutputStream dos = new DigestOutputStream(bos, md);
            // write message by chunks
            for (int i = 0; i < MY_MESSAGE_LEN / CHUNK_SIZE; i++) {
                dos.write(myMessage, i * CHUNK_SIZE, CHUNK_SIZE);
            }
            // check write
            assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
            // check that associated digest has been updated properly
            assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[k])));
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 10 with DigestOutputStream

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

the class DigestOutputStreamTest method testWriteint05.

/**
     * Test #5 for <code>write(int)</code> method<br>
     * Test #2 for <code>on(boolean)</code> method<br>
     *
     * Assertion: broken <code>DigestOutputStream</code>instance:
     * associated <code>MessageDigest</code> not set.
     * <code>write(int)</code> must work when digest
     * functionality is off
     */
public final void testWriteint05() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
    DigestOutputStream dos = new DigestOutputStream(bos, null);
    // set digest functionality to off
    dos.on(false);
    // the following must pass without any exception
    for (int i = 0; i < MY_MESSAGE_LEN; i++) {
        dos.write(myMessage[i]);
    }
    // check that bytes have been written correctly
    assertTrue(Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)106 MessageDigest (java.security.MessageDigest)86 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)44 IOException (java.io.IOException)41 OutputStream (java.io.OutputStream)26 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)13 Support_OutputStream (tests.support.Support_OutputStream)9 NullOutputStream (com.keepassdroid.stream.NullOutputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 NullOutputStream (org.apache.commons.io.output.NullOutputStream)7 Base64OutputStream (android.util.Base64OutputStream)6 DigestInputStream (java.security.DigestInputStream)6 DataOutputStream (java.io.DataOutputStream)5 FileInputStream (java.io.FileInputStream)5 Attributes (java.util.jar.Attributes)5