// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.pqc.jcajce.provider.hqc;

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.bouncycastle.pqc.jcajce.spec.HQCParameterSpec;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
import java.io.IOException;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.pqc.crypto.hqc.HQCPublicKeyParameters;
import org.bouncycastle.pqc.jcajce.interfaces.HQCKey;
import java.security.PublicKey;

public class BCHQCPublicKey implements PublicKey, HQCKey
{
    private static final long serialVersionUID = 1L;
    private transient HQCPublicKeyParameters params;
    
    public BCHQCPublicKey(final HQCPublicKeyParameters params) {
        this.params = params;
    }
    
    public BCHQCPublicKey(final SubjectPublicKeyInfo subjectPublicKeyInfo) throws IOException {
        this.init(subjectPublicKeyInfo);
    }
    
    private void init(final SubjectPublicKeyInfo subjectPublicKeyInfo) throws IOException {
        this.params = (HQCPublicKeyParameters)PublicKeyFactory.createKey(subjectPublicKeyInfo);
    }
    
    @Override
    public boolean equals(final Object o) {
        return o == this || (o instanceof BCHQCPublicKey && Arrays.areEqual(this.params.getEncoded(), ((BCHQCPublicKey)o).params.getEncoded()));
    }
    
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.params.getEncoded());
    }
    
    @Override
    public final String getAlgorithm() {
        return Strings.toUpperCase(this.params.getParameters().getName());
    }
    
    @Override
    public byte[] getEncoded() {
        try {
            return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(this.params).getEncoded();
        }
        catch (final IOException ex) {
            return null;
        }
    }
    
    @Override
    public String getFormat() {
        return "X.509";
    }
    
    @Override
    public HQCParameterSpec getParameterSpec() {
        return HQCParameterSpec.fromName(this.params.getParameters().getName());
    }
    
    HQCPublicKeyParameters getKeyParams() {
        return this.params;
    }
    
    private void readObject(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        this.init(SubjectPublicKeyInfo.getInstance(objectInputStream.readObject()));
    }
    
    private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.defaultWriteObject();
        objectOutputStream.writeObject(this.getEncoded());
    }
}
