1 /*
   2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 7087021
  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", "SunJCE");
  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 }