Search in sources :

Example 16 with DigestOutputStream

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

the class DigestOutputStreamTest method test_writeI.

/**
     * java.security.DigestOutputStream#write(int)
     */
public void test_writeI() throws Exception {
    // Test for method void java.security.DigestOutputStream.write(int)
    DigestOutputStream dos = new DigestOutputStream(new ByteArrayOutputStream(), MessageDigest.getInstance("SHA"));
    dos.write((byte) 43);
    byte[] digestResult = dos.getMessageDigest().digest();
    byte[] expected = { -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, expected));
}
Also used : DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with DigestOutputStream

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

the class DigestOutputStreamTest method testWriteint02.

/**
     * Test #2 for <code>write(int)</code> method<br>
     * Test #1 for <code>on(boolean)</code> method<br>
     *
     * Assertion: <code>write(int)</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 testWriteint02() 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);
            // turn digest off
            dos.on(false);
            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 digest value has not been updated by write()
            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 18 with DigestOutputStream

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

the class DigestOutputStreamTest method testWriteint03.

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

Example 19 with DigestOutputStream

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

the class DigestOutputStreamTest method test_write$BII_3.

/**
     * Test #3 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_3() throws NoSuchAlgorithmException, IOException {
    // check precondition
    assertTrue(MY_MESSAGE_LEN % (CHUNK_SIZE + 1) != 0);
    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 + 1); i++) {
                dos.write(myMessage, i * (CHUNK_SIZE + 1), CHUNK_SIZE + 1);
            }
            // write remaining bytes
            dos.write(myMessage, MY_MESSAGE_LEN / (CHUNK_SIZE + 1) * (CHUNK_SIZE + 1), MY_MESSAGE_LEN % (CHUNK_SIZE + 1));
            // 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 20 with DigestOutputStream

use of java.security.DigestOutputStream in project iosched by google.

the class CloudFileManager method calulateHash.

public static byte[] calulateHash(JsonElement contents) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new InternalError("MD5 MessageDigest is not available");
    }
    OutputStream byteSink = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // ignore, since this is only used to calculate MD5
        }
    };
    DigestOutputStream dis = new DigestOutputStream(byteSink, md);
    new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
    return dis.getMessageDigest().digest();
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

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