org.spongycastle.pqc.jcajce.provider.newhope.NHKeyPairGeneratorSpi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov Show documentation
Show all versions of prov Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.pqc.jcajce.provider.newhope;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
import org.spongycastle.crypto.KeyGenerationParameters;
import org.spongycastle.pqc.crypto.newhope.NHKeyPairGenerator;
import org.spongycastle.pqc.crypto.newhope.NHPrivateKeyParameters;
import org.spongycastle.pqc.crypto.newhope.NHPublicKeyParameters;
public class NHKeyPairGeneratorSpi
extends java.security.KeyPairGenerator
{
NHKeyPairGenerator engine = new NHKeyPairGenerator();
SecureRandom random = new SecureRandom();
boolean initialised = false;
public NHKeyPairGeneratorSpi()
{
super("NH");
}
public void initialize(
int strength,
SecureRandom random)
{
if (strength != 1024)
{
throw new IllegalArgumentException("strength must be 1024 bits");
}
engine.init(new KeyGenerationParameters(random, 1024));
initialised = true;
}
public void initialize(
AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
throw new InvalidAlgorithmParameterException("parameter object not recognised");
}
public KeyPair generateKeyPair()
{
if (!initialised)
{
engine.init(new KeyGenerationParameters(random, 1024));
initialised = true;
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
NHPublicKeyParameters pub = (NHPublicKeyParameters)pair.getPublic();
NHPrivateKeyParameters priv = (NHPrivateKeyParameters)pair.getPrivate();
return new KeyPair(new BCNHPublicKey(pub), new BCNHPrivateKey(priv));
}
}