test/sun/security/mscapi/SignUsingSHA2withRSA.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 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  */


  61         Enumeration e = ks.aliases();
  62         PrivateKey privateKey = null;
  63         PublicKey publicKey = null;
  64 
  65         while (e.hasMoreElements()) {
  66             String alias = (String) e.nextElement();
  67             if (alias.equals("6753664")) {
  68                 System.out.println("Loaded entry: " + alias);
  69                 privateKey = (PrivateKey) ks.getKey(alias, null);
  70                 publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
  71             }
  72         }
  73         if (privateKey == null || publicKey == null) {
  74             throw new Exception("Cannot load the keys need to run this test");
  75         }
  76         System.out.println("-------------------------------------------------");
  77 
  78         generatedSignatures.add(signUsing("SHA256withRSA", privateKey));
  79         generatedSignatures.add(signUsing("SHA384withRSA", privateKey));
  80         generatedSignatures.add(signUsing("SHA512withRSA", privateKey));


  81 
  82         System.out.println("-------------------------------------------------");
  83 
  84         verifyUsing("SHA256withRSA", publicKey, generatedSignatures.get(0));
  85         verifyUsing("SHA384withRSA", publicKey, generatedSignatures.get(1));
  86         verifyUsing("SHA512withRSA", publicKey, generatedSignatures.get(2));


  87 
  88         System.out.println("-------------------------------------------------");
  89     }
  90 
  91     private static byte[] signUsing(String signAlgorithm,
  92         PrivateKey privateKey) throws Exception {
  93 
  94         // Must explicitly specify the SunMSCAPI JCE provider
  95         // (otherwise SunJCE is chosen because it appears earlier in the list)
  96         Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");
  97         if (sig1 == null) {
  98             throw new Exception("'" + signAlgorithm + "' is not supported");
  99         }
 100         System.out.println("Using " + signAlgorithm + " signer from the " +
 101             sig1.getProvider().getName() + " JCE provider");
 102 
 103         System.out.println("Using key: " + privateKey);
 104         sig1.initSign(privateKey);
 105         sig1.update(toBeSigned);
 106         byte [] sigBytes = null;


   1 /*
   2  * Copyright (c) 2011, 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  */


  61         Enumeration e = ks.aliases();
  62         PrivateKey privateKey = null;
  63         PublicKey publicKey = null;
  64 
  65         while (e.hasMoreElements()) {
  66             String alias = (String) e.nextElement();
  67             if (alias.equals("6753664")) {
  68                 System.out.println("Loaded entry: " + alias);
  69                 privateKey = (PrivateKey) ks.getKey(alias, null);
  70                 publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
  71             }
  72         }
  73         if (privateKey == null || publicKey == null) {
  74             throw new Exception("Cannot load the keys need to run this test");
  75         }
  76         System.out.println("-------------------------------------------------");
  77 
  78         generatedSignatures.add(signUsing("SHA256withRSA", privateKey));
  79         generatedSignatures.add(signUsing("SHA384withRSA", privateKey));
  80         generatedSignatures.add(signUsing("SHA512withRSA", privateKey));
  81         generatedSignatures.add(signUsing("SHA224withRSA", privateKey));
  82 
  83 
  84         System.out.println("-------------------------------------------------");
  85 
  86         verifyUsing("SHA256withRSA", publicKey, generatedSignatures.get(0));
  87         verifyUsing("SHA384withRSA", publicKey, generatedSignatures.get(1));
  88         verifyUsing("SHA512withRSA", publicKey, generatedSignatures.get(2));
  89         verifyUsing("SHA224withRSA", publicKey, generatedSignatures.get(3));
  90 
  91 
  92         System.out.println("-------------------------------------------------");
  93     }
  94 
  95     private static byte[] signUsing(String signAlgorithm,
  96         PrivateKey privateKey) throws Exception {
  97 
  98         // Must explicitly specify the SunMSCAPI JCE provider
  99         // (otherwise SunJCE is chosen because it appears earlier in the list)
 100         Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");
 101         if (sig1 == null) {
 102             throw new Exception("'" + signAlgorithm + "' is not supported");
 103         }
 104         System.out.println("Using " + signAlgorithm + " signer from the " +
 105             sig1.getProvider().getName() + " JCE provider");
 106 
 107         System.out.println("Using key: " + privateKey);
 108         sig1.initSign(privateKey);
 109         sig1.update(toBeSigned);
 110         byte [] sigBytes = null;