J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 0000000 |
| 27 | * @summary MacClone |
| 28 | * @author Jan Luehe |
| 29 | */ |
| 30 | import javax.crypto.*; |
| 31 | |
| 32 | public class MacClone { |
| 33 | |
| 34 | public static void main(String[] args) throws Exception { |
| 35 | |
| 36 | // |
| 37 | // Clone uninitialized Mac object |
| 38 | // |
| 39 | Mac mac = Mac.getInstance("HmacSHA1", "SunJCE"); |
| 40 | Mac macClone = (Mac)mac.clone(); |
| 41 | System.out.println(macClone.getProvider().toString()); |
| 42 | System.out.println(macClone.getAlgorithm()); |
| 43 | boolean thrown = false; |
| 44 | try { |
| 45 | macClone.update((byte)0x12); |
| 46 | } catch (IllegalStateException ise) { |
| 47 | thrown = true; |
| 48 | } |
| 49 | if (!thrown) { |
| 50 | throw new Exception("Expected IllegalStateException not thrown"); |
| 51 | } |
| 52 | |
| 53 | // |
| 54 | // Clone initialized Mac object |
| 55 | // |
| 56 | KeyGenerator kgen = KeyGenerator.getInstance("DES"); |
| 57 | SecretKey skey = kgen.generateKey(); |
| 58 | |
| 59 | mac = Mac.getInstance("HmacSHA1"); |
| 60 | mac.init(skey); |
| 61 | macClone = (Mac)mac.clone(); |
| 62 | System.out.println(macClone.getProvider().toString()); |
| 63 | System.out.println(macClone.getAlgorithm()); |
| 64 | mac.update((byte)0x12); |
| 65 | macClone.update((byte)0x12); |
| 66 | byte[] macFinal = mac.doFinal(); |
| 67 | byte[] macCloneFinal = macClone.doFinal(); |
| 68 | if (!java.util.Arrays.equals(macFinal, macCloneFinal)) { |
| 69 | throw new Exception("MAC results are different"); |
| 70 | } |
| 71 | } |
| 72 | } |