/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package java.security; import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectInputStream; import java.io.ObjectStreamException; import java.io.Serializable; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.spec.SecretKeySpec; /** * {@code KeyRep} is a standardized representation for serialized {@link Key} * objects. */ public class KeyRep implements Serializable { private static final long serialVersionUID = -4757683898830641853L; // Key type private final Type type; // Key algorithm name private final String algorithm; // Key encoding format private final String format; // Key encoding private byte[] encoded; /** * Constructs a new instance of {@code KeyRep} with the specified arguments. * The arguments should be obtained from the {@code Key} object that has to * be serialized. * * @param type * the type of the key. * @param algorithm * the algorithm (obtained by {@link Key#getAlgorithm()}). * @param format * the format of the key (obtained by {@link Key#getFormat()}). * @param encoded * the encoded {@code byte[]} (obtained by * {@link Key#getEncoded()}). * @throws NullPointerException * if {@code type, algorithm, format or encoded} is {@code null} * . */ public KeyRep(Type type, String algorithm, String format, byte[] encoded) { this.type = type; this.algorithm = algorithm; this.format = format; this.encoded = encoded; if(this.type == null) { throw new NullPointerException("type == null"); } if(this.algorithm == null) { throw new NullPointerException("algorithm == null"); } if(this.format == null) { throw new NullPointerException("format == null"); } if(this.encoded == null) { throw new NullPointerException("encoded == null"); } } /** * Resolves and returns the {@code Key} object. Three {@link Type}|format * combinations are supported: *