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

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

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import java.io.IOException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.pqc.crypto.lms.LMSPrivateKeyParameters;
import org.bouncycastle.pqc.crypto.lms.HSSPrivateKeyParameters;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.pqc.crypto.lms.LMSKeyParameters;
import org.bouncycastle.pqc.jcajce.interfaces.LMSPrivateKey;
import java.security.PrivateKey;

public class BCLMSPrivateKey implements PrivateKey, LMSPrivateKey
{
    private static final long serialVersionUID = 8568701712864512338L;
    private transient LMSKeyParameters keyParams;
    private transient ASN1Set attributes;
    
    public BCLMSPrivateKey(final LMSKeyParameters lmsKeyParameters) {
        this.keyParams = ((lmsKeyParameters instanceof HSSPrivateKeyParameters) ? lmsKeyParameters : new HSSPrivateKeyParameters((LMSPrivateKeyParameters)lmsKeyParameters, ((LMSPrivateKeyParameters)lmsKeyParameters).getIndex(), ((LMSPrivateKeyParameters)lmsKeyParameters).getIndex() + ((LMSPrivateKeyParameters)lmsKeyParameters).getUsagesRemaining()));
    }
    
    public BCLMSPrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.init(privateKeyInfo);
    }
    
    private void init(final PrivateKeyInfo privateKeyInfo) throws IOException {
        this.attributes = privateKeyInfo.getAttributes();
        this.keyParams = (LMSKeyParameters)PrivateKeyFactory.createKey(privateKeyInfo);
    }
    
    @Override
    public long getIndex() {
        if (this.getUsagesRemaining() == 0L) {
            throw new IllegalStateException("key exhausted");
        }
        if (this.keyParams instanceof LMSPrivateKeyParameters) {
            return ((LMSPrivateKeyParameters)this.keyParams).getIndex();
        }
        return ((HSSPrivateKeyParameters)this.keyParams).getIndex();
    }
    
    @Override
    public long getUsagesRemaining() {
        if (this.keyParams instanceof LMSPrivateKeyParameters) {
            return ((LMSPrivateKeyParameters)this.keyParams).getUsagesRemaining();
        }
        return ((HSSPrivateKeyParameters)this.keyParams).getUsagesRemaining();
    }
    
    @Override
    public LMSPrivateKey extractKeyShard(final int n) {
        if (this.keyParams instanceof LMSPrivateKeyParameters) {
            return new BCLMSPrivateKey(((LMSPrivateKeyParameters)this.keyParams).extractKeyShard(n));
        }
        return new BCLMSPrivateKey(((HSSPrivateKeyParameters)this.keyParams).extractKeyShard(n));
    }
    
    @Override
    public String getAlgorithm() {
        return "LMS";
    }
    
    @Override
    public String getFormat() {
        return "PKCS#8";
    }
    
    @Override
    public byte[] getEncoded() {
        try {
            return PrivateKeyInfoFactory.createPrivateKeyInfo(this.keyParams, this.attributes).getEncoded();
        }
        catch (final IOException ex) {
            return null;
        }
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (o instanceof BCLMSPrivateKey) {
            final BCLMSPrivateKey bclmsPrivateKey = (BCLMSPrivateKey)o;
            try {
                return Arrays.areEqual(this.keyParams.getEncoded(), bclmsPrivateKey.keyParams.getEncoded());
            }
            catch (final IOException ex) {
                throw new IllegalStateException("unable to perform equals");
            }
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        try {
            return Arrays.hashCode(this.keyParams.getEncoded());
        }
        catch (final IOException ex) {
            throw new IllegalStateException("unable to calculate hashCode");
        }
    }
    
    CipherParameters getKeyParams() {
        return this.keyParams;
    }
    
    @Override
    public int getLevels() {
        if (this.keyParams instanceof LMSPrivateKeyParameters) {
            return 1;
        }
        return ((HSSPrivateKeyParameters)this.keyParams).getL();
    }
    
    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());
    }
}
