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:Main.java

/**
 * Get the library-wide random number generator. This method is synchronized so that multiple accesses to the
 * generator when a generator is not yet set will not throw an UnsupportedOperationException
 * @return the random number generator set in {@link #setRandom(Random)} or (by default) a SecureRandom
 *///  w ww  .  j av a  2s. co m
public static synchronized Random getRandom() {
    if (randomNumberGenerator_ == null) {
        setRandom(new SecureRandom());
    }
    return randomNumberGenerator_;
}

From source file:org.beangle.security.codec.DESEncrypt.java

public byte[] doEncrypt(byte plainText[]) throws Exception {
    SecureRandom sr = new SecureRandom();
    byte rawKeyData[] = desKey;
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(1, key, sr);//from www.  j  a  v a2 s.  c  o m
    byte data[] = plainText;
    byte encryptedData[] = cipher.doFinal(data);
    return encryptedData;
}

From source file:com.apabi.r2k.common.security.util.NonceUtils.java

/**
 * SecureRandom?, Hex?./*from   w ww . j  a va  2 s.co m*/
 * 
 * @param length ,?.
 */
public static String randomHexString(final int length) {
    SecureRandom nonceGenerator = new SecureRandom();
    byte[] nonce = new byte[length / 2];
    nonceGenerator.nextBytes(nonce);
    return Hex.encodeHexString(nonce);
}

From source file:com.platform.common.utils.security.NonceUtils.java

/**
 * SecureRandom?, Hex?./*from  www. j  a v a  2 s  . co  m*/
 * 
 * @param length ,?.
 */
public static String randomHexString(int length) {
    SecureRandom nonceGenerator = new SecureRandom();
    byte[] nonce = new byte[length / 2];
    nonceGenerator.nextBytes(nonce);
    return Hex.encodeHexString(nonce);
}

From source file:com.predic8.membrane.core.interceptor.registration.SecurityUtils.java

static String createPasswdCompatibleHash(String password)
        throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] salt = new byte[128];
    new SecureRandom().nextBytes(salt);

    String saltString = Base64.encodeBase64String(salt);
    if (saltString.length() > 16)
        saltString = saltString.substring(0, 16);

    return createPasswdCompatibleHash(password, saltString);
}

From source file:Main.java

public static SSLSocketFactory setCertificates(InputStream... certificates) {
    try {/* www  .  j a v a  2 s . c  o m*/
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);
        int index = 0;
        for (InputStream certificate : certificates) {
            String certificateAlias = Integer.toString(index++);
            keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
            try {
                if (certificate != null)
                    certificate.close();
            } catch (IOException e) {
            }
        }
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
        socketFactory = sslContext.getSocketFactory();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return socketFactory;
}

From source file:filters.SaltingPasswordFilter.java

@Override
public String executePreProcessing(String request) {
    final Random r = new SecureRandom();
    byte[] salt = new byte[32];
    r.nextBytes(salt);/*from   w  w  w .j  a  va 2  s.co  m*/
    byte[] encodedSalt = Base64.encodeBase64(salt);
    String saltString = new String(encodedSalt);
    request = request.concat(saltString);
    return request;
}

From source file:Main.java

private static byte[] doFinal(String key, int opmode, byte[] input)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    key = checkNull(key) ? DEFAULT_KEY : key;
    if (checkNull(key)) {
        return null;
    }//from   www.j a v  a 2s  .  co m
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(opmode, securekey, sr);
    return cipher.doFinal(input);
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * generate salt for hash// www. j  a  v a  2s .  c o m
 *
 * @return salt
 */
public static String generateSalt() {
    byte[] salt = new byte[32];
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextBytes(salt);
    return new String(Base64.encodeBase64(salt));
}

From source file:ru.org.linux.csrf.CSRFProtectionService.java

public static void generateCSRFCookie(HttpServletRequest request, HttpServletResponse response) {
    SecureRandom random = new SecureRandom();

    byte[] value = new byte[16];
    random.nextBytes(value);//from  w  ww.j  a v  a  2  s .  c o m

    String token = new String(Base64.encodeBase64(value));

    Cookie cookie = new Cookie(CSRF_COOKIE, token);
    cookie.setMaxAge(TWO_YEARS);
    cookie.setPath("/");
    response.addCookie(cookie);

    request.setAttribute(CSRF_ATTRIBUTE, token);
}