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.hobba.hobaserver.services.security.TokenUtil.java

public String getToken(String kid, String expiration_time) {
    HobaKeysFacadeREST hkfrest = new HobaKeysFacadeREST();
    HobaKeys hk = hkfrest.findHKIDbyKID(kid);
    HobaDevices hd = hk.getIdDevices();//  w  ww. ja  va2  s .c  o  m
    HobaUser hu = hd.getIduser();

    SecureRandom random = new SecureRandom();
    String rand = new BigInteger(256, random).toString(32);
    HobaToken ht = new HobaToken();
    ht.setToken(rand);
    long time = 0;
    try {
        time = Long.parseLong(expiration_time);
        if (time > 0) {
            Date date = new Date(new Date().getTime() + (time * 1000));
            ht.setExpiration(date);
        } else {
            ht.setExpiration(null);
        }
    } catch (Exception e) {
        ht.setExpiration(null);
    }
    ht.setIsValid(Boolean.TRUE);
    ht.setIdUser(hu);

    HobaTokenFacadeREST htfrest = new HobaTokenFacadeREST();
    ht = htfrest.create(ht);
    String token = kid + ":" + rand;
    byte[] encodedBytes = Base64.encodeBase64(token.getBytes());
    token = new String(encodedBytes);
    return token;
}

From source file:com.blackcrowsys.sinscrypto.AesKeyGenerator.java

@Override
public String generateSalt() {
    SecureRandom random = new SecureRandom();
    byte[] bytes = new byte[SALTSIZE];
    random.nextBytes(bytes);// w  ww .ja va 2s  .  c  o  m
    return Hex.encodeHexString(bytes);
}

From source file:egovframework.oe1.utl.fcc.service.EgovNumberUtil.java

public static int getRandomNum(int startNum, int endNum) {

    int randomNum = 0;

    try {/*  ww  w.  j  av  a2s  .  c om*/

        // ? ? ?
        SecureRandom rnd = new SecureRandom();

        do {

            // ?? ? ? ?.
            randomNum = rnd.nextInt(endNum + 1);

        } while (randomNum < startNum); // ? ?
                                        // ?
                                        // ? 
                                        // ??
                                        // ?.

    } catch (Exception e) {

        //log.debug(e.getMessage());
        log.trace(e.getMessage());

    }

    return randomNum;

}

From source file:org.shareok.data.redis.RedisUtil.java

/**
 *This works by choosing 130 bits from a cryptographically secure random bit generator, and encoding them in base-32. 128 bits is considered to be cryptographically strong, 
 * but each digit in a base 32 number can encode 5 bits, so 128 is rounded up to the next multiple of 5. 
 * This encoding is compact and efficient, with 5 random bits per character. Compare this to a random UUID, 
 * which only has 3.4 bits per character in standard layout, and only 122 random bits in total.
 * //from  ww  w.  j  ava2 s .c  om
 * If you allow session identifiers to be easily guessable (too short, flawed random number generator, etc.), 
 * attackers can hijack other's sessions. Note that SecureRandom objects are expensive to initialize, so you'll want to keep one around and reuse it.
 * 
 * @return a secure, random string
 */
public static String getRandomString() {
    SecureRandom random = new SecureRandom();
    return new BigInteger(130, random).toString(32);
}

From source file:net.sf.gazpachoquest.util.impl.RandomTokenGeneratorImpl.java

@Override
public String generate(int length) {
    StringBuilder token = new StringBuilder();
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < length; i++) {
        int pos = random.nextInt(ALPHABET_LENGTH);
        token.append(ALPHABET[pos]);/* w w  w. j  av a2  s  .c o m*/
    }
    return token.toString();
}

From source file:Crypto.ChiffreDES.java

public Cle generateKey(int longueur) {
    try {//from  w  ww .j  a v a2  s  .c o m
        //GENERATION DE CLE
        cleGen = KeyGenerator.getInstance("DES", "BC");
        cleGen.init(new SecureRandom());
        SecretKey Cle = cleGen.generateKey();
        return new CleDES(Cle, longueur);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchProviderException ex) {
        Logger.getLogger(ChiffreDES.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:egovframework.rte.tex.com.service.EgovNumberUtil.java

public static int getRandomNum(int startNum, int endNum) {

    int randomNum = 0;

    try {//from  w w w  . ja  v a2 s  . c  o m

        // ? ? ?
        SecureRandom rnd = new SecureRandom();

        do {

            // ?? ? ? ?.
            randomNum = rnd.nextInt(endNum + 1);

        } while (randomNum < startNum); // ? ?
        // ?
        // ? 
        // ??
        // ?.

    } catch (Exception e) {

        //log.debug(e.getMessage());
        log.trace(e.getMessage());

    }

    return randomNum;

}

From source file:net.seleucus.wsp.crypto.FwknopSymmetricCryptoTest.java

@Before
public void setUp() {
    sr = new SecureRandom();
}

From source file:com.xk72.cocoafob.LicenseGenerator.java

protected LicenseGenerator() {
    random = new SecureRandom();
}

From source file:org.datacleaner.util.SecurityUtils.java

/**
 * Removes the certificate checks of HTTPS traffic on a HTTP client. Use
 * with caution!/*w  ww  .  java2 s.c o  m*/
 * 
 * @param httpClient
 * @throws IllegalStateException
 */
public static void removeSshCertificateChecks(HttpClient httpClient) throws IllegalStateException {
    try {
        // prepare a SSL context which doesn't validate certificates
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        final TrustManager trustManager = new NaiveTrustManager();
        sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
        final SSLSocketFactory schemeSocketFactory = new SSLSocketFactory(sslContext);
        final Scheme sslScheme = new Scheme("https", 443, schemeSocketFactory);

        // try again with a new registry
        final SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
        registry.register(sslScheme);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}