Enhanced simplified version after Spring encryption tool Security3.1, springsecurity3.1
Enhanced versions increase the number of encrypted strings to 80 bits, and the hash values generated each time are different.
Package com. rapido. utils; import org. springframework. security. crypto. password. passwordEncoder; import org. springframework. security. crypto. password. standardPasswordEncoder;/*** enhanced simplified version after Spring encryption tool Security3.1, iterative 1024 times using a SHA-256 algorithm, using a key (site-wide secret) and 8-bit random salt encryption of the original password * random salt ensures that the hash generated when the same password is used multiple times is different; * The key should be stored separately from the password, you can use one key for encryption to make brute-force cracking more difficult. * Reference: http://www.iteye.com/topic/1120844 ** @ author X-rapido */public class EncryptUtil {private static final String SITE_WIDE_SECRET = "rapido "; // configure the original password private static final PasswordEncoder encoder = new StandardPasswordEncoder (SITE_WIDE_SECRET ); /*** encrypted password ** @ param rawPassword original password * @ return */public static String encrypt (String rawPassword) {return encoder. encode (rawPassword);}/*** password comparison, return the 80-bit encrypted password ** @ param rawPassword original password * @ param password encrypted password * @ return */public static boolean match (String rawPassword, String password) {return encoder. matches (rawPassword, password);} public static void main (String [] args) {String rawPwd = EncryptUtil. encrypt ("rapido"); if (match ("rapido", rawPwd) {System. out. println ("password rapido matched successfully:" + rawPwd + "");} else {System. out. println ("password matching failed");} System. out. println (EncryptUtil. encrypt ("rapido"); System. out. println (EncryptUtil. encrypt ("rapido"); System. out. println (EncryptUtil. encrypt ("rapido "));}}
Output
Password rapido matched successfully: Success