/*
* 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.cert;
import java.io.ByteArrayInputStream;
import java.io.NotSerializableException;
import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.SignatureException;
import java.util.Arrays;
/**
* Abstract class to represent identity certificates. It represents a way to
* verify the binding of a Principal and its public key. Examples are X.509,
* PGP, and SDSI.
*/
public abstract class Certificate implements Serializable {
private static final long serialVersionUID = -3585440601605666277L;
// The standard name of the certificate type
private final String type;
/**
* Creates a new {@code Certificate} with the specified type.
*
* @param type
* the certificate type.
*/
protected Certificate(String type) {
this.type = type;
}
/**
* Returns the certificate type.
*
* @return the certificate type.
*/
public final String getType() {
return type;
}
/**
* Compares the argument to the certificate, and returns {@code true} if they
* represent the same object using a class specific comparison. The
* implementation in Object returns {@code true} only if the argument is the
* exact same object as the callee (==).
*
* @param other
* the object to compare with this object.
* @return {@code true} if the object is the same as this object, {@code
* false} if it is different from this object.
* @see #hashCode
*/
public boolean equals(Object other) {
// obj equal to itself
if (this == other) {
return true;
}
if (other instanceof Certificate) {
try {
// check that encoded forms match
return Arrays.equals(this.getEncoded(),
((Certificate)other).getEncoded());
} catch (CertificateEncodingException e) {
throw new RuntimeException(e);
}
}
return false;
}
/**
* Returns an integer hash code for the certificate. Any two objects which
* return {@code true} when passed to {@code equals} must return the same
* value for this method.
*
* @return the certificate's hash
* @see #equals
*/
public int hashCode() {
try {
byte[] encoded = getEncoded();
int hash = 0;
for (int i=0; i