use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.
the class X509Builder method randomSerial.
public X509Builder randomSerial() {
try {
BigInteger sn = new BigInteger(64, new SecureRandom());
certificateSerialNumber = new CertificateSerialNumber(sn);
info.set(X509CertInfo.SERIAL_NUMBER, certificateSerialNumber);
} catch (Exception e) {
fault(e, "randomSerial");
}
return this;
}
use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.
the class CertificateUtils method generateSelfSignedX509Certificate.
/**
* Generate a self signed X509 certificate with Bouncy Castle.
* @throws SignatureException
* @throws IllegalStateException
* @throws InvalidKeyException
* @throws CertificateEncodingException
*/
public static X509Certificate generateSelfSignedX509Certificate() throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, InvalidKeyException, IllegalStateException, SignatureException {
Security.addProvider(new BouncyCastleProvider());
int validityDays = 3652;
// GENERATE THE PUBLIC/PRIVATE RSA KEY PAIR
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// GENERATE THE X509 CERTIFICATE
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
X500Principal dnName = new X500Principal("CN=OATServer");
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setSubjectDN(dnName);
// use the same
certGen.setIssuerDN(dnName);
certGen.setNotBefore(new java.sql.Time(System.currentTimeMillis()));
Calendar expiry = Calendar.getInstance();
expiry.add(Calendar.DAY_OF_YEAR, validityDays);
certGen.setNotAfter(expiry.getTime());
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC");
return cert;
}
use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.
the class CitrixClient method generateSessionId.
private String generateSessionId() throws NoSuchAlgorithmException {
// Create a secure random number generator
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
// Get 1024 random bits
byte[] seed = new byte[1];
sr.nextBytes(seed);
sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
int nextInt = sr.nextInt();
String sessionId = "" + ((nextInt < 0) ? nextInt * -1 : nextInt);
log.info("Session Id Generated [" + sessionId + "]");
return sessionId;
}
use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.
the class CitrixClient method generateNonce.
public String generateNonce() {
try {
// Create a secure random number generator
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
// Get 1024 random bits
byte[] bytes = new byte[16];
sr.nextBytes(bytes);
// nonce = new BASE64Encoder().encode( bytes);
String nonce = Base64.encodeBase64String(bytes);
log.info("Nonce Generated " + nonce);
return nonce;
} catch (NoSuchAlgorithmException e) {
throw new ASException(e);
}
}
use of java.security.SecureRandom in project languagetool by languagetool-org.
the class HTTPTools method disableCertChecks.
/**
* For testing, we disable all checks because we use a self-signed certificate on the server
* side and we want this test to run everywhere without importing the certificate into the JVM's trust store.
*
* See http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate
*/
static void disableCertChecks() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
Aggregations