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

package org.bouncycastle.jcajce.provider.asymmetric.mlkem;

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.jcajce.interfaces.MLKEMPublicKey;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.jcajce.spec.MLKEMParameterSpec;
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import java.io.IOException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.util.Strings;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPrivateKeyParameters;
import org.bouncycastle.jcajce.interfaces.BCKey;
import org.bouncycastle.jcajce.interfaces.MLKEMPrivateKey;

public class BCMLKEMPrivateKey implements MLKEMPrivateKey, BCKey
{
    private static final long serialVersionUID = 1L;
    private transient MLKEMPrivateKeyParameters params;
    private transient String algorithm;
    private transient ASN1Set attributes;
    private transient byte[] priorEncoding;
    
    public BCMLKEMPrivateKey(final MLKEMPrivateKeyParameters params) {
        this.params = params;
        this.algorithm = Strings.toUpperCase(params.getParameters().getName());
    }
    
    public BCMLKEMPrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.init(privateKeyInfo);
    }
    
    private void init(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.attributes = privateKeyInfo.getAttributes();
        this.priorEncoding = privateKeyInfo.getEncoded();
        this.params = (MLKEMPrivateKeyParameters)PrivateKeyFactory.createKey(privateKeyInfo);
        this.algorithm = Strings.toUpperCase(MLKEMParameterSpec.fromName(this.params.getParameters().getName()).getName());
    }
    
    @Override
    public boolean equals(final Object o) {
        return o == this || (o instanceof BCMLKEMPrivateKey && Arrays.areEqual(this.params.getEncoded(), ((BCMLKEMPrivateKey)o).params.getEncoded()));
    }
    
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.params.getEncoded());
    }
    
    @Override
    public final String getAlgorithm() {
        return this.algorithm;
    }
    
    @Override
    public byte[] getEncoded() {
        try {
            if (this.priorEncoding != null) {
                return this.priorEncoding;
            }
            return PrivateKeyInfoFactory.createPrivateKeyInfo(this.params, this.attributes).getEncoded();
        }
        catch (final IOException ex) {
            return null;
        }
    }
    
    @Override
    public MLKEMPublicKey getPublicKey() {
        return new BCMLKEMPublicKey(this.params.getPublicKeyParameters());
    }
    
    @Override
    public byte[] getPrivateData() {
        return this.params.getEncoded();
    }
    
    @Override
    public byte[] getSeed() {
        return this.params.getSeed();
    }
    
    @Override
    public MLKEMPrivateKey getPrivateKey(final boolean b) {
        if (b && this.params.getSeed() != null) {
            return new BCMLKEMPrivateKey(this.params.getParametersWithFormat(1));
        }
        return new BCMLKEMPrivateKey(this.params.getParametersWithFormat(2));
    }
    
    @Override
    public MLKEMParameterSpec getParameterSpec() {
        return MLKEMParameterSpec.fromName(this.params.getParameters().getName());
    }
    
    @Override
    public String getFormat() {
        return "PKCS#8";
    }
    
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        final String lineSeparator = Strings.lineSeparator();
        final byte[] publicKey = this.params.getPublicKey();
        sb.append(this.getAlgorithm()).append(" ").append("Private Key").append(" [").append(new Fingerprint(publicKey).toString()).append("]").append(lineSeparator).append("    public data: ").append(Hex.toHexString(publicKey)).append(lineSeparator);
        return sb.toString();
    }
    
    MLKEMPrivateKeyParameters 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());
    }
}
