Home | History | Annotate | Download | only in spec
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package tests.security.spec;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import java.security.Key;
     23 import java.security.KeyFactory;
     24 import java.security.KeyPair;
     25 import java.security.KeyPairGenerator;
     26 import java.security.interfaces.DSAPrivateKey;
     27 import java.security.interfaces.DSAPublicKey;
     28 import java.security.spec.EncodedKeySpec;
     29 import java.security.spec.PKCS8EncodedKeySpec;
     30 import java.security.spec.X509EncodedKeySpec;
     31 
     32 public class EncodedKeySpec2Test extends TestCase {
     33 
     34     /**
     35      * java.security.spec.EncodedKeySpec#getEncoded()
     36      */
     37     public void test_getEncoded() throws Exception {
     38 
     39                KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
     40 
     41                keyGen.initialize(1024);
     42                KeyPair keys = keyGen.generateKeyPair();
     43 
     44 
     45                KeyFactory fact = KeyFactory.getInstance("DSA");
     46 
     47 
     48                // check public key encoding
     49                byte[] encoded = keys.getPublic().getEncoded();
     50                Key key = fact.generatePublic(new X509EncodedKeySpec(encoded));
     51 
     52                assertTrue("public key encodings were different",
     53                            isEqual(key, keys.getPublic()));
     54 
     55                // check private key encoding
     56                encoded = keys.getPrivate().getEncoded();
     57                key = fact.generatePrivate(new PKCS8EncodedKeySpec(encoded));
     58 
     59                assertTrue("private key encodings were different",
     60                            isEqual(key, keys.getPrivate()));
     61     }
     62 
     63     private boolean isEqual(Key key1, Key key2) {
     64         if (key1 instanceof DSAPublicKey && key2 instanceof DSAPublicKey) {
     65             DSAPublicKey dsa1 = ((DSAPublicKey) key1);
     66             DSAPublicKey dsa2 = ((DSAPublicKey) key2);
     67             return dsa1.getY().equals(dsa2.getY())
     68                     && dsa1.getParams().getG().equals(dsa2.getParams().getG())
     69                     && dsa1.getParams().getP().equals(dsa2.getParams().getP())
     70                     && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
     71 
     72         } else if (key1 instanceof DSAPrivateKey
     73                 && key2 instanceof DSAPrivateKey) {
     74             DSAPrivateKey dsa1 = ((DSAPrivateKey) key1);
     75             DSAPrivateKey dsa2 = ((DSAPrivateKey) key2);
     76             return dsa1.getX().equals(dsa2.getX())
     77                     && dsa1.getParams().getG().equals(dsa2.getParams().getG())
     78                     && dsa1.getParams().getP().equals(dsa2.getParams().getP())
     79                     && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
     80         } else {
     81             return false;
     82         }
     83     }
     84 }
     85