use of java.security.KeyPairGenerator in project nhin-d by DirectProject.
the class CertGenerator method createCertificate.
public static CertCreateFields createCertificate(CertCreateFields fields) throws Exception {
// generate a key pair first using RSA and a key strength provided by the user
KeyPairGenerator kpg = (KeyPairGenerator) KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(fields.getKeyStrength(), new SecureRandom());
KeyPair keyPair = kpg.generateKeyPair();
if (fields.getSignerCert() == null)
// this is request for a new CA
return createNewCA(fields, keyPair);
else
// new leaf certificate request
return createLeafCertificate(fields, keyPair);
}
use of java.security.KeyPairGenerator in project tomee by apache.
the class SslTomEETest method test.
@Test
public void test() throws Exception {
final File keystore = new File("target/keystore");
{
// generate keystore/trustore
if (keystore.exists()) {
Files.delete(keystore);
}
keystore.getParentFile().mkdirs();
try (final FileOutputStream fos = new FileOutputStream(keystore)) {
final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
keyGenerator.initialize(1024);
final KeyPair pair = keyGenerator.generateKeyPair();
final boolean addBc = Security.getProvider("BC") == null;
if (addBc) {
Security.addProvider(new BouncyCastleProvider());
}
try {
final X509v1CertificateBuilder x509v1CertificateBuilder = new JcaX509v1CertificateBuilder(new X500Name("cn=serveralias"), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1)), new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)), new X500Name("cn=serveralias"), pair.getPublic());
final X509CertificateHolder certHldr = x509v1CertificateBuilder.build(new JcaContentSignerBuilder("SHA1WithRSA").setProvider("BC").build(pair.getPrivate()));
final X509Certificate cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHldr);
final KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, "changeit".toCharArray());
ks.setKeyEntry("serveralias", pair.getPrivate(), "changeit".toCharArray(), new Certificate[] { cert });
ks.store(fos, "changeit".toCharArray());
} finally {
if (addBc) {
Security.removeProvider("BC");
}
}
} catch (final Exception e) {
Assert.fail(e.getMessage());
}
}
final Configuration configuration = new Configuration();
configuration.setSsl(true);
configuration.setKeystoreFile(keystore.getAbsolutePath());
configuration.setKeystorePass("changeit");
configuration.setKeyAlias("serveralias");
final Container container = new Container();
container.setup(configuration);
container.start();
try {
assertEquals(8443, ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("Tomcat:type=ProtocolHandler,port=8443"), "port"));
} finally {
container.stop();
}
// ensure it is not always started
configuration.setSsl(false);
container.setup(configuration);
container.start();
try {
assertFalse(ManagementFactory.getPlatformMBeanServer().isRegistered(new ObjectName("Tomcat:type=ProtocolHandler,port=8443")));
} finally {
container.close();
}
}
use of java.security.KeyPairGenerator in project bnd by bndtools.
the class Settings method generate.
public void generate(char[] password) throws Exception {
check();
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
privateKey = pair.getPrivate();
publicKey = pair.getPublic();
data.secret = privateKey.getEncoded();
data.id = publicKey.getEncoded();
save(password);
}
use of java.security.KeyPairGenerator in project bnd by bndtools.
the class RSA method generate.
public static Pair<RSAPrivateKey, RSAPublicKey> generate() throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
KeyPair keypair = kpg.generateKeyPair();
return new Pair<RSAPrivateKey, RSAPublicKey>((RSAPrivateKey) keypair.getPrivate(), (RSAPublicKey) keypair.getPublic());
}
use of java.security.KeyPairGenerator in project bnd by bndtools.
the class Server method createKey.
private KeyPair createKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(config.keysize, random);
KeyPair pair = keyGen.generateKeyPair();
return pair;
}
Aggregations