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.connorbrezinsky.msg.security.Hash.java

/**
 * Computes a salted PBKDF2 hash of given plaintext password suitable for
 * storing in a database. Empty passwords are not supported.
 *///from  w  w  w  .  ja v a 2 s .  c o m
public static String getSaltedHash(String password) throws Exception {
    byte[] salt = SecureRandom.getInstance("SHA1PRNG").generateSeed(saltLen);
    // store the salt with the password
    return Base64.encodeBase64String(salt) + "$" + hash(password, salt);
}

From source file:org.businessmanager.util.PasswordGenerator.java

public static Long generateSalt() {
    if (secureRandom == null) {
        try {/*from  w  w  w  . ja  va2  s . co m*/
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("Could not initialize SecureRandom because the algorithm could not be found.");
        }
    }
    return Long.valueOf(secureRandom.nextLong());
}

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static String encryptAES(byte[] input, String key) throws Exception {
    byte[] crypted = null;
    javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes());
    kgen.init(128, secureRandom);/*  ww  w. j av  a 2  s  .c om*/
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skey);
    crypted = cipher.doFinal(input);
    return new String(Hex.encode(crypted));
}

From source file:org.apache.openmeetings.util.crypt.SHA256Implementation.java

private static byte[] getSalt() throws NoSuchAlgorithmException {
    SecureRandom sr = SecureRandom.getInstance(SECURE_RND_ALG);
    byte[] salt = new byte[SALT_LENGTH];
    sr.nextBytes(salt);// w w  w.  j  a v  a2s.  c  om
    return salt;
}

From source file:fr.mael.microrss.util.SecurityUtil.java

public String random() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance(configuration.getSaltAlgorithm());
    // Salt generation 64 bits long
    byte[] bSalt = new byte[8];
    random.nextBytes(bSalt);//from ww w .j  a  va  2 s . c  o m
    return Base64.encodeBase64String(bSalt);
}

From source file:org.appcelerator.util.SecurityUtil.java

/**
 * generate a cryptographically secure random seed based on using
 * the SHA1PRNG secure random number. it uses a double seeded
 * secure generation using 2 1024-bit random generators and then
 * hashing with a SHA-256 hash./*ww w  . java  2  s . c  o  m*/
 *
 * @return seed
 * @throws Exception upon error
 */
public static String generateSecureRandomSeed() throws Exception {
    SecureRandom r2 = SecureRandom.getInstance("SHA1PRNG");
    byte buf[] = new byte[1024 / 8];
    byte buf2[] = new byte[1024 / 8];
    random.nextBytes(buf2);
    r2.nextBytes(buf);
    byte buf3[] = digest512.digest((Util.toHexString(buf) + Util.toHexString(buf2)).getBytes());
    return Util.toHexString(buf3);
}

From source file:yoyo.framework.standard.shared.SecurityUtils.java

/**
 * ???/*from w  ww .  ja  va  2  s.  c om*/
 * <ul>
 * <li>AES256bit?????</li>
 * <li>SHA1PRNG???????URL?BASE64?????</li>
 * </ul>
 * @return ?(URL?BASE64)
 */
public static String createKey() {
    try {
        final KeyGenerator generator = KeyGenerator.getInstance(ALGO_NAME);
        generator.init(KEY_LENGTH, SecureRandom.getInstance(RAND_NAME));
        return Base64.encodeBase64URLSafeString(generator.generateKey().getEncoded());
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java

/**
 * Generates a random password/*  ww w  . j a v a 2s .  co m*/
 *
 * @return the password
 */
public static String generatePassword() {
    SecureRandom random;

    try {
        random = SecureRandom.getInstance("SHA1PRNG");
        String pwd = Base64.encodeBase64String(random.generateSeed(5));

        return pwd.substring(0, pwd.length() - 2);
    } catch (NoSuchAlgorithmException e) {
        return "changeit";
    }
}

From source file:org.wso2.carbon.identity.sso.saml.ui.session.mgt.FESessionManager.java

/**
 * Initialize the FESessionManager class. Create the instances of SecureRandom and
 * MessageDigest and keep them as static references.
 *//*from ww w.j  a va2  s .  c  om*/
private static void initialize() {
    try {
        secureRandomInstance = SecureRandom.getInstance("SHA1PRNG");
        messageDigest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        log.error("Error when initializing the SAML2 SSO FESessionManager.", e);
        throw new RuntimeException(e);
    }
}

From source file:io.seldon.memcache.SecurityHashPeer.java

public static synchronized void initialise() {
    if (prng == null) {
        try {//  w  w w .j a va2  s . c o  m
            prng = SecureRandom.getInstance("SHA1PRNG");
            //get its digest
            sha = MessageDigest.getInstance("SHA-1");
            logger.info("Initialised random generator and digest");
        } catch (NoSuchAlgorithmException ex) {
            logger.error("Can't get crypotographic algorithm", ex);
        }
    }
}