< prev index next >

test/jdk/javax/crypto/Cipher/CipherStreamClose.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 25,35 **** * @test * @bug 7160837 * @summary Make sure Cipher IO streams doesn't call extra doFinal if close() * is called multiple times. Additionally, verify the input and output streams * match with encryption and decryption with non-stream crypto. - * @modules java.xml.bind * @run main CipherStreamClose */ import java.io.*; import java.security.DigestOutputStream; --- 25,34 ----
*** 40,50 **** import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.CipherInputStream; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; - import javax.xml.bind.DatatypeConverter; public class CipherStreamClose { private static final String message = "This is the sample message"; static boolean debug = false; --- 39,48 ----
*** 64,74 **** } data = bos.toByteArray(); } if (debug) { ! System.out.println(DatatypeConverter.printHexBinary(data)); } return encCipher.doFinal(data); } --- 62,72 ---- } data = bos.toByteArray(); } if (debug) { ! System.out.println(printHexBinary(data)); } return encCipher.doFinal(data); }
*** 104,114 **** } data = bos.toByteArray(); } if (debug) { ! System.out.println(DatatypeConverter.printHexBinary(data)); } return data; } public static Object streamDecrypt(byte[] data, SecretKey key, --- 102,112 ---- } data = bos.toByteArray(); } if (debug) { ! System.out.println(printHexBinary(data)); } return data; } public static Object streamDecrypt(byte[] data, SecretKey key,
*** 128,138 **** } public static void main(String[] args) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA1"); SecretKeySpec key = new SecretKeySpec( ! DatatypeConverter.parseHexBinary( "12345678123456781234567812345678"), "AES"); // Run 'message' through streamEncrypt byte[] se = streamEncrypt(message, key, digest); // 'digest' already has the value from the stream, just finish the op --- 126,136 ---- } public static void main(String[] args) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA1"); SecretKeySpec key = new SecretKeySpec( ! parseHexBinary( "12345678123456781234567812345678"), "AES"); // Run 'message' through streamEncrypt byte[] se = streamEncrypt(message, key, digest); // 'digest' already has the value from the stream, just finish the op
*** 142,155 **** byte[] be = blockEncrypt(message, key); // Take digest of encrypted blockEncrypt result byte[] bd = digest.digest(be); // Verify both returned the same value if (!Arrays.equals(sd, bd)) { ! System.err.println("Stream: "+DatatypeConverter.printHexBinary(se)+ ! "\t Digest: "+DatatypeConverter.printHexBinary(sd)); ! System.err.println("Block : "+DatatypeConverter.printHexBinary(be)+ ! "\t Digest: "+DatatypeConverter.printHexBinary(bd)); throw new Exception("stream & block encryption does not match"); } digest.reset(); // Sanity check: Decrypt separately from stream to verify operations --- 140,153 ---- byte[] be = blockEncrypt(message, key); // Take digest of encrypted blockEncrypt result byte[] bd = digest.digest(be); // Verify both returned the same value if (!Arrays.equals(sd, bd)) { ! System.err.println("Stream: "+ printHexBinary(se)+ ! "\t Digest: "+ printHexBinary(sd)); ! System.err.println("Block : "+printHexBinary(be)+ ! "\t Digest: "+ printHexBinary(bd)); throw new Exception("stream & block encryption does not match"); } digest.reset(); // Sanity check: Decrypt separately from stream to verify operations
*** 164,169 **** --- 162,213 ---- if (message.compareTo(sm) != 0) { System.err.println("Expected: "+message+"\nStream: "+sm); throw new Exception("Stream decryption does not match expected."); } } + + public static byte[] parseHexBinary(String s) { + final int len = s.length(); + + // "111" is not a valid hex encoding. + if (len % 2 != 0) { + throw new IllegalArgumentException("hexBinary needs to be even-length: " + s); + } + + byte[] out = new byte[len / 2]; + + for (int i = 0; i < len; i += 2) { + int h = hexToBin(s.charAt(i)); + int l = hexToBin(s.charAt(i + 1)); + if (h == -1 || l == -1) { + throw new IllegalArgumentException("contains illegal character for hexBinary: " + s); + } + + out[i / 2] = (byte) (h * 16 + l); + } + + return out; + } + + private static int hexToBin(char ch) { + if ('0' <= ch && ch <= '9') { + return ch - '0'; + } + if ('A' <= ch && ch <= 'F') { + return ch - 'A' + 10; + } + if ('a' <= ch && ch <= 'f') { + return ch - 'a' + 10; + } + return -1; + } + private static final char[] hexCode = "0123456789ABCDEF".toCharArray(); + + public static String printHexBinary(byte[] data) { + StringBuilder r = new StringBuilder(data.length * 2); + for (byte b : data) { + r.append(hexCode[(b >> 4) & 0xF]); + r.append(hexCode[(b & 0xF)]); + } + return r.toString(); + } }
< prev index next >