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:cn.cuizuoli.appranking.http.client.TrustSSLSocketFactoryFactoryBean.java

@Override
public SSLSocketFactory getObject() throws Exception {
    try {//w ww .j a  v a 2  s  .  com
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
        return sc.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.cloudwave.wp5.feedbackhandler.model.factories.ApplicationFactoryImpl.java

/**
 * {@inheritDoc}//from w ww  .  j  av  a2  s .c  o m
 */
@Override
public DbApplication create(final String applicationId) {
    final String accessToken = new BigInteger(130, new SecureRandom()).toString(32);
    return new DbApplicationImpl(applicationId, accessToken);
}

From source file:com.rr.familyPlanning.ui.security.encryptObject.java

/**
 * Encrypts and encodes the Object and IV for url inclusion
 *
 * @param input//from   www .j  a va  2s  .c om
 * @return
 * @throws Exception
 */
public String[] encryptObject(Object obj) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    try {
        // Serialize the object
        out.writeObject(obj);
        byte[] serialized = stream.toByteArray();
        // Setup the cipher and Init Vector
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] iv = new byte[cipher.getBlockSize()];
        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        // Hash the key with SHA-256 and trim the output to 128-bit for the key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(keyString.getBytes());
        byte[] key = new byte[16];
        System.arraycopy(digest.digest(), 0, key, 0, key.length);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        // encrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        // Encrypt & Encode the input
        byte[] encrypted = cipher.doFinal(serialized);
        byte[] base64Encoded = Base64.encodeBase64(encrypted);
        String base64String = new String(base64Encoded);
        String urlEncodedData = URLEncoder.encode(base64String, "UTF-8");
        // Encode the Init Vector
        byte[] base64IV = Base64.encodeBase64(iv);
        String base64IVString = new String(base64IV);
        String urlEncodedIV = URLEncoder.encode(base64IVString, "UTF-8");

        return new String[] { urlEncodedData, urlEncodedIV };
    } finally {
        stream.close();
        out.close();
    }
}

From source file:UuidGenerator.java

/**
 * Initializes the factory./*from w w  w . ja v  a  2  s  . c o  m*/
 *
 * @param obj
 */
private synchronized static void initialize(final Object obj) {
    try {
        InetAddress inet = InetAddress.getLocalHost();
        byte[] bytes = inet.getAddress();
        String hexInetAddress = hexFormat(getInt(bytes), 8);
        String thisHashCode = hexFormat(System.identityHashCode(obj), 8);
        s_midValue = hexInetAddress + thisHashCode;
        s_seeder = new SecureRandom();
        s_seeder.nextInt();
    } catch (java.net.UnknownHostException e) {
        throw new Error("can not initialize the UuidGenerator generator");
    }
    s_initialized = true;
}

From source file:pl.bcichecki.rms.client.android.services.clients.restful.https.ssl.SimpleSSLSocketFactory.java

public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);
    sslContext = SSLContext.getInstance("SSL");
    x509TrustManager = new SimpleX509TrustManager();
    sslContext.init(null, new TrustManager[] { x509TrustManager }, new SecureRandom());
}

From source file:org.obiba.onyx.core.identifier.impl.randomincrement.RandomIncrementIdentifierSequence.java

public RandomIncrementIdentifierSequence() {
    random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);/*from   w w  w . j a v  a2s  . c  om*/
}

From source file:ti.modules.titanium.network.NonValidatingSSLSocketFactory.java

public NonValidatingSSLSocketFactory() {
    try {//from   w w w.  java2  s .c om
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManager managers[] = new TrustManager[] { new NonValidatingTrustManager() };
        context.init(null, managers, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }

}

From source file:dtu.ds.warnme.app.ws.client.https.ssl.SimpleSSLSocketFactory.java

@SuppressLint("TrulyRandom")
public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);
    sslContext = SSLContext.getInstance("SSL");
    x509TrustManager = new SimpleX509TrustManager();
    sslContext.init(null, new TrustManager[] { x509TrustManager }, new SecureRandom());
}

From source file:course.SessionDAO.java

public String startSession(String username) {

    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);/*  w  w  w  .  j  a v  a2 s  .  c o m*/

    // result = new Base64().encodeToString(rawHmac);

    // BASE64Encoder encoder = new BASE64Encoder();

    // String sessionID = encoder.encode(randomBytes);

    String sessionID = new Base64().encodeToString(randomBytes);

    // build the BSON object
    BasicDBObject session = new BasicDBObject("username", username);

    session.append("_id", sessionID);

    sessionsCollection.insert(session);

    return session.getString("_id");
}

From source file:com.sandopolus.yeildify.service1.resources.RamdomResourceImpl.java

@PostConstruct
public void init() {
    RAND = new SecureRandom();
    JSON_MAPPER = new ObjectMapper();
    HTTP_CLIENT = HttpClientBuilder.create().build();
}