2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.cm.test;
24 import java.io.IOException;
25 import java.security.KeyPair;
26 import java.security.PrivateKey;
27 import java.security.PublicKey;
29 import org.junit.AfterClass;
30 import org.junit.Test;
31 import org.onap.aaf.auth.env.AuthzEnv;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.cadi.cm.CertException;
34 import org.onap.aaf.cadi.cm.Factory;
36 import junit.framework.Assert;
38 public class JU_KeyMarshaling {
41 public static void tearDownAfterClass() throws Exception {
46 AuthzEnv env = new AuthzEnv();
47 AuthzTrans trans = env.newTrans();
49 KeyPair kpair = Factory.generateKeyPair(trans);
50 String privateString = Factory.toString(trans, kpair.getPrivate());
51 System.out.println("Private as base64 encoded as PKCS8 Spec");
52 System.out.println(privateString);
54 // Take String, and create Private Key
55 PrivateKey pk = Factory.toPrivateKey(trans, privateString);
56 Assert.assertEquals(kpair.getPrivate().getAlgorithm(), pk.getAlgorithm());
57 Assert.assertEquals(kpair.getPrivate().getFormat(), pk.getFormat());
58 Assert.assertEquals(kpair.getPrivate().getEncoded(), pk.getEncoded());
61 String s = Factory.toString(trans, kpair.getPublic());
62 System.out.println("Public as base64 encoded x509 Spec");
63 System.out.println(s);
65 PublicKey pub = Factory.toPublicKey(trans, s);
66 Assert.assertEquals(kpair.getPublic().toString(), pub.toString());
69 } catch (IOException e) {
71 } catch (CertException e) {
74 StringBuilder sb = new StringBuilder("=== Timings ===\n");
75 trans.auditTrail(1, sb);
76 System.out.println(sb);