use of org.bouncycastle.crypto.params.RSAKeyParameters in project jruby-openssl by jruby.
the class PKCS10Request method generatePublicKey.
public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
final KeySpec keySpec;
final KeyFactory keyFactory;
if (keyParams instanceof RSAKeyParameters) {
RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
keyFactory = SecurityHelper.getKeyFactory("RSA");
return keyFactory.generatePublic(keySpec);
} else if (keyParams instanceof DSAPublicKeyParameters) {
DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
DSAParameters params = dsa.getParameters();
keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
keyFactory = SecurityHelper.getKeyFactory("DSA");
return keyFactory.generatePublic(keySpec);
} else if (keyParams instanceof ECPublicKeyParameters) {
ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
ECDomainParameters ecParams = ec.getParameters();
ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
// NOTE: likely to fail if non BC factory picked up :
keySpec = new ECPublicKeySpec(ec.getQ(), params);
keyFactory = SecurityHelper.getKeyFactory("EC");
return keyFactory.generatePublic(keySpec);
} else {
throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
}
}
Aggregations