Search in sources :

Example 11 with DigestOutputStream

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

the class DigestOutputStreamTest method test_write$BII_4.

/**
     * Test #4 for <code>write(byte[],int,int)</code> method<br>
     *
     * Assertion: put bytes into output stream<br>
     *
     * Assertion: does not update associated digest if digest
     * functionality is off<br>
     */
public final void test_write$BII_4() throws NoSuchAlgorithmException, 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);
            // set digest functionality off
            dos.on(false);
            // 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 not been updated
            assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[k] + "_NU")));
            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 12 with DigestOutputStream

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

the class DigestOutputStreamTest method test_write$BII_6.

/**
     * java.security.DigestOutputStream#write(byte[], int, int)
     */
public void test_write$BII_6() throws Exception {
    // Regression form HARMONY-1091.
    MessageDigest md = new MyMessageDigest1();
    byte[] bytes = new byte[] { 1, 2 };
    DigestOutputStream dig = new DigestOutputStream(new ByteArrayOutputStream(), md);
    // buf == null
    try {
        dig.write(null, -1, 0);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    // offset + len > buf.length
    try {
        dig.write(bytes, 0, bytes.length + 1);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    // offset < 0
    try {
        dig.write(bytes, -1, 1);
        fail("No expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    // len < 0
    try {
        dig.write(bytes, 0, -1);
        fail("No expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) MyMessageDigest1(org.apache.harmony.security.tests.support.MyMessageDigest1) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest)

Example 13 with DigestOutputStream

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

the class DigestOutputStreamTest method testWriteint01.

/**
     * Test #1 for <code>write(int)</code> method<br>
     *
     * Assertion: writes the byte to the output stream<br>
     * Assertion: updates associated digest<br>
     */
public final void testWriteint01() throws IOException {
    for (int k = 0; k < algorithmName.length; k++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
            DigestOutputStream dos = new DigestOutputStream(bos, md);
            for (int i = 0; i < MY_MESSAGE_LEN; i++) {
                dos.write(myMessage[i]);
            }
            // check that bytes have been written correctly
            assertTrue("write", Arrays.equals(MDGoldenData.getMessage(), 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 14 with DigestOutputStream

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

the class DigestOutputStreamTest method testOn.

/**
     * Test for <code>on()</code> method<br>
     * Assertion: turns digest functionality on or off
     */
public final void testOn() throws IOException {
    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);
            // turn digest off
            dos.on(false);
            for (int i = 0; i < MY_MESSAGE_LEN - 1; i++) {
                dos.write(myMessage[i]);
            }
            // turn digest on
            dos.on(true);
            // read remaining byte
            dos.write(myMessage[MY_MESSAGE_LEN - 1]);
            byte[] digest = dos.getMessageDigest().digest();
            // check that digest value has been
            // updated by the last write(int) call
            assertFalse(Arrays.equals(digest, MDGoldenData.getDigest(algorithmName[k])));
            assertFalse(Arrays.equals(digest, MDGoldenData.getDigest(algorithmName[k] + "_NU")));
            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 15 with DigestOutputStream

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

the class DigestOutputStreamTest method test_onZ.

/**
     * java.security.DigestOutputStream#on(boolean)
     */
public void test_onZ() throws Exception {
    // Test for method void java.security.DigestOutputStream.on(boolean)
    DigestOutputStream dos = new DigestOutputStream(new ByteArrayOutputStream(), MessageDigest.getInstance("SHA"));
    dos.on(false);
    byte[] digestArray = { 23, 43, 44 };
    dos.write(digestArray, 1, 1);
    byte[] digestResult = dos.getMessageDigest().digest();
    byte[] expected = { -38, 57, -93, -18, 94, 107, 75, 13, 50, 85, -65, -17, -107, 96, 24, -112, -81, -40, 7, 9 };
    assertTrue("Digest did not return expected result.", Arrays.equals(digestResult, expected));
    // now turn on processing and re-run
    dos.on(true);
    dos.write(digestArray, 1, 1);
    digestResult = dos.getMessageDigest().digest();
    byte[] expected1 = { -87, 121, -17, 16, -52, 111, 106, 54, -33, 107, -118, 50, 51, 7, -18, 59, -78, -30, -37, -100 };
    assertTrue("Digest did not return expected result.", Arrays.equals(digestResult, expected1));
}
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