org.bouncycastle.pqc.jcajce.provider.sike.BCSIKEPrivateKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-debug-jdk15to18 Show documentation
Show all versions of bcprov-debug-jdk15to18 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.
package org.bouncycastle.pqc.jcajce.provider.sike;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.PrivateKey;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.pqc.crypto.sike.SIKEPrivateKeyParameters;
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.pqc.jcajce.interfaces.SIKEKey;
import org.bouncycastle.pqc.jcajce.spec.SIKEParameterSpec;
import org.bouncycastle.util.Arrays;
public class BCSIKEPrivateKey
implements PrivateKey, SIKEKey
{
private static final long serialVersionUID = 1L;
private transient SIKEPrivateKeyParameters params;
private transient ASN1Set attributes;
public BCSIKEPrivateKey(
SIKEPrivateKeyParameters params)
{
this.params = params;
}
public BCSIKEPrivateKey(PrivateKeyInfo keyInfo)
throws IOException
{
init(keyInfo);
}
private void init(PrivateKeyInfo keyInfo)
throws IOException
{
this.attributes = keyInfo.getAttributes();
this.params = (SIKEPrivateKeyParameters) PrivateKeyFactory.createKey(keyInfo);
}
/**
* Compare this SPHINCS-256 private key with another object.
*
* @param o the other object
* @return the result of the comparison
*/
public boolean equals(Object o)
{
if (o == this)
{
return true;
}
if (o instanceof BCSIKEPrivateKey)
{
BCSIKEPrivateKey otherKey = (BCSIKEPrivateKey)o;
return Arrays.areEqual(params.getEncoded(), otherKey.params.getEncoded());
}
return false;
}
public int hashCode()
{
return Arrays.hashCode(params.getEncoded());
}
/**
* @return name of the algorithm - "SIKE"
*/
public final String getAlgorithm()
{
return "SIKE";
}
public byte[] getEncoded()
{
try
{
PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes);
return pki.getEncoded();
}
catch (IOException e)
{
return null;
}
}
public SIKEParameterSpec getParameterSpec()
{
return SIKEParameterSpec.fromName(params.getParameters().getName());
}
public String getFormat()
{
return "PKCS#8";
}
SIKEPrivateKeyParameters getKeyParams()
{
return params;
}
private void readObject(
ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
byte[] enc = (byte[])in.readObject();
init(PrivateKeyInfo.getInstance(enc));
}
private void writeObject(
ObjectOutputStream out)
throws IOException
{
out.defaultWriteObject();
out.writeObject(this.getEncoded());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy