码迷,mamicode.com
首页 > 其他好文 > 详细

登陆注册的-密码加盐

时间:2020-07-24 16:30:16      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:dig   dom   cep   byte   details   tar   ext   illegal   tps   

主要代码:

1         // 盐值
2         String salt = null;
3         String password = user.getPassword();
4         //加密密码
5         String encryptPassword = null; 
6    
7         salt = SaltUtil.generateSalt();
8     
9         encryPassword = PassWordUtil.encrypt(password,salt);

SaltUtil 类

 1 public class SaltUtil {
 2 
 3     /**
 4      * 生成32位随机盐
 5      *
 6      * @return
 7      */
 8     public static String generateSalt() {
 9         return new SecureRandomNumberGenerator().nextBytes(16).toHex();
10     }
11 
12 }

PassWordUtil 类

 1 public class PasswordUtil {
 2 
 3     /**
 4      * 密码加盐,再加密
 5      *
 6      * @param pwd
 7      * @param salt
 8      * @return
 9      */
10     public static String encrypt(String pwd, String salt) {
11         if (StringUtils.isBlank(pwd)) {
12             throw new IllegalArgumentException("密码不能为空");
13         }
14         if (StringUtils.isBlank(salt)) {
15             throw new IllegalArgumentException("盐值不能为空");
16         }
17         return DigestUtils.sha256Hex(pwd + salt);
18     }
19 
20 }

 

可学习的相关博客(进一步学习更深的知识)

1.SHA256加密之显示明文密码的伪处理方法

登陆注册的-密码加盐

标签:dig   dom   cep   byte   details   tar   ext   illegal   tps   

原文地址:https://www.cnblogs.com/chensisi/p/13371671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!