Example usage for java.security SecureRandom nextBytes

List of usage examples for java.security SecureRandom nextBytes

Introduction

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

Prototype

@Override
public void nextBytes(byte[] bytes) 

Source Link

Document

Generates a user-specified number of random bytes.

Usage

From source file:com.andiandy.m101j.week3.hw2_3.SessionDAO.java

public String generateSessionID() {
    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);

    byte[] idBytes = Base64.encodeBase64(randomBytes);
    String sessionID = new String(idBytes);

    return sessionID;
}

From source file:com.andiandy.m101j.week4.course.SessionDAO.java

public String getSessionID() {
    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);

    byte[] idBytes = Base64.encodeBase64(randomBytes);
    String sessionID = new String(idBytes);

    return sessionID;
}

From source file:org.openbel.framework.core.df.encryption.AbstractEncryptionService.java

/**
 * Generate a initialization vector of the specified length.
 * @param length   number of bytes to generate
 * @return iv/*from   w  ww  .  j av  a  2  s . c  om*/
 */
protected String generateInitializationVector(int length) {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[length];
    random.nextBytes(bytes);
    return encodeBase64(bytes);
}

From source file:course.SessionDAO.java

public String startSession(String username) {

    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);

    // result = new Base64().encodeToString(rawHmac);

    // BASE64Encoder encoder = new BASE64Encoder();

    // String sessionID = encoder.encode(randomBytes);

    String sessionID = new Base64().encodeToString(randomBytes);

    // build the BSON object
    BasicDBObject session = new BasicDBObject("username", username);

    session.append("_id", sessionID);

    sessionsCollection.insert(session);//from   ww  w .  j  a  v  a 2s. c  o  m

    return session.getString("_id");
}

From source file:com.kspichale.kundera.course.SessionDAO.java

public String startSession(String username) {

    // get 32 byte random number. that's a lot of bits.
    SecureRandom generator = new SecureRandom();
    byte randomBytes[] = new byte[32];
    generator.nextBytes(randomBytes);

    BASE64Encoder encoder = new BASE64Encoder();

    String sessionID = encoder.encode(randomBytes);

    // build the BSON object
    BasicDBObject session = new BasicDBObject("username", username);

    session.append("_id", sessionID);

    sessionsCollection.insert(session);//from w  ww .  j  a  v a 2 s  .c o m

    return session.getString("_id");
}

From source file:com.amazon.alexa.avs.auth.companionapp.CodeChallengeWorkflow.java

/**
 * As per Proof Key/SPOP protocol Version 10
 * @return a random 32 sized octet sequence from allowed range
 *///from  w  w  w .j a  v  a 2 s . c  o m
private byte[] generateRandomOctetSequence() {
    SecureRandom random = new SecureRandom();
    byte[] octetSequence = new byte[32];
    random.nextBytes(octetSequence);

    return octetSequence;
}

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);
    return Base64.encodeBase64String(bSalt);
}

From source file:org.unitedid.auth.client.factors.PasswordFactor.java

public String getRandomNonce() throws NoSuchAlgorithmException {
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    byte[] randomBytes = new byte[6];
    rand.nextBytes(randomBytes);

    String result = "";
    for (int i = 0; i < randomBytes.length; i++) {
        result += Integer.toString((randomBytes[i] & 0xff) + 0x100, 16).substring(1);
    }//from w  ww.j  a  va2  s  .  co  m

    return result;
}

From source file:org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogWriter.java

@Override
protected WALHeader buildWALHeader(WALHeader.Builder builder) throws IOException {
    if (conf.getBoolean(HConstants.ENABLE_WAL_ENCRYPTION, false)) {
        // Get an instance of our cipher
        Cipher cipher = Encryption.getCipher(conf,
                conf.get(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, DEFAULT_CIPHER));
        if (cipher == null) {
            throw new RuntimeException("Cipher '" + cipher + "' is not available");
        }//from   w  w  w. j a v  a2 s  .c o  m

        // Generate an encryption key for this WAL
        SecureRandom rng = new SecureRandom();
        byte[] keyBytes = new byte[cipher.getKeyLength()];
        rng.nextBytes(keyBytes);
        Key key = new SecretKeySpec(keyBytes, cipher.getName());
        builder.setEncryptionKey(HBaseZeroCopyByteString.wrap(EncryptionUtil.wrapKey(conf,
                conf.get(HConstants.CRYPTO_WAL_KEY_NAME_CONF_KEY,
                        conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, User.getCurrent().getShortName())),
                key)));

        // Set up the encryptor
        encryptor = cipher.getEncryptor();
        encryptor.setKey(key);

        if (LOG.isTraceEnabled()) {
            LOG.trace("Initialized secure protobuf WAL: cipher=" + cipher.getName());
        }
    }

    return super.buildWALHeader(builder);
}

From source file:fr.amapj.service.engine.sudo.SudoManager.java

private String generateSudo() {
    try {/*from   w w w  .  jav a2s.c  o  m*/
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

        byte[] salt = new byte[16];
        random.nextBytes(salt);

        Base64 coder = new Base64(true);
        String str = coder.encodeAsString(salt);
        str = str.replace('\r', '0');
        str = str.replace('\n', '0');
        return str;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Erreur inattendue", e);
    }
}