List of usage examples for java.security SecureRandom getInstance
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:org.wso2.carbon.identity.core.util.IdentityUtil.java
/** * Generates a secure random hexadecimal string using SHA1 PRNG and digest * * @return Random hexadecimal encoded String * @throws Exception//from w w w . ja va 2s . c o m */ public static String generateUUID() throws Exception { try { // SHA1 Pseudo Random Number Generator SecureRandom prng = SecureRandom.getInstance("SHA1PRNG"); // random number String randomNum = Integer.toString(prng.nextInt()); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] digest = sha.digest(randomNum.getBytes()); // Hexadecimal encoding return new String(Hex.encodeHex(digest)); } catch (NoSuchAlgorithmException e) { throw new Exception("Failed to generate UUID ", e); } }
From source file:org.everit.osgi.password.encryptor.pbkdf2.internal.PBKDF2PasswordEncryptorComponent.java
private byte[] generateSalt() throws NoSuchAlgorithmException { // VERY important to use SecureRandom instead of just Random SecureRandom random;//from ww w. j a v a2s. c o m random = SecureRandom.getInstance(SALT_ALGORITHM); // Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5 byte[] salt = new byte[8]; random.nextBytes(salt); return salt; }
From source file:org.n2.app.beans.RegisterBean.java
/** * Creates a fixed length small fingerprint (digest / hash) * See: https://www.owasp.org/index.php/Hashing_Java *//*from w w w .j a v a2 s . c o m*/ private void securePassword() { try { // Uses a secure Random not a simple Random SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); // Salt generation 64 bits long byte[] salt = new byte[8]; random.nextBytes(salt); // Digest computation byte[] digest = getHash(ITERATION_NUMBER, password, salt); setPassword(byteToBase64(digest)); setSalt(byteToBase64(salt)); } catch (Exception e) { LOG.error("Error while creating password hash", e); throw new RuntimeException("Error while creating user."); } }
From source file:com.cloud.vm.VMInstanceVO.java
public VMInstanceVO(long id, long serviceOfferingId, String name, String instanceName, Type type, Long vmTemplateId, HypervisorType hypervisorType, long guestOSId, long domainId, long accountId, long userId, boolean haEnabled) { this.id = id; hostName = name != null ? name : uuid; if (vmTemplateId != null) { templateId = vmTemplateId;// www.j a v a 2 s . c o m } this.instanceName = instanceName; this.type = type; this.guestOSId = guestOSId; this.haEnabled = haEnabled; state = State.Stopped; this.accountId = accountId; this.domainId = domainId; this.serviceOfferingId = serviceOfferingId; this.hypervisorType = hypervisorType; this.userId = userId; limitCpuUse = false; try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] randomBytes = new byte[16]; random.nextBytes(randomBytes); vncPassword = Base64.encodeBase64URLSafeString(randomBytes); } catch (NoSuchAlgorithmException e) { s_logger.error("Unexpected exception in SecureRandom Algorithm selection ", e); } }
From source file:com.adito.server.jetty.CustomJsseListener.java
protected SSLServerSocketFactory createFactory() throws Exception { if (KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).isKeyStoreEmpty()) { throw new Exception( "The keystore does not contain any certificates. Please run the installation wizard (--install)."); }/* w w w . j av a 2s .c om*/ KeyStore ks = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getKeyStore(); String pw = ContextHolder.getContext().getConfig() .retrieveProperty(new ContextKey("webServer.keystore.sslCertificate.password")); KeyManager[] kma = new KeyManager[] { new CustomKeyManager(pw) }; TrustManager[] tma = null; if (trustManager == null) { TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tm.init(ks); tma = tm.getTrustManagers(); } else { // LDP - Add the existing trust managers so that outgoing certificates are still trusted. TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tm.init(ks); tma = new TrustManager[tm.getTrustManagers().length + 1]; for (int i = 0; i < tm.getTrustManagers().length; i++) { tma[i] = tm.getTrustManagers()[i]; } tma[tma.length - 1] = trustManager; } SSLContext sslc = SSLContext.getInstance("SSL"); sslc.init(kma, tma, SecureRandom.getInstance("SHA1PRNG")); SSLServerSocketFactory ssfc = sslc.getServerSocketFactory(); if (log.isInfoEnabled()) log.info("SSLServerSocketFactory=" + ssfc); initialised = true; return ssfc; }
From source file:com.sslexplorer.server.jetty.CustomJsseListener.java
protected SSLServerSocketFactory createFactory() throws Exception { if (KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).isKeyStoreEmpty()) { throw new Exception( "The keystore does not contain any certificates. Please run the installation wizard (--install)."); }/*from w w w . j a v a 2 s . c om*/ KeyStore ks = KeyStoreManager.getInstance(KeyStoreManager.DEFAULT_KEY_STORE).getKeyStore(); String pw = ContextHolder.getContext().getConfig() .retrieveProperty(new ContextKey("webServer.keystore.sslCertificate.password")); KeyManager[] kma = new KeyManager[] { new CustomKeyManager(pw) }; TrustManager[] tma = null; if (trustManager == null) { TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tm.init(ks); tma = tm.getTrustManagers(); } else { // LDP - Add the existing trust managers so that outgoing certificates are still trusted. TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tm.init(ks); tma = new TrustManager[tm.getTrustManagers().length + 1]; for (int i = 0; i < tm.getTrustManagers().length - 1; i++) { tma[i] = tm.getTrustManagers()[i]; } tma[tma.length - 1] = trustManager; } SSLContext sslc = SSLContext.getInstance("SSL"); sslc.init(kma, tma, SecureRandom.getInstance("SHA1PRNG")); SSLServerSocketFactory ssfc = sslc.getServerSocketFactory(); if (log.isInfoEnabled()) log.info("SSLServerSocketFactory=" + ssfc); initialised = true; return ssfc; }
From source file:org.sakaiproject.tool.impl.SessionComponent.java
/** * Final initialization, once all dependencies are set. */// w ww . j av a 2 s . c o m public void init() { // start the maintenance thread if (m_checkEvery > 0) { m_maintenance = new Maintenance(); m_maintenance.start(); } // Salt generation 64 bits long salt = new byte[8]; SecureRandom random; try { random = SecureRandom.getInstance("SHA1PRNG"); random.nextBytes(salt); } catch (NoSuchAlgorithmException e) { M_log.warn("Random number generator not available - using time randomness"); salt = String.valueOf(System.currentTimeMillis()).getBytes(); } M_log.info("init(): interval: " + m_defaultInactiveInterval + " refresh: " + m_checkEvery); }
From source file:org.wso2.carbon.identity.password.history.store.Impl.DefaultPasswordHistoryDataStore.java
/** * This private method returns a saltValue using SecureRandom. * * @return saltValue//from ww w. ja v a 2 s . com */ private String generateSaltValue() { String saltValue; try { SecureRandom secureRandom = SecureRandom.getInstance(SHA_1_PRNG); byte[] bytes = new byte[16]; //secureRandom is automatically seeded by calling nextBytes secureRandom.nextBytes(bytes); saltValue = Base64.encode(bytes); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("SHA1PRNG algorithm could not be found."); } return saltValue; }
From source file:com.aimluck.eip.util.ALCommonUtils.java
/** * ID??SecureRandom????//from w w w . j a v a 2 s . c om * * @return random ID??SecureRandom */ public static SecureRandom getSecureRandom() { SecureRandom random = null; try { random = SecureRandom.getInstance(DEF_RANDOM_ALGORITHM); byte seed[] = random.generateSeed(DEF_RANDOM_LENGTH); random.setSeed(seed); } catch (Exception e) { logger.error("ALCommonUtils.getSecureRandom", e); return null; } return random; }