当前位置:网站首页>[MD5] quickly realize MD5 encryption and decryption (salt value)
[MD5] quickly realize MD5 encryption and decryption (salt value)
2022-07-19 01:06:00 【No bug is the biggest bug】
scene : Simulate user account registration, password encryption and login verification
Here is a customized user class that owns username and password Field , As a reference
1. User password encryption storage
// Simulate the user name and password at the time of user registration
String username = "username";
String password = "123456";
// obtain User object
User user = new User();
// Get salt value
String salt = CodecUtils.generateSalt();
// Encrypt the password through the salt value
password = CodecUtils.md5Hex(password, salt);
// Store the encrypted password and salt value into the object
user.setSalt(salt);
user.setPassword(password);
user.setUsername(username);
// Final data warehousing 2. Perform password decryption verification when the user logs in
// Simulate the user name and password entered when the user logs in
String pw = "123456";
String username = "username";
// Get the user information through the user name
User user = UserMapper.findByUserName(username);
// Judge user Is it empty If it is blank, return null
if (user == null) {
return null;
}
// Get the user Salt value in object
String salt = user.getSalt();
// Get the user Password in object
String password= user.getPassword();
// Decrypt the password through the tool class
password = CodecUtils.md5Hex(password, salt);
if(password.equals(pw)){
System.out.println(" Login successful ");
}else{
System.out.println(" Login failed ");
}
3. The last attached MD5 Tool class
public class CodecUtils {
public static String md5Hex(String data,String salt) {
if (StringUtils.isBlank(salt)) {
salt = data.hashCode() + "";
}
return DigestUtils.md5Hex(salt + DigestUtils.md5Hex(data));
}
public static String shaHex(String data, String salt) {
if (StringUtils.isBlank(salt)) {
salt = data.hashCode() + "";
}
return DigestUtils.sha512Hex(salt + DigestUtils.sha512Hex(data));
}
public static String generateSalt(){
return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
}
}边栏推荐
- NVIDIA的jetson利用deepstream加速常见问题分析,deepstream消息发出外部接收,xvaier自启动程序,优秀的博客总结(精二)
- mysql插入数据会失败?为什么?
- 在centos安装mysql出现的问题
- Asynchronous programming & Collaboration
- 12. 整数转罗马数字 ●●
- Comment conditionalonmissingbean implémente l'écrasement des haricots dans les composants tiers
- 347. The first k high-frequency elements ●●
- 【MySQL】ERROR 1130 问题解决方案
- Hcia-r & s self use notes (8) IP address basis, subnet mask, subnet Division
- 如何使用服务器搭建我们的博客
猜你喜欢

利用 Redis 的 sorted set 做每周热评的功能

玩转CSDN编辑器

MySQL lock: comprehensive understanding

Redis命令

How to solve the over correlation of graph neural network? A new perspective of IBM!

【着色器实现Wave效果_Shader效果第一篇】

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions - extreme and maximum values of multivariate functions

【MariaDB】解决:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

Greenplum6.x客户端连接

Mysql—锁:全面理解
随机推荐
Relationship between sampling rate and signal-to-noise ratio
Double pointer summary (to be continued)
cmd中文乱码解决方案
Vite3.0 发布
Rocky基础练习题-find、sed、grep、tar
API design principles from the requirements of compiler for instruction set
Mysql—多版本並發控制(MVCC)
QT打开外部程序并嵌入Qt界面
Press and hold day 27: Service
采样率和信噪比之间的关系
347. 前 K 个高频元素 ●●
【MD5】快速实现MD5加密解密(盐值)
[untitled]
12. Integer to Roman numerals ●●
Acquisition of network security B module coding information of national vocational college skills competition
如何使用服务器搭建我们的博客
Mysql—多版本并发控制(MVCC)
war或jar使用Resource或ClassPathResource加载classpath下文件失败
Mysql - contrôle de la concurrence Multi - versions (mvcc)
Application of optimistic lock and pessimistic lock in kubernetes