标签:加密 spring security encrypt passwordencoder
增强版对加密串增加到80位,没错产生的哈希值都不相同package com.rapido.utils; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.StandardPasswordEncoder; /** * Spring 加密工具 Security3.1之后增强简化版,采用SHA-256算法,迭代1024次,使用一个密钥(site-wide secret)以及8位随机盐对原密码进行加密 * 随机盐确保相同的密码使用多次时,产生的哈希都不同; * 密钥应该与密码区别开来存放,加密时使用一个密钥即可,使暴力破解变得更困难些 * 参考文章:http://www.iteye.com/topic/1120844 * * @author X-rapido */ public class EncryptUtil { private static final String SITE_WIDE_SECRET = "rapido"; // 原始密码配置 private static final PasswordEncoder encoder = new StandardPasswordEncoder(SITE_WIDE_SECRET); /** * 加密密码 * * @param rawPassword 原始密码 * @return */ public static String encrypt(String rawPassword) { return encoder.encode(rawPassword); } /** * 密码比较,返回80位加密密码 * * @param rawPassword 原始密码 * @param 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("密码rapido匹配成功:" + rawPwd + ""); } else { System.out.println("密码匹配失败"); } System.out.println(EncryptUtil.encrypt("rapido")); System.out.println(EncryptUtil.encrypt("rapido")); System.out.println(EncryptUtil.encrypt("rapido")); } }输出
密码rapido匹配成功:df10352d9f07d49679bd71fc2e7122052572471ffe35f295218f1c41c51aa03eb8071ef339f7a536 a190a5bf4f2b34e7b29344108babb287caf2ddb0e4cb54ade855944707cba3616ead361e3a80b6a7 4f6ebd5625ba325e8670ed43a14c2f431b3de542912859126701dbfa0deaab9cea8b19e285e613fa 9a32c7ec25086b99cc018e6a1b9cb937d952019b73d43097c68b6ab9f0eee30351a0a53548b9d758
Spring 加密工具 Security3.1之后增强简化版
标签:加密 spring security encrypt passwordencoder
原文地址:http://blog.csdn.net/xiaokui_wingfly/article/details/45888249