Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

In this page you can find the example usage for java.security SecureRandom SecureRandom.

Prototype

public SecureRandom() 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:com.twitter.hbc.httpclient.auth.OAuth1.java

public OAuth1(String consumerKey, String consumerSecret, String token, String tokenSecret) {
    this.consumerKey = Preconditions.checkNotNull(consumerKey);
    this.consumerSecret = Preconditions.checkNotNull(consumerSecret);

    this.token = Preconditions.checkNotNull(token);
    this.tokenSecret = Preconditions.checkNotNull(tokenSecret);

    this.normalizer = Normalizer.getStandardNormalizer();
    this.signer = Signer.getStandardSigner();

    this.secureRandom = new SecureRandom();
}

From source file:com.openvcx.conference.KeyGenerator.java

/**
 * Constructor used to initialize this instance
 */
public KeyGenerator() {
    m_rand = new SecureRandom();
}

From source file:com.naver.timetable.bo.HttpClientBO.java

public String getHttpBody(String url, String method, List<NameValuePair> param) {
    HttpClient httpClient = null;/*from   w  w  w  . ja  va  2 s. c o m*/
    HttpResponse httpResponse = null;
    HttpRequestBase httpRequest;

    try {
        if (StringUtils.upperCase(method).equals("POST")) {
            httpRequest = new HttpPost(url);
            ((HttpPost) httpRequest).setEntity(new UrlEncodedFormEntity(param));
        } else {
            httpRequest = new HttpGet(url);
        }

        TrustManager[] trustManagers = new TrustManager[1];
        trustManagers[0] = new DefaultTrustManager();

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(new KeyManager[0], trustManagers, new SecureRandom());
        SSLContext.setDefault(sslContext);

        sslContext.init(null, trustManagers, null);
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

        //         httpClient = HttpClientBuilder.create().build();
        httpResponse = httpClient.execute(httpRequest);
        return EntityUtils.toString(httpResponse.getEntity());
    } catch (ClientProtocolException e) {
        LOG.error("Client protocol error : ", e);
    } catch (IOException e) {
        LOG.error("IO error : ", e);
    } catch (KeyManagementException e) {
        LOG.error("IO error : ", e);
    } catch (NoSuchAlgorithmException e) {
        LOG.error("IO error : ", e);
    } finally {
        // ?
        HttpClientUtils.closeQuietly(httpResponse);
        HttpClientUtils.closeQuietly(httpClient);
    }

    return null;
}

From source file:com.zimbra.cs.service.mail.SendVerificationCode.java

static String generateVerificationCode() {
    ZimbraLog.misc.debug("Generating verification code");
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[3];
    random.nextBytes(bytes);/*from   w ww  . j a  v a2  s .c o m*/
    return new String(Hex.encodeHex(bytes));
}

From source file:org.everit.authentication.cas.ecm.tests.SecureHttpClient.java

/**
 * Constructor.//from www .ja va2  s .  c o  m
 */
public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception {
    this.principal = principal;

    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(new BasicCookieStore());

    KeyStore trustStore = KeyStore.getInstance("jks");
    trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(),
            "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, new SecureRandom());

    httpClient = HttpClientBuilder.create().setSslcontext(sslContext)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}

From source file:client.authz.Configuration.java

/**
 * If this is a https authServerUrl and noCertCheck is true, create an SSLContext that uses
 * an X509TrustManager that allows any certificate.
 * @return SSLContext with all trusting TrustManager if noCertCheck is true, null otherwise
 *///w w  w. j  a  v  a2 s.c  o m
public SSLContext getSSLContext() {
    SSLContext sslContext = null;
    if (authServerUrl.startsWith("https") && noCertCheck) {
        try {
            // Install a TrustManager that ignores certificate checks
            sslContext = SSLContext.getInstance("TLS");
            TrustManager[] trustManagers = { new TrustAllManager() };
            sslContext.init(null, trustManagers, new SecureRandom());
        } catch (Exception e) {
            throw new IllegalStateException("Failed to create HttpsClient", e);
        }
    }

    return sslContext;
}

From source file:com.cttapp.bby.mytlc.layer8apps.SimpleSSLSocketFactory.java

public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(null);/*from  w  w  w  .  ja v  a2s .com*/

    try {
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains and simply
        // accept all type of certificates
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }

            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }
        } };

        // Initialize the socket factory
        context.init(null, trustAllCerts, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.solarnetwork.pki.bc.test.BCCertificateServiceTest.java

@Before
public void setup() {
    service = new BCCertificateService();
    KeyPairGenerator keyGen;//  w w w  . jav a  2  s .  co  m
    try {
        keyGen = KeyPairGenerator.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    keyGen.initialize(2048, new SecureRandom());
    KeyPair keypair = keyGen.generateKeyPair();
    publicKey = keypair.getPublic();
    privateKey = keypair.getPrivate();
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Generates random salt string/*from w  w  w. j av a2 s . com*/
 * @return random salt string
 */
public static String nextSalt() {
    SecureRandom random = new SecureRandom();
    return new BigInteger(130, random).toString(32);
}

From source file:org.everit.osgi.authentication.cas.tests.SecureHttpClient.java

public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception {
    this.principal = principal;

    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(new BasicCookieStore());

    KeyStore trustStore = KeyStore.getInstance("jks");
    trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(),
            "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, new SecureRandom());

    httpClient = HttpClientBuilder.create().setSslcontext(sslContext)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}