org.bouncycastle.jcajce.provider.asymmetric.edec.EdECUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15to18 Show documentation
Show all versions of bcprov-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.jcajce.provider.asymmetric.edec;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
/**
* utility class for converting jce/jca XDH, and EdDSA
* objects into their org.bouncycastle.crypto counterparts.
*/
class EdECUtil
{
public static AsymmetricKeyParameter generatePublicKeyParameter(
PublicKey key)
throws InvalidKeyException
{
if (key instanceof BCXDHPublicKey)
{
return ((BCXDHPublicKey)key).engineGetKeyParameters();
}
else if (key instanceof BCEdDSAPublicKey)
{
return ((BCEdDSAPublicKey)key).engineGetKeyParameters();
}
else
{
// see if we can build a key from key.getEncoded()
try
{
byte[] bytes = key.getEncoded();
if (bytes == null)
{
throw new InvalidKeyException("no encoding for EdEC/XDH public key");
}
return PublicKeyFactory.createKey(bytes);
}
catch (Exception e)
{
throw new InvalidKeyException("cannot identify EdEC/XDH public key: " + e.getMessage());
}
}
}
public static AsymmetricKeyParameter generatePrivateKeyParameter(
PrivateKey key)
throws InvalidKeyException
{
if (key instanceof BCXDHPrivateKey)
{
return ((BCXDHPrivateKey)key).engineGetKeyParameters();
}
else if (key instanceof BCEdDSAPrivateKey)
{
return ((BCEdDSAPrivateKey)key).engineGetKeyParameters();
}
else
{
// see if we can build a key from key.getEncoded()
try
{
byte[] bytes = key.getEncoded();
if (bytes == null)
{
throw new InvalidKeyException("no encoding for EdEC/XDH private key");
}
return PrivateKeyFactory.createKey(bytes);
}
catch (Exception e)
{
throw new InvalidKeyException("cannot identify EdEC/XDH private key: " + e.getMessage());
}
}
}
}