Search in sources :

Example 36 with SecureRandom

use of java.security.SecureRandom in project blade by biezhi.

the class HttpRequest method getTrustedFactory.

/**
	 * @return 返回SSL套接字工厂
	 * @throws HttpRequestException
	 */
private static SSLSocketFactory getTrustedFactory() throws HttpRequestException {
    if (TRUSTED_FACTORY == null) {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            // Intentionally left blank
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            // Intentionally left blank
            }
        } };
        try {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, trustAllCerts, new SecureRandom());
            TRUSTED_FACTORY = context.getSocketFactory();
        } catch (GeneralSecurityException e) {
            IOException ioException = new IOException("Security exception configuring SSL context");
            ioException.initCause(e);
            throw new HttpRequestException(ioException);
        }
    }
    return TRUSTED_FACTORY;
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) X509Certificate(java.security.cert.X509Certificate)

Example 37 with SecureRandom

use of java.security.SecureRandom in project spring-security by spring-projects.

the class SecureRandomFactoryBeanTests method testCreatesUsingDefaults.

@Test
public void testCreatesUsingDefaults() throws Exception {
    SecureRandomFactoryBean factory = new SecureRandomFactoryBean();
    Object result = factory.getObject();
    assertThat(result).isInstanceOf(SecureRandom.class);
    int rnd = ((SecureRandom) result).nextInt();
    assertThat(rnd).isNotEqualTo(0);
}
Also used : SecureRandom(java.security.SecureRandom) SecureRandomFactoryBean(org.springframework.security.core.token.SecureRandomFactoryBean) Test(org.junit.Test)

Example 38 with SecureRandom

use of java.security.SecureRandom in project spring-security by spring-projects.

the class SecureRandomFactoryBeanTests method testCreatesUsingSeed.

@Test
public void testCreatesUsingSeed() throws Exception {
    SecureRandomFactoryBean factory = new SecureRandomFactoryBean();
    Resource resource = new ClassPathResource("org/springframework/security/core/token/SecureRandomFactoryBeanTests.class");
    assertThat(resource).isNotNull();
    factory.setSeed(resource);
    Object result = factory.getObject();
    assertThat(result).isInstanceOf(SecureRandom.class);
    int rnd = ((SecureRandom) result).nextInt();
    assertThat(rnd).isNotEqualTo(0);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) SecureRandom(java.security.SecureRandom) ClassPathResource(org.springframework.core.io.ClassPathResource) SecureRandomFactoryBean(org.springframework.security.core.token.SecureRandomFactoryBean) Test(org.junit.Test)

Example 39 with SecureRandom

use of java.security.SecureRandom in project spring-security by spring-projects.

the class BCryptPasswordEncoderTests method customRandom.

@Test
public void customRandom() {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(8, new SecureRandom());
    String result = encoder.encode("password");
    assertThat(encoder.matches("password", result)).isTrue();
}
Also used : SecureRandom(java.security.SecureRandom) Test(org.junit.Test)

Example 40 with SecureRandom

use of java.security.SecureRandom in project spring-security by spring-projects.

the class BouncyCastleAesBytesEncryptorTest method setup.

@Before
public void setup() {
    // generate random password, salt, and test data
    SecureRandom secureRandom = new SecureRandom();
    password = UUID.randomUUID().toString();
    byte[] saltBytes = new byte[16];
    secureRandom.nextBytes(saltBytes);
    salt = new String(Hex.encode(saltBytes));
    testData = new byte[1024 * 1024];
    secureRandom.nextBytes(testData);
}
Also used : SecureRandom(java.security.SecureRandom) Before(org.junit.Before)

Aggregations

SecureRandom (java.security.SecureRandom)720 SSLContext (javax.net.ssl.SSLContext)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)97 IOException (java.io.IOException)87 Test (org.junit.Test)76 SecretKey (javax.crypto.SecretKey)62 X509Certificate (java.security.cert.X509Certificate)61 KeyGenerator (javax.crypto.KeyGenerator)57 TrustManager (javax.net.ssl.TrustManager)56 X509TrustManager (javax.net.ssl.X509TrustManager)47 Cipher (javax.crypto.Cipher)46 KeyPairGenerator (java.security.KeyPairGenerator)44 BigInteger (java.math.BigInteger)42 CertificateException (java.security.cert.CertificateException)40 InvalidKeyException (java.security.InvalidKeyException)35 KeyPair (java.security.KeyPair)34 KeyStore (java.security.KeyStore)34 SecretKeySpec (javax.crypto.spec.SecretKeySpec)30 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)28 KeyManagementException (java.security.KeyManagementException)28