List of usage examples for java.security SecureRandom nextBytes
@Override public void nextBytes(byte[] bytes)
From source file:org.sonar.server.usertoken.TokenGeneratorImpl.java
@Override public String generate() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[20]; random.nextBytes(bytes); return Hex.encodeHexString(bytes); }
From source file:se.sawano.java.security.otp.DefaultRandomSupplier.java
private SecureRandom createRandom() { counter = 0;/* ww w . j a va 2 s .c om*/ final SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(new byte[1]); // Ensure seeding return secureRandom; }
From source file:gov.nih.nci.cagrid.opensaml.provider.SecureRandomIDProvider.java
/** * @see gov.nih.nci.cagrid.opensaml.SAMLIdentifier#generateRandomBytes(java.security.SecureRandom, int) *///from ww w.j a va 2s . c o m public byte[] generateRandomBytes(SecureRandom random, int n) { byte[] bytes = new byte[n]; random.nextBytes(bytes); return bytes; }
From source file:com.erudika.para.utils.Utils.java
/** * Generates an authentication token - a random string encoded in Base64. * @param length the length of the generated token * @param urlSafe switches to a URL safe encoding * @return a random string//from w w w . j a va 2s .c o m */ public static String generateSecurityToken(int length, boolean urlSafe) { final byte[] bytes = new byte[length]; SecureRandom rand; try { rand = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException ex) { logger.error(null, ex); rand = new SecureRandom(); } rand.nextBytes(bytes); return urlSafe ? base64encURL(bytes) : base64enc(bytes); }
From source file:adminpassword.Encryption.java
public String encrypt(String word, String idKey) throws Exception { byte[] ivBytes; String password = idKey; //you can give whatever you want. This is for testing purpose SecureRandom random = new SecureRandom(); byte bytes[] = new byte[20]; random.nextBytes(bytes); byte[] saltBytes = bytes; // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); //encrypting the word Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8")); //prepend salt and vi byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);//from w w w . j a v a 2 s . c o m return new Base64().encodeToString(buffer); }
From source file:com.blackcrowsys.sinscrypto.AesKeyGenerator.java
@Override public String generateIV() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[IVSIZE]; random.nextBytes(bytes); return Hex.encodeHexString(bytes); }
From source file:com.blackcrowsys.sinscrypto.AesKeyGenerator.java
@Override public String generateSalt() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[SALTSIZE]; random.nextBytes(bytes); return Hex.encodeHexString(bytes); }
From source file:nl.waisda.services.UserService.java
public ResetPassword requestPasswordReset(User user) { SecureRandom random = new SecureRandom(); byte bytes[] = new byte[64]; random.nextBytes(bytes); String plainTextKey = Base64.encodeBase64String(bytes); plainTextKey = plainTextKey.replaceAll("[^a-zA-Z0-9]", ""); ResetPassword reset = new ResetPassword(user, plainTextKey); rpwRepo.store(reset);// www . j a v a 2s . c om return reset; }
From source file:adminpassword.AESDemo.java
public String generateSalt() { SecureRandom random = new SecureRandom(); byte bytes[] = new byte[20]; random.nextBytes(bytes); String s = new String(bytes); return s;/* w w w . ja v a 2 s . c o m*/ }
From source file:com.andiandy.m101j.week2.hw3.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); String sessionID = Base64.encodeBase64(randomBytes).toString(); // build the BSON object BasicDBObject session = new BasicDBObject("username", username); session.append("_id", sessionID); sessionsCollection.insert(session);// ww w. j ava 2s . co m return session.getString("_id"); }