Example usage for java.security SecureRandom nextInt

List of usage examples for java.security SecureRandom nextInt

Introduction

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

Prototype

public int nextInt() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

Usage

From source file:spade.client.CommandLine.java

private static void setupClientSSLContext() throws Exception {
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextInt();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(serverKeyStorePublic);//www .ja  va  2  s.  co m
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(clientKeyStorePrivate, "private".toCharArray());

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), secureRandom);
    sslSocketFactory = sslContext.getSocketFactory();
}

From source file:org.apache.accumulo.tserver.DirectoryDecommissioner.java

private static Path getBackupName(FileSystem fs, Path path) {
    SecureRandom rand = new SecureRandom();
    return new Path(path.getParent(),
            path.getName() + "_" + System.currentTimeMillis() + "_" + Math.abs(rand.nextInt()) + ".bak");
}

From source file:org.wso2.carbon.appmgt.usage.publisher.APPMgtGoogleAnalayticsHandler.java

/**
 * Get a random number string./*w  w  w .ja  v a 2s  .c om*/
 *
 * @return
 */
private static String getRandomNumber() {
    SecureRandom secureRandom = new SecureRandom();
    return Integer.toString((int) (secureRandom.nextInt() * 0x7fffffff));
}

From source file:org.eclipse.thym.ios.core.pbxproject.PBXProject.java

public static String generateReference() {
    try {//  ww  w.j  av a 2 s .  c o  m
        MessageDigest md = MessageDigest.getInstance("SHA1");
        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        String randomNum = Integer.toString(prng.nextInt());
        String ref = new String(Hex.encodeHex(md.digest(randomNum.getBytes())));
        return ref.toUpperCase().substring(0, 24);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:com.proctorcam.proctorserv.HashedAuthenticator.java

public static HashMap<String, Object> applyReverseGuidAndSign(HashMap<String, Object> params,
        String customer_identifier, String shared_secret) {
    //Create random GUID
    SecureRandom secRandom = null;
    try {/*w  w w  .j  ava 2  s .  co  m*/
        secRandom = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException ex) {
        System.err.println(ex.getMessage());
    }
    String guid = new Integer(secRandom.nextInt()).toString();
    params.put("customer_id", customer_identifier);
    params.put("guid", guid);
    params.put("signature", "");

    //Create query string using key-value pairs in params and
    //appending shared_secret
    StringBuilder query = new StringBuilder();

    for (String key : params.keySet()) {
        if (key != "signature") {
            Object data = params.get(key);

            if (is_hashmap(data)) {
                HashMap<String, Object> map = (HashMap<String, Object>) data;
                String hashMapQuery = hashQuery(key, map);
                query.append(hashMapQuery);

            } else if (is_array(data)) {
                Object[] array = getArray(data);
                String arrayString = arrayQuery(key, array);
                query.append(arrayString);

            } else {
                query.append(key).append("=").append(data).append("&");
            }
        }
    }

    //Deletes trailing & from query and append shared_secret
    query.deleteCharAt(query.length() - 1);
    query.append(shared_secret);
    String signature = buildSig(query);

    //Add signature to params
    params.put("signature", signature);

    //Reverse guid in params
    String reverseGUID = new StringBuilder(guid).reverse().toString();
    params.put("guid", reverseGUID);

    return params;
}

From source file:org.wso2.carbon.identity.core.util.IdentityUtil.java

public static int getRandomInteger() throws IdentityException {

    try {/*from   w  w w. j  a v a2  s. co  m*/
        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        int number = prng.nextInt();
        while (number < 0) {
            number = prng.nextInt();
        }
        return number;
    } catch (NoSuchAlgorithmException e) {
        log.error("Error when generating a random number.", e);
        throw new IdentityException("Error when generating a random number.", e);
    }

}

From source file:org.jboss.tools.aerogear.hybrid.ios.core.pbxproject.PBXProject.java

public static String generateReference() {
    MessageDigest md = null;/*from  w ww . j  a  v a 2s  . c  o  m*/
    SecureRandom prng = null;
    try {
        md = MessageDigest.getInstance("SHA1");
        prng = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    }

    String randomNum = new Integer(prng.nextInt()).toString();
    String ref = new String(Hex.encodeHex(md.digest(randomNum.getBytes())));
    return ref.toUpperCase().substring(0, 24);
}

From source file:org.wso2.carbon.identity.core.util.IdentityUtil.java

/**
 * Generates a secure random hexadecimal string using SHA1 PRNG and digest
 *
 * @return Random hexadecimal encoded String
 * @throws Exception/*from  w  w  w  .jav  a2s  . c o m*/
 */
public static String generateUUID() throws Exception {

    try {
        // SHA1 Pseudo Random Number Generator
        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");

        // random number
        String randomNum = Integer.toString(prng.nextInt());
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] digest = sha.digest(randomNum.getBytes());

        // Hexadecimal encoding
        return new String(Hex.encodeHex(digest));

    } catch (NoSuchAlgorithmException e) {
        throw new Exception("Failed to generate UUID ", e);
    }
}

From source file:de.tntinteractive.portalsammler.engine.SecureStore.java

public static SecureStore readFrom(final StorageLayer storage, final SecureRandom srand, final byte[] key)
        throws IOException, GeneralSecurityException {

    final SecureStore ret = new SecureStore(storage, srand, key);

    readSettings(storage, srand, key, ret);
    readIndex(storage, srand, key, ret);
    ret.setCurrentFileIndexToHighestKnown();
    ret.currentOutputBuffer = new ByteArrayOutputStream();
    if (storage.fileExists(ret.getCurrentFilename())) {
        ret.currentOutputBuffer.write(ret.readAndDecrypt(ret.getCurrentFilename()));
    } else {//from  w  w w .  ja va2 s  .c  o m
        writeInt(ret.currentOutputBuffer, srand.nextInt());
    }
    ret.checkCurrentBufferSize();
    return ret;
}

From source file:org.openhie.openempi.recordlinkage.protocols.ThreeThirdPartyCBFProtocol.java

protected int getNonce() {
    SecureRandom rnd = new SecureRandom();
    return rnd.nextInt();
}