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:com.inflight.util.Password.java

/** Computes a salted PBKDF2 hash of given plaintext password
suitable for storing in a database. //  w w w.jav  a  2  s .co m
Empty passwords are not supported. */
public static String getSaltedHash(String password) throws Exception {
    byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
    //        System.out.println(salt.toString());
    //        // store the salt with the password

    System.out.println(Base64.encodeBase64String(salt) + "$" + hash(password, salt));
    return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
    //return "5Py5AAS9QlaRhtm0Ac3FNeyfuTU4oNpYKDCig4vtwS8=$1KQIRHF5zXULP/yoOr9lXHlOWyUjq/Q0A+5Caap9WA8=";
}

From source file:RandomUtil.java

/**
 * Creates a new secure random number generator. The following secure random
 * algorithm names are tried://from   ww  w  .  ja  va  2  s. c  o m
 * <ul>
 * <li>The value of system property "boticelli.securerandom", if set. </li>
 * <li> "SHA1PRNG" </li>
 * <li> "IBMSecureRandom" (available if running in the IBM JRE) </li>
 * </ul>
 */
private static SecureRandom createSecureRandom() {
    SecureRandom secureRnd = null;
    try {

        for (int i = 0; i < secureRndNames.length; i++) {
            try {
                if (secureRndNames[i] != null) {
                    secureRnd = SecureRandom.getInstance(secureRndNames[i]);
                    break;
                }
            } catch (NoSuchAlgorithmException nsae) {
                //  log.debug("no secure random algorithm named \"" + secureRndNames[i] + "\"",
                //      nsae);
            }
        }
        if (secureRnd == null) {
            throw new IllegalStateException(
                    "no secure random algorithm found. (tried " + Arrays.asList(secureRndNames) + ")");
        }
        secureRnd.setSeed(System.currentTimeMillis());
    } catch (Exception e) {
        // log.fatal("error initializing secure random", e);
    }

    return secureRnd;
}

From source file:com.healthcit.cacure.utils.PasswordService.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);/*from ww  w .j av  a  2  s  .co m*/
    kgen.init(128, sr); // 192 and 256 bits may not be available   
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:eu.freme.broker.security.tools.PasswordHasher.java

public static String getSaltedHash(String password) throws Exception {
    byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
    return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
}

From source file:org.openchain.certification.utility.PasswordUtil.java

public static String getToken(String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] salt;/*from ww w .  j av  a2 s  .  c  om*/
    salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLenght);
    return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);//from ww  w.  java 2 s . c  o  m
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:aaf.vhr.crypto.GoogleAuthenticator.java

/**
 * Generate a random secret key. This must be saved by the server and associated with the
 * users account to verify the code displayed by Google Authenticator.
 * The user must register this secret on their device.
 * @return secret key//w  ww  . j a va 2s  . c  o m
 */
public static String generateSecretKey() {
    try {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        Base32 codec = new Base32();

        byte[] buffer = new byte[10];
        sr.nextBytes(buffer);
        byte[] bEncodedKey = codec.encode(buffer);
        String encodedKey = new String(bEncodedKey);
        return encodedKey;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}

From source file:com.zimbra.cs.account.PreAuthKey.java

public static String generateRandomPreAuthKey() throws ServiceException {
    try {//  w w  w . ja va2 s. co m
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] key = new byte[KEY_SIZE_BYTES];
        random.nextBytes(key);
        return new String(Hex.encodeHex(key));
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("unable to initialize SecureRandom", e);
    }
}

From source file:org.wso2.carbon.identity.authenticator.emailotp.OneTimePassword.java

public static String getRandomNumber(int size) {

    StringBuilder generatedToken = new StringBuilder();
    try {//  w ww  . j a va 2 s.c  om
        SecureRandom number = SecureRandom.getInstance(EmailOTPAuthenticatorConstants.ALGORITHM_NAME);
        // Generate 20 integers 0..20
        for (int i = 0; i < size; i++) {
            generatedToken.append(number.nextInt(9));
        }
    } catch (NoSuchAlgorithmException e) {
        log.error("Unable to find the Algorithm", e);
    }

    return generatedToken.toString();
}

From source file:org.wso2.carbon.identity.authenticator.smsotp.OneTimePassword.java

/**
 *
 * @param size digit size of the number/*  www. j  a  va  2 s .  c om*/
 * @return token
 */
public static String getRandomNumber(int size) {

    StringBuilder generatedToken = new StringBuilder();
    try {
        SecureRandom number = SecureRandom.getInstance(SMSOTPConstants.ALGORITHM_NAME);
        // Generate 20 integers 0..20
        for (int i = 0; i < size; i++) {
            generatedToken.append(number.nextInt(9));
        }
    } catch (NoSuchAlgorithmException e) {
        log.error("Unable to find the Algorithm", e);
    }

    return generatedToken.toString();
}