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:fi.ilmoeuro.membertrack.util.Crypto.java

public static String randomSalt() {
    byte[] randomBytes = new byte[32];
    SecureRandom random = new SecureRandom();
    random.nextBytes(randomBytes);/* w  w  w . j  a v a2s .c  o  m*/
    String salt = Hex.encodeHexString(randomBytes);
    return salt;
}

From source file:login.login_servlet.java

public String generateToken(int len) {
    String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    SecureRandom rnd = new SecureRandom();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++)
        sb.append(AB.charAt(rnd.nextInt(AB.length())));

    return sb.toString();

}

From source file:com.amazonaws.tvm.AESEncryption.java

private static byte[] getIv() throws Exception {
    byte[] iv = new byte[16];
    new SecureRandom().nextBytes(iv);

    return iv;/*from  ww w . j a  va  2  s.  c o  m*/
}

From source file:com.datasingularity.http.asyncget.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from   w ww .j a  v  a  2s. co  m
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new VeryTrustingTrustManager() }, new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.apabi.r2k.common.security.util.NonceUtils.java

/**
 * SecureRandom?Long.
 */
public static long randomLong() {
    return new SecureRandom().nextLong();
}

From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java

public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // ???/*w w  w.  j  a  v a  2  s.c  om*/
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed(seed); // ??
    // ??, ??keysize ?. 512  1024  64 ?
    keygen.initialize(512, secrand);
    // ?pubkey?prikey
    KeyPair keys = keygen.generateKeyPair(); // ?
    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded());
    OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME));
    ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();

    byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded());
    os = new FileOutputStream(new File(PRIKEY_FILE_NAME));
    bais = new ByteArrayInputStream(prikeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();
}

From source file:UUID.java

/**
 * DOCUMENT ME!//from  w  w w.  j a v  a 2 s. co m
 *
 * @return DOCUMENT ME!
 */
public static UUID randomUUID() {
    SecureRandom securerandom = numberGenerator;

    if (securerandom == null) {
        numberGenerator = securerandom = new SecureRandom();
    }

    byte[] abyte0 = new byte[16];
    securerandom.nextBytes(abyte0);
    abyte0[6] &= 0xf;
    abyte0[6] |= 0x40;
    abyte0[8] &= 0x3f;
    abyte0[8] |= 0x80;

    UUID uuid = new UUID(abyte0);

    return new UUID(abyte0);
}

From source file:com.google.gwtjsonrpc.server.SignedToken.java

/**
 * Generate a random key for use with the XSRF library.
 * //w  w w .jav a2 s .  c o  m
 * @return a new private key, base 64 encoded.
 */
public static String generateRandomKey() {
    final byte[] r = new byte[26];
    new SecureRandom().nextBytes(r);
    return encodeBase64(r);
}

From source file:co.cask.cdap.client.rest.RestUtil.java

public static Registry<ConnectionSocketFactory> getRegistryWithDisabledCertCheck()
        throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override/*from  ww w.j a  v a2 s  .com*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }
    } }, new SecureRandom());
    SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf)
            .register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
}

From source file:fr.cph.stock.security.Security.java

/**
 * Generate a salt, a random key, en encrypt it
 * /* w w w.  j  a  v a2  s.c  om*/
 * @return a key encrypted
 * @throws NoSuchAlgorithmException
 *             the NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 *             the UnsupportedEncodingException
 */
public static String generateSalt() throws NoSuchAlgorithmException, UnsupportedEncodingException {
    SecureRandom random = new SecureRandom();
    return Security.encodeToSha256(random.toString());
}