Example usage for java.security SecureRandom getInstance

List of usage examples for java.security SecureRandom getInstance

Introduction

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

Prototype

public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.

Usage

From source file:lti.oauth.OAuthUtil.java

public static String generateNonce() throws Exception {
    Random rand = SecureRandom.getInstance("SHA1PRNG");
    return String.valueOf(rand.nextLong());
}

From source file:org.alfresco.encryption.KeyStoreTests.java

public byte[] generateKeyData() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed(System.currentTimeMillis());
    byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
    random.nextBytes(bytes);/*from  w w  w .  j  a v a 2 s.  co  m*/
    return bytes;
}

From source file:com.trifork.riak.RiakClient.java

/**
 * helper method to use a reasonable default client id
 * //from w w w . j a  va 2 s.  c  om
 * @throws IOException
 */
public void prepareClientID() throws IOException {
    Preferences prefs = Preferences.userNodeForPackage(RiakClient.class);

    String clid = prefs.get("client_id", null);
    if (clid == null) {
        SecureRandom sr;
        try {
            sr = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] data = new byte[6];
        sr.nextBytes(data);
        clid = Base64Coder.encodeLines(data);
        prefs.put("client_id", clid);
        try {
            prefs.flush();
        } catch (BackingStoreException e) {
            throw new IOException(e);
        }
    }

    setClientID(clid);
}

From source file:dk.netarkivet.common.distribute.HTTPSRemoteFileRegistry.java

private HTTPSRemoteFileRegistry() {
    FileInputStream keyStoreInputStream = null;
    try {//from w  ww .ja v a 2  s .  co  m
        keyStoreInputStream = new FileInputStream(KEYSTORE_PATH);
        KeyStore store = KeyStore.getInstance(SUN_JCEKS_KEYSTORE_TYPE);
        store.load(keyStoreInputStream, KEYSTORE_PASSWORD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(SUN_X509_CERTIFICATE_ALGORITHM);
        kmf.init(store, KEY_PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(SUN_X509_CERTIFICATE_ALGORITHM);
        tmf.init(store);
        sslContext = SSLContext.getInstance(SSL_PROTOCOL);
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
                SecureRandom.getInstance(SHA1_PRNG_RANDOM_ALGORITHM));
    } catch (GeneralSecurityException | IOException e) {
        throw new IOFailure("Unable to create secure environment for keystore '" + KEYSTORE_PATH + "'", e);
    } finally {
        IOUtils.closeQuietly(keyStoreInputStream);
    }
}

From source file:com.appunite.websocket.WebSocket.java

/**
 * Create instance of WebSocket/*  w  w  w .j av a  2 s .c  o m*/
 * 
 * @param listener
 *            where all calls will be thrown
 */
public WebSocket(WebSocketListener listener) {
    checkArgument(listener != null, "Lister cannot be null");
    this.mListener = listener;

    SecureRandom secureRandom;
    try {
        secureRandom = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        // if we do not have secure random we have to leave data unmasked
        secureRandom = null;
    }
    if (secureRandom == null) {
        mSecureRandom = Optional.absent();
    } else {
        mSecureRandom = Optional.of(secureRandom);
    }
}

From source file:com.cloud.server.auth.SHA256SaltedUserAuthenticator.java

@Override
public String encode(String password) {
    // 1. Generate the salt
    SecureRandom randomGen;/*from  w  w  w.  ja  v  a 2s .  c o  m*/
    try {
        randomGen = SecureRandom.getInstance("SHA1PRNG");

        byte salt[] = new byte[s_saltlen];
        randomGen.nextBytes(salt);

        String saltString = new String(Base64.encode(salt));
        String hashString = encode(password, salt);

        // 3. concatenate the two and return
        return saltString + ":" + hashString;
    } catch (NoSuchAlgorithmException e) {
        throw new CloudRuntimeException("Unable to hash password", e);
    } catch (UnsupportedEncodingException e) {
        throw new CloudRuntimeException("Unable to hash password", e);
    }
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.getRandomAlphaNumericString instead
 * @return// w  w  w .j a  v  a  2 s.  c  o  m
 */
@Deprecated
public static synchronized String getRandomAlphaNumericString(int length) {
    try {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        Random rand = new Random();
        char buff[] = new char[length];
        for (int i = 0; i < length; ++i) {
            // reseed rand once you've used up all available entropy bits
            if ((i % 10) == 0)
                rand.setSeed(sr.nextLong()); // 64 bits of random!
            buff[i] = VALID_CHARACTERS[rand.nextInt(VALID_CHARACTERS.length)];
        }
        return new String(buff);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:com.tremolosecurity.proxy.SessionManagerImpl.java

public SessionManagerImpl(ConfigManager cfg, ServletContext ctx) {
    sessions = new ConcurrentHashMap<String, TremoloHttpSession>();
    this.ctx = ctx;
    try {//from  w w w  .  j a  va 2  s .c  om
        this.random = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        logger.error("Could not load secure random", e);
    }

    this.cfg = cfg;

    for (String key : this.cfg.getAuthChains().keySet()) {
        AuthChainType act = this.cfg.getAuthChains().get(key);
        if (act.getLevel() == 0) {
            this.anonChainType = act;
            String mechName = act.getAuthMech().get(0).getName();
            this.anonMech = (AnonAuth) cfg.getAuthMech(cfg.getAuthMechs().get(mechName).getUri());
        }
    }

    if (this.anonMech == null) {
        this.anonChainType = new AuthChainType();
        this.anonChainType.setFinishOnRequiredSucess(true);
        this.anonChainType.setLevel(0);
        this.anonChainType.setName("anon");

        this.anonMech = new AnonAuth();

    }

    checker = new SessionTimeoutChecker(this.cfg, this);
    checker.start();

    if (cfg.getCfg().getApplications().getOpenSessionCookieName() == null) {
        cfg.getCfg().getApplications().setOpenSessionCookieName("TREMOLO_SESSION");
    }

}

From source file:org.apache.cloudstack.server.auth.PBKDF2UserAuthenticator.java

public static byte[] makeSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    byte[] salt = new byte[s_saltlen];
    sr.nextBytes(salt);/*from w  w  w. jav  a2  s.  co  m*/
    return salt;
}

From source file:com.basho.riak.pbc.RiakClient.java

/**
 * helper method to use a reasonable default client id
 * beware, it caches the client id. If you call it multiple times on the same client
 * you get the *same* id (not good for reusing a client with different ids)
 * /* w  w w .java2 s  .  c om*/
 * @throws IOException
 */
public void prepareClientID() throws IOException {
    Preferences prefs = Preferences.userNodeForPackage(RiakClient.class);

    String clid = prefs.get("client_id", null);
    if (clid == null) {
        SecureRandom sr;
        try {
            sr = SecureRandom.getInstance("SHA1PRNG");
            // Not totally secure, but doesn't need to be
            // and 100% less prone to 30 second hangs on linux jdk5
            sr.setSeed(UUID.randomUUID().getLeastSignificantBits() + new Date().getTime());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] data = new byte[6];
        sr.nextBytes(data);
        clid = CharsetUtils.asString(Base64.encodeBase64Chunked(data), CharsetUtils.ISO_8859_1);
        prefs.put("client_id", clid);
        try {
            prefs.flush();
        } catch (BackingStoreException e) {
            throw new IOException(e.toString());
        }
    }

    setClientID(clid);
}