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:de.petendi.commons.crypto.HybridCrypto.java

private synchronized void createSymmetricPassphrase() {
    if (symmetricKey == null) {
        symmetricKey = securityProviderConnector.generateSecretKey();
        SecureRandom randomSecureRandom = new SecureRandom();
        iv = new byte[16];
        randomSecureRandom.nextBytes(iv);
        byte[] encodedKey = symmetricKey.getEncoded();
        concatenated = new byte[iv.length + encodedKey.length];
        System.arraycopy(iv, 0, concatenated, 0, iv.length);
        System.arraycopy(encodedKey, 0, concatenated, iv.length, encodedKey.length);
    }/*from ww  w.j a v a2s  . co  m*/
}

From source file:org.marietjedroid.connect.MarietjeMessenger.java

/**
 * Generates a new token//from   www.  ja v  a2 s .co  m
 * 
 * @return a new token
 */
protected static String generateToken() {
    SecureRandom random = new SecureRandom();
    while (true) {
        String attempt = new BigInteger(130, random).toString();
        attempt = new String(org.apache.commons.codec.binary.Base64.encodeBase64(attempt.getBytes()))
                .substring(0, 7);

        return attempt;
    }
}

From source file:org.reficio.ws.client.ssl.SSLUtils.java

public static SSLSocketFactory getMergedSocketFactory(org.reficio.ws.client.core.Security securityOne,
        Security securityTwo) throws GeneralSecurityException {
    X509KeyManager keyManagerOne = getKeyManager(securityOne.getKeyStore(), securityOne.getKeyStorePassword());
    X509KeyManager keyManagerTwo = getKeyManager(securityTwo.getKeyStore(), securityTwo.getKeyStorePassword());

    X509TrustManager trustManager = getMultiTrustManager(getTrustManager(securityOne.getTrustStore()),
            getTrustManager(securityTwo.getTrustStore()));

    SSLContext context = SSLContext.getInstance(securityOne.getSslContextProtocol());
    boolean strictHostVerification = securityOne.isStrictHostVerification()
            && securityTwo.isStrictHostVerification();

    context.init(new KeyManager[] { keyManagerOne, keyManagerTwo }, new TrustManager[] { trustManager },
            new SecureRandom());
    X509HostnameVerifier verifier = strictHostVerification ? SSLSocketFactory.STRICT_HOSTNAME_VERIFIER
            : SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    return new SSLSocketFactory(context, verifier);
}

From source file:org.sana.net.http.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from   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     
        context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:fi.vm.kapa.identification.service.PhaseIdService.java

/**
 * Creates new standard length token ID that is used to calculate
 * the phase ID./* w  ww  . j  av a 2  s  .  c om*/
 *
 * @return New token ID as alphanumeric string
 */
public String nextTokenId() {
    String token = new BigInteger(130, new SecureRandom()).toString(32);
    while (token.length() != TID_LENGTH) {
        token = new BigInteger(130, new SecureRandom()).toString(32);
    }
    return token;
}

From source file:org.jasig.cas.web.flow.CasFlowExecutionKeyFactory.java

private static byte[] getRandomSalt(final int size) {
    final SecureRandom secureRandom = new SecureRandom();
    final byte[] bytes = new byte[size];

    secureRandom.nextBytes(bytes);// w  w  w .  j  ava2s  .c o  m
    return bytes;
}

From source file:net.mc_cubed.msrp.MsrpUtil.java

/**
 * Generate a RFC 4975 Section 7.1 compliant transaction identifier
 * <p>/*from  w  w w  .j ava2  s  . c  om*/
 * To form a new request, the sender creates a transaction identifier and
 * uses this and the method name to create an MSRP request start line. The
 * transaction identifier MUST NOT collide with that of other transactions
 * that exist at the same time. Therefore, it MUST contain at least 64 bits
 * of randomness.
 */
public static String generateMsrpTransactionId() {
    SecureRandom secure = new SecureRandom();
    byte[] bytes = new byte[MSRP_TX_ID_LENGTH];
    secure.nextBytes(bytes);
    return Base64.encodeBase64String(bytes);
}

From source file:org.cloudfoundry.identity.uaa.login.feature.ResetPasswordIT.java

@Before
public void setUp() throws Exception {
    int randomInt = new SecureRandom().nextInt();

    String adminAccessToken = testClient.getOAuthAccessToken("admin", "adminsecret", "client_credentials",
            "clients.read clients.write clients.secret");
    String scimClientId = "scim" + randomInt;
    testClient.createScimClient(adminAccessToken, scimClientId);
    String scimAccessToken = testClient.getOAuthAccessToken(scimClientId, "scimsecret", "client_credentials",
            "scim.read scim.write password.write");
    userEmail = "user" + randomInt + "@example.com";
    testClient.createUser(scimAccessToken, userEmail, userEmail, "secret", true);
}

From source file:com.zimbra.common.util.RandomPassword.java

public static String generate(int minLength, int maxLength, String alphabet) {
    SecureRandom random = new SecureRandom();

    // Calculate the desired length of the password
    int length;/*  ww w.  ja  va2  s. com*/
    if (minLength > maxLength) {
        throw new IllegalArgumentException("minLength=" + minLength + " > maxLength=" + maxLength);
    } else if (minLength < maxLength) {
        length = minLength + random.nextInt(1 + maxLength - minLength);
    } else {
        length = maxLength;
    }

    int alphabetLength = alphabet.length();
    int limit = byteLimit(alphabetLength);

    StringBuffer password = new StringBuffer(length);
    byte[] randomByte = new byte[1];

    while (password.length() < length) {
        random.nextBytes(randomByte);
        int i = randomByte[0] + 128;
        if (i < limit) {
            password.append(alphabet.charAt(i % alphabetLength));
        }
    }

    return password.toString();
}