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:org.addhen.smssync.net.ssl.TrustedSocketFactory.java

public TrustedSocketFactory(String host, boolean secure)
        throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { TrustManagerFactory.get(host, secure) }, new SecureRandom());
    mSocketFactory = sslContext.getSocketFactory();
    mSchemeSocketFactory = org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
    mSchemeSocketFactory//from w w w  . j  a  v a2s . c  o  m
            .setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}

From source file:camera.Camera.java

public Camera() throws MqttException, SocketException {
    System.out.println("Log4J: " + HoraFile.getCanonicalPath(LOG4J));
    System.out.println("Ini:   " + HoraFile.getCanonicalPath(INI));
    DOMConfigurator.configureAndWatch(LOG4J, 10 * 1000);
    MQTTLINK = HoraIni.LeseIniString(INI, "MQTT", "LINK_PORT", MQTTLINK, true);
    MQTTLINK = "tcp://" + MQTTLINK;
    ///*www. j  ava  2  s. co  m*/
    this.mac = TestMac.getMacAddress("wlan");
    MemoryPersistence persistence = new MemoryPersistence();
    SecureRandom random = new SecureRandom();
    String id = new BigInteger(60, random).toString(32);
    System.out.println("camid=" + id);
    client = new MqttClient(MQTTLINK, id, persistence);
    client.setTimeToWait(5000);
}

From source file:org.mifos.tools.provider.RestAdapterProvider.java

private OkHttpClient createClient() {

    final OkHttpClient client = new OkHttpClient();

    final TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override//  w w  w  .  j  av  a  2  s. co m
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (final java.security.GeneralSecurityException ex) {
        // do nothing, ignore
    }

    try {
        final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(final String hostname, final SSLSession session) {
                return true;
            }
        };
        client.setHostnameVerifier(hostnameVerifier);
        client.setSslSocketFactory(ctx.getSocketFactory());
    } catch (final Exception e) {
        // do nothing, ignore
    }

    return client;
}

From source file:estacionamento.util.HTTPUtil.java

/**
 *
 * @return//w  w  w  .  j av a 2  s .  com
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, trustAllCerts, new SecureRandom());

    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

From source file:com.codesharp.cooper.plugins.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*  w w w.  j a  v a 2 s. c om*/
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

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

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.framework.library.connection.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {// w w  w.j a va2  s . c om
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

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

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.assignment4.security.ValidateSaltFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    // Assume its HTTP
    HttpServletRequest httpReq = (HttpServletRequest) request;

    // Get the salt sent with the request
    String salt = (String) httpReq.getParameter("randId");
    // Validate that the salt is in the cache
    Cache<String, Boolean> csrfPreventionSaltCache = (Cache<String, Boolean>) httpReq.getSession()
            .getAttribute("csrfPreventionSaltCache");

    if (csrfPreventionSaltCache != null && salt == null
            && (null != request.getParameter("activationtoken") || null != request.getParameter("crypt"))) {

        csrfPreventionSaltCache = CacheBuilder.newBuilder().maximumSize(5000)
                .expireAfterWrite(20, TimeUnit.MINUTES).build();

        httpReq.getSession().setAttribute("csrfPreventionSaltCache", csrfPreventionSaltCache);

        // Generate the salt and store it in the users cache
        salt = RandomStringUtils.random(20, 0, 0, true, true, null, new SecureRandom());
        csrfPreventionSaltCache.put(salt, Boolean.TRUE);

        // Add the salt to the current request so it can be used
        // by the page rendered in this request
        httpReq.setAttribute("randId", salt);
    }//  w ww . ja v a  2 s. co  m

    if (csrfPreventionSaltCache != null && salt != null && csrfPreventionSaltCache.getIfPresent(salt) != null) {

        // If the salt is in the cache, we move on
        chain.doFilter(request, response);
    } else {
        // Otherwise we throw an exception aborting the request flow
        throw new ServletException("Potential CSRF detected!! Inform a scary sysadmin ASAP.");
    }
}

From source file:de.gfred.lbbms.mobile.services.IgnoreSelfCertificatesSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   w  w w . ja v  a 2  s.  c o m*/
        SSLContext context = SSLContext.getInstance("TLS");

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                // do nothing
            }

        } };

        context.init(null, trustAllCerts, new SecureRandom());

        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.andiandy.m101j.week4.course.SessionDAO.java

public String getSessionID() {
    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);/*  w ww  .  ja v a 2s  . c om*/

    byte[] idBytes = Base64.encodeBase64(randomBytes);
    String sessionID = new String(idBytes);

    return sessionID;
}

From source file:com.andiandy.m101j.week3.hw2_3.SessionDAO.java

public String generateSessionID() {
    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);//from w  w w  . j a  v  a2  s. c  om

    byte[] idBytes = Base64.encodeBase64(randomBytes);
    String sessionID = new String(idBytes);

    return sessionID;
}