Search in sources :

Example 16 with DigestInputStream

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

the class DigestInputStreamTest method testSetMessageDigest.

/**
     * Test for <code>setMessageDigest()</code> method<br>
     *
     * Assertion: set associated message digest<br>
     */
public final void testSetMessageDigest() {
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            DigestInputStream dis = new DigestInputStream(null, null);
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            dis.setMessageDigest(md);
            assertTrue(dis.getMessageDigest() == md);
            return;
        } 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)

Example 17 with DigestInputStream

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

the class DigestInputStreamTest method testRead01.

/**
     * Test #1 for <code>read()</code> method<br>
     *
     * Assertion: returns the byte read<br>
     * Assertion: updates associated digest<br>
     */
public final void testRead01() 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++) {
                // check that read() returns valid values
                assertTrue("retval", ((byte) dis.read() == myMessage[i]));
            }
            // 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 18 with DigestInputStream

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

the class DigestInputStreamTest method testReadbyteArrayintint02.

/**
     * Test #2 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 testReadbyteArrayintint02() throws IOException {
    // check precondition
    assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
    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];
            for (int i = 0; i < MY_MESSAGE_LEN / CHUNK_SIZE; i++) {
                // check that read(byte[],int,int) returns valid value
                assertTrue("retval", dis.read(bArray, i * CHUNK_SIZE, CHUNK_SIZE) == CHUNK_SIZE);
            }
            // 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 19 with DigestInputStream

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

the class DigestInputStreamTest method testRead03.

/**
     * Test #3 for <code>read()</code> method<br>
     * Test #1 for <code>on(boolean)</code> method<br>
     *
     * Assertion: <code>read()</code> must not update digest if it is off<br>
     * Assertion: <code>on(boolean)</code> turns digest functionality on
     * (if <code>true</code> passed as a parameter) or off (if <code>false</code>
     *  passed)
     */
public final void testRead03() 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);
            // turn digest off
            dis.on(false);
            for (int i = 0; i < MY_MESSAGE_LEN; i++) {
                dis.read();
            }
            // check that digest value has not been updated by read()
            assertTrue(Arrays.equals(dis.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[ii] + "_NU")));
            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 20 with DigestInputStream

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

the class DigestInputStreamTest method testReadbyteArrayintint04.

/**
     * Test #4 for <code>read(byte[],int,int)</code> method<br>
     *
     * Assertion: returns the number of bytes read<br>
     *
     * Assertion: updates associated digest<br>
     */
public final void testReadbyteArrayintint04() 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];
            // read all but EOS
            dis.read(bArray, 0, bArray.length);
            // check that subsequent read(byte[],int,int) calls return -1 (EOS)
            assertEquals("retval1", -1, dis.read(bArray, 0, 1));
            assertEquals("retval2", -1, dis.read(bArray, 0, bArray.length));
            assertEquals("retval3", -1, dis.read(bArray, 0, 1));
            // 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)

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