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

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

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.bouncycastle.pqc.jcajce.spec.NTRUParameterSpec;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import java.io.IOException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.pqc.crypto.ntru.NTRUPrivateKeyParameters;
import org.bouncycastle.pqc.jcajce.interfaces.NTRUKey;
import java.security.PrivateKey;

public class BCNTRUPrivateKey implements PrivateKey, NTRUKey
{
    private static final long serialVersionUID = 1L;
    private transient NTRUPrivateKeyParameters params;
    private transient ASN1Set attributes;
    
    public BCNTRUPrivateKey(final NTRUPrivateKeyParameters params) {
        this.params = params;
    }
    
    public BCNTRUPrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.init(privateKeyInfo);
    }
    
    private void init(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.attributes = privateKeyInfo.getAttributes();
        this.params = (NTRUPrivateKeyParameters)PrivateKeyFactory.createKey(privateKeyInfo);
    }
    
    @Override
    public boolean equals(final Object o) {
        return o == this || (o instanceof BCNTRUPrivateKey && Arrays.areEqual(this.params.getEncoded(), ((BCNTRUPrivateKey)o).params.getEncoded()));
    }
    
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.params.getEncoded());
    }
    
    @Override
    public final String getAlgorithm() {
        return "NTRU";
    }
    
    @Override
    public byte[] getEncoded() {
        try {
            return PrivateKeyInfoFactory.createPrivateKeyInfo(this.params, this.attributes).getEncoded();
        }
        catch (final IOException ex) {
            return null;
        }
    }
    
    @Override
    public NTRUParameterSpec getParameterSpec() {
        return NTRUParameterSpec.fromName(this.params.getParameters().getName());
    }
    
    @Override
    public String getFormat() {
        return "PKCS#8";
    }
    
    NTRUPrivateKeyParameters getKeyParams() {
        return this.params;
    }
    
    private void readObject(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        this.init(PrivateKeyInfo.getInstance(objectInputStream.readObject()));
    }
    
    private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.defaultWriteObject();
        objectOutputStream.writeObject(this.getEncoded());
    }
}
