Powerful cryptographic tools in Spring Security 3.1 passwordencoder

Source: Internet
Author: User

OK, this encryption mechanism is very complex, or to see better understanding:

3.1. The new Passwordencoder inheritance relationship in version 0

    

After the Spring-security 3.1.0 release, the password package in the Spring-security-crypto module provides support for a more cryptographically encrypted password, which also has a Passwordencoder interface, which is defined as follows.

Java code
    1. public interface passwordencoder{
    2. string Encode (string rawpassword);
    3. Boolean Matches (String rawpassword,string Encodedpassword);
    4. }  

Two methods are defined, and the Encode method encrypts the method, and the match method is used to verify that the password and password are consistent and returns true if it is consistent. Compared to the Passwordencoder interface in the Authentication.encoding package, many are simplified.

Located in the Org.springframeword.security.crypto.password package

The Standardpasswordencoder class, which is the (unique) implementation class of the Passwordencoder interface, is the core of the encryption method described in this article. It uses the SHA-256 algorithm, iterates 1024 times, encrypts the original password using a key (Site-wide secret) and 8-bit random salts.

Random salts ensure that the same password is used multiple times, the resulting hash is different, the key should be separated from the password to store, encryption with a key can be, the hash algorithm 1024 times the implementation of enhanced security, so that the brute force is more difficult.

compared with the previous version of Passwordencoder, the benefits are obvious: The salt value is not provided by the user, randomly generated each time, multiple encryption ———— iterative SHA algorithm + key + random salt to encrypt the password, greatly increasing the difficulty of password cracking.

OK, here's how we can test it:

Java code
  1. import org.springframework.security.crypto.password.PasswordEncoder;
  2. import org.springframework.security.crypto.password.StandardPasswordEncoder;
  3.   
  4. /** 
  5. * @author Xuyi
  6. * Spring Security 3.1 passwordencoder
  7.  */  
  8. Public class Encryptutil {
  9. //obtained from the configuration file
  10. private static final String Site_wide_secret = "My-secret-key";
  11. private static final passwordencoder encoder = new standardpasswordencoder ( /c0>
  12. Site_wide_secret);
  13.    
  14. Public static string Encrypt (string rawpassword) {
  15. return encoder.encode (Rawpassword);
  16.     }  
  17.    
  18. Public static boolean match (string Rawpassword, string password) {
  19. return encoder.matches (rawpassword, password);
  20.     }  
  21.       
  22. Public static void main (string[] args) {
  23. System.out.println (Encryptutil.encrypt ("Each time the results are not the same)");
  24. System.out.println (Encryptutil.encrypt ("Each time the results are not the same)");
  25. System.out.println (Encryptutil.encrypt ("Each time the results are not the same)");
  26. System.out.println (Encryptutil.encrypt ("Each time the results are not the same)");
  27. System.out.println (Encryptutil.encrypt ("Each time the results are not the same)");
  28. //But take each result out to match and you'll find that you can get true.   
  29.     }  
  30.  }  
 

Powerful cryptographic tools in Spring Security 3.1 passwordencoder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.