Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

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

Prototype

public SecureRandom() 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:com.alibaba.citrus.service.requestcontext.session.idgen.random.impl.RandomIDGenerator.java

@Override
protected void init() {
    length = defaultIfNull(length, SESSION_ID_LENGTH_DEFAULT);

    try {//from w  w w  .jav  a 2  s.c o m
        rnd = new SecureRandom();
    } catch (Throwable e) {
        rnd = new Random();
    }
}

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

public static KeyPair genKeyPair(int bit) {
    try {/*  ww  w  .  j  a v a2  s .  co  m*/
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM, JCE_PROVIDER);
        keyPairGen.initialize(bit, new SecureRandom());
        return keyPairGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * //  w  w w .ja  v a2  s  .co  m
 * @param src ??
 * @param key 8?
 * @return     ??
 * @throws Exception
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    SecureRandom sr = new SecureRandom();
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}

From source file:com.hurence.logisland.util.string.Anonymizer.java

public Anonymizer() {
    random = new SecureRandom();
    salt = new byte[SALT_BYTE_SIZE];

    timeSalt = random.nextInt(1000000000) + 1000000000;
    random.nextBytes(salt);//from ww w . j  a v a2 s. co m
}

From source file:ece356.UserDBAO.java

private static String generateSalt() {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);//from   ww  w  .j  a va2 s .  co  m
    return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
}

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);/*from   w w w  . j  av a  2  s . c  o m*/

    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);

    return session.getString("_id");
}

From source file:com.qut.middleware.esoemanager.util.PolicyIDGenerator.java

public PolicyIDGenerator() {
    this.lock = new ReentrantLock();

    try {/*from  ww w. j  av  a  2s.c  o  m*/
        /* Attempt to get the specified RNG instance */
        this.random = SecureRandom.getInstance(this.RNG);
    } catch (NoSuchAlgorithmException nsae) {
        this.logger.error(Messages.getString("IdentifierGeneratorImpl.13")); //$NON-NLS-1$
        this.logger.debug(nsae.getLocalizedMessage(), nsae);
        this.random = new SecureRandom();
    }

    this.random.setSeed(System.currentTimeMillis());
}

From source file:es.logongas.openshift.ant.JenkinsPasswordHashPropertyTask.java

/**
 * The MIT License//from  w  w w  .j  a va2s.  com
 * 
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, David Calavera, Seiji Sogabe
 * Generates random salt.
 */
private String generateSalt() {
    StringBuilder buf = new StringBuilder();
    SecureRandom sr = new SecureRandom();
    for (int i = 0; i < 6; i++) {// log2(52^6)=34.20... so, this is about 32bit strong.
        boolean upper = sr.nextBoolean();
        char ch = (char) (sr.nextInt(26) + 'a');
        if (upper) {
            ch = Character.toUpperCase(ch);
        }
        buf.append(ch);
    }
    return buf.toString();
}

From source file:springmvc.service.PersonServiceImpl.java

public String generate() {
    SecureRandom random = new SecureRandom();
    String pw = new BigInteger(130, random).toString(32);
    return pw.substring(0, 10);

}

From source file:com.liferay.util.Encryptor.java

public static Key generateKey(String algorithm) throws EncryptorException {
    try {/*w w  w  .  j  a v a 2s.  c  o  m*/
        Security.addProvider(getProvider());

        KeyGenerator generator = KeyGenerator.getInstance(algorithm);
        generator.init(56, new SecureRandom());

        Key key = generator.generateKey();

        return key;
    } catch (Exception e) {
        throw new EncryptorException(e);
    }
}