Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import org.apache.shiro.crypto.hash.Sha256Hash;
  2. import org.apache.shiro.crypto.RandomNumberGenerator;
  3. import org.apache.shiro.crypto.SecureRandomNumberGenerator;
  4. ...
  5.  
  6. //We'll use a Random Number Generator to generate salts. This
  7. //is much more secure than using a username as a salt or not
  8. //having a salt at all. Shiro makes this easy.
  9. //
  10. //Note that a normal app would reference an attribute rather
  11. //than create a new RNG every time:
  12. RandomNumberGenerator rng = new SecureRandomNumberGenerator();
  13. Object salt = rng.nextBytes();
  14.  
  15. //Now hash the plain-text password with the random salt and multiple
  16. //iterations and then Base64-encode the value (requires less space than Hex):
  17. String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt, 1024).toBase64();
  18.  
  19. User user = new User(username, hashedPasswordBase64);
  20. //save the salt with the new account. The HashedCredentialsMatcher
  21. //will need it later when handling login attempts:
  22. user.setPasswordSalt(salt);
  23. userDAO.create(user);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement