Search in sources :

Example 26 with SecureRandom

use of java.security.SecureRandom in project sonarqube by SonarSource.

the class JwtCsrfVerifier method generateState.

public String generateState(HttpServletRequest request, HttpServletResponse response, int timeoutInSeconds) {
    // Create a state token to prevent request forgery.
    // Store it in the cookie for later validation.
    String state = new BigInteger(130, new SecureRandom()).toString(32);
    response.addCookie(newCookieBuilder(request).setName(CSRF_STATE_COOKIE).setValue(state).setHttpOnly(false).setExpiry(timeoutInSeconds).build());
    return state;
}
Also used : BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom)

Example 27 with SecureRandom

use of java.security.SecureRandom in project sonarqube by SonarSource.

the class OAuthCsrfVerifier method generateState.

public String generateState(HttpServletRequest request, HttpServletResponse response) {
    // Create a state token to prevent request forgery.
    // Store it in the session for later validation.
    String state = new BigInteger(130, new SecureRandom()).toString(32);
    response.addCookie(newCookieBuilder(request).setName(CSRF_STATE_COOKIE).setValue(sha256Hex(state)).setHttpOnly(true).setExpiry(-1).build());
    return state;
}
Also used : BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom)

Example 28 with SecureRandom

use of java.security.SecureRandom in project sonarqube by SonarSource.

the class AesCipher method generateRandomSecretKey.

String generateRandomSecretKey() {
    try {
        KeyGenerator keyGen = KeyGenerator.getInstance(CRYPTO_KEY);
        keyGen.init(KEY_SIZE_IN_BITS, new SecureRandom());
        SecretKey secretKey = keyGen.generateKey();
        return Base64.encodeBase64String(secretKey.getEncoded());
    } catch (Exception e) {
        throw new IllegalStateException("Fail to generate secret key", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) IOException(java.io.IOException)

Example 29 with SecureRandom

use of java.security.SecureRandom in project RxCache by VictorAlbertos.

the class BuiltInEncryptor method generateSecretKey.

private SecretKeySpec generateSecretKey(String key) throws Exception {
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes("UTF-8"));
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(KEY_LENGTH, secureRandom);
    SecretKey secretKey = keyGenerator.generateKey();
    return new SecretKeySpec(secretKey.getEncoded(), "AES");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 30 with SecureRandom

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;
}
Also used : CertificateSerialNumber(sun.security.x509.CertificateSerialNumber) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom)

Aggregations

SecureRandom (java.security.SecureRandom)639 SSLContext (javax.net.ssl.SSLContext)94 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)87 IOException (java.io.IOException)68 Test (org.junit.Test)62 SecretKey (javax.crypto.SecretKey)59 KeyGenerator (javax.crypto.KeyGenerator)53 X509Certificate (java.security.cert.X509Certificate)50 TrustManager (javax.net.ssl.TrustManager)50 Cipher (javax.crypto.Cipher)43 X509TrustManager (javax.net.ssl.X509TrustManager)40 KeyPairGenerator (java.security.KeyPairGenerator)38 BigInteger (java.math.BigInteger)35 InvalidKeyException (java.security.InvalidKeyException)34 CertificateException (java.security.cert.CertificateException)33 KeyPair (java.security.KeyPair)30 KeyStore (java.security.KeyStore)29 Random (java.util.Random)28 SecretKeySpec (javax.crypto.spec.SecretKeySpec)28 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)27