![JAR search and dependency download from the Maven repository](/logo.png)
com.unbound.provider.UBRSAKeyFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
package com.unbound.provider;
import com.unbound.client.Partition;
import com.unbound.client.RSAPrivateKeyObject;
import com.unbound.common.crypto.SystemProvider;
import java.security.*;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;
public final class UBRSAKeyFactory extends KeyFactorySpi
{
private final Partition partition;
private KeyParameters keyParameter = null;
UBRSAKeyFactory(Partition partition)
{
this.partition = partition;
}
// --------------------- interface -----------------
@Override
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException
{
return SystemProvider.KeyFactory.getInstance("RSA").generatePublic(keySpec);
}
@Override
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException
{
if (keySpec instanceof KeyFactorySpec)
{
keyParameter = ((KeyFactorySpec)keySpec).getKeyParams();
keySpec = ((KeyFactorySpec)keySpec).getOriginal();
}
if (keySpec == null) throw new InvalidKeySpecException("keySpec == null");
if ((keySpec instanceof RSAPrivateCrtKeySpec) || (keySpec instanceof PKCS8EncodedKeySpec))
{
RSAPrivateCrtKey keyValue = (RSAPrivateCrtKey)SystemProvider.KeyFactory.getInstance("RSA").generatePrivate(keySpec);
RSAPrivateKeyObject object = partition.importRsaKey(null, keyValue, keyParameter);
return new UBRSAPrivateKey(object);
}
throw new InvalidKeySpecException("Must use RSAPrivateCrtKeySpec or PKCS8EncodedKeySpec; was " + keySpec.getClass().getName());
}
@Override
protected T engineGetKeySpec(Key key, Class keySpec) throws InvalidKeySpecException
{
if ((key == null) || (keySpec == null)) throw new InvalidKeySpecException("key and keySpec must not be null");
if (key instanceof RSAPublicKey && X509EncodedKeySpec.class.isAssignableFrom(keySpec))
{
return SystemProvider.KeyFactory.getInstance("RSA").getKeySpec(key, keySpec);
}
if (key instanceof RSAPublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec))
{
return (T) new RSAPublicKeySpec(((RSAPublicKey)key).getModulus(), ((RSAPublicKey)key).getPublicExponent());
}
throw new InvalidKeySpecException("Could not encode key");
}
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException
{
return key;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy