jdk/test/javax/crypto/Cipher/CipherStreamClose.java
author prr
Tue, 01 Jul 2014 10:20:56 -0700
changeset 25211 2476da7d73bf
parent 17917 9bd1b39cdbcf
child 37779 7c84df693837
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     1
/*
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     4
 *
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     8
 *
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    13
 * accompanied this code).
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    14
 *
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    18
 *
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    21
 * questions.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    22
 */
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    23
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    24
/*
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    25
 * @test
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    26
 * @bug 7160837
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    27
 * @summary Make sure Cipher IO streams doesn't call extra doFinal if close()
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    28
 * is called multiple times.  Additionally, verify the input and output streams
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    29
 * match with encryption and decryption with non-stream crypto.
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    30
 */
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    31
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    32
import java.io.*;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    33
import java.security.DigestOutputStream;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    34
import java.security.DigestInputStream;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    35
import java.security.MessageDigest;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    36
import java.util.Arrays;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    37
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    38
import javax.crypto.Cipher;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    39
import javax.crypto.CipherOutputStream;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    40
import javax.crypto.CipherInputStream;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    41
import javax.crypto.SecretKey;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    42
import javax.crypto.spec.SecretKeySpec;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    43
import javax.xml.bind.DatatypeConverter;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    44
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    45
public class CipherStreamClose {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    46
    private static final String message = "This is the sample message";
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    47
    static boolean debug = false;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    48
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    49
    /*
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    50
     * This method does encryption by cipher.doFinal(), and not with
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    51
     * CipherOutputStream
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    52
     */
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    53
    public static byte[] blockEncrypt(String message, SecretKey key)
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    54
        throws Exception {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    55
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    56
        byte[] data;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    57
        Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    58
        encCipher.init(Cipher.ENCRYPT_MODE, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    59
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    60
            try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    61
                oos.writeObject(message);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    62
            }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    63
            data = bos.toByteArray();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    64
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    65
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    66
        if (debug) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    67
            System.out.println(DatatypeConverter.printHexBinary(data));
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    68
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    69
        return encCipher.doFinal(data);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    70
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    71
    }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    72
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    73
    /*
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    74
     * This method does decryption by cipher.doFinal(), and not with
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    75
     * CipherIntputStream
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    76
     */
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    77
    public static Object blockDecrypt(byte[] data, SecretKey key)
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    78
        throws Exception {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    79
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    80
        Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    81
        c.init(Cipher.DECRYPT_MODE, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    82
        data = c.doFinal(data);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    83
        try (ByteArrayInputStream bis = new ByteArrayInputStream(data)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    84
            try (ObjectInputStream ois = new ObjectInputStream(bis)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    85
                return ois.readObject();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    86
            }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    87
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    88
    }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    89
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    90
    public static byte[] streamEncrypt(String message, SecretKey key,
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    91
        MessageDigest digest)
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    92
        throws Exception {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    93
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    94
        byte[] data;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    95
        Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    96
        encCipher.init(Cipher.ENCRYPT_MODE, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    97
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    98
            DigestOutputStream dos = new DigestOutputStream(bos, digest);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
    99
            CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   100
            try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   101
                oos.writeObject(message);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   102
            }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   103
            data = bos.toByteArray();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   104
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   105
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   106
        if (debug) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   107
            System.out.println(DatatypeConverter.printHexBinary(data));
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   108
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   109
        return data;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   110
    }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   111
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   112
    public static Object streamDecrypt(byte[] data, SecretKey key,
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   113
        MessageDigest digest) throws Exception {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   114
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   115
        Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   116
        decCipher.init(Cipher.DECRYPT_MODE, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   117
        digest.reset();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   118
        try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   119
            DigestInputStream dis = new DigestInputStream(bis, digest);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   120
            CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   121
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   122
            try (ObjectInputStream ois = new ObjectInputStream(cis)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   123
                return ois.readObject();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   124
            }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   125
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   126
    }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   127
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   128
    public static void main(String[] args) throws Exception {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   129
        MessageDigest digest = MessageDigest.getInstance("SHA1");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   130
        SecretKeySpec key = new SecretKeySpec(
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   131
            DatatypeConverter.parseHexBinary(
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   132
            "12345678123456781234567812345678"), "AES");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   133
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   134
        // Run 'message' through streamEncrypt
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   135
        byte[] se = streamEncrypt(message, key, digest);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   136
        // 'digest' already has the value from the stream, just finish the op
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   137
        byte[] sd = digest.digest();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   138
        digest.reset();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   139
        // Run 'message' through blockEncrypt
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   140
        byte[] be = blockEncrypt(message, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   141
        // Take digest of encrypted blockEncrypt result
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   142
        byte[] bd = digest.digest(be);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   143
        // Verify both returned the same value
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   144
        if (!Arrays.equals(sd, bd)) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   145
            System.err.println("Stream: "+DatatypeConverter.printHexBinary(se)+
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   146
                "\t Digest: "+DatatypeConverter.printHexBinary(sd));
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   147
            System.err.println("Block : "+DatatypeConverter.printHexBinary(be)+
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   148
                "\t Digest: "+DatatypeConverter.printHexBinary(bd));
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   149
            throw new Exception("stream & block encryption does not match");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   150
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   151
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   152
        digest.reset();
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   153
        // Sanity check: Decrypt separately from stream to verify operations
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   154
        String bm = (String) blockDecrypt(be, key);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   155
        if (message.compareTo(bm) != 0) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   156
            System.err.println("Expected: "+message+"\nBlock:    "+bm);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   157
            throw new Exception("Block decryption does not match expected");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   158
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   159
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   160
        // Have decryption and digest included in the object stream
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   161
        String sm = (String) streamDecrypt(se, key, digest);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   162
        if (message.compareTo(sm) != 0) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   163
            System.err.println("Expected: "+message+"\nStream:   "+sm);
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   164
            throw new Exception("Stream decryption does not match expected.");
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   165
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   166
    }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents:
diff changeset
   167
}