一个md5加密解密验证方式参考

一个md5加密解密验证方式参考

java 复制代码
package com.hmdp.utils;


import cn.hutool.core.util.RandomUtil;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

public class PasswordEncoder {

    public static String encode(String password) {
        // 生成盐
        String salt = RandomUtil.randomString(20);
        // 加密
        return encode(password,salt);
    }
    private static String encode(String password, String salt) {
        // 加密
        return salt + "@" + DigestUtils.md5DigestAsHex((password + salt).getBytes(StandardCharsets.UTF_8));
    }
    public static Boolean matches(String encodedPassword, String rawPassword) {
        if (encodedPassword == null || rawPassword == null) {
            return false;
        }
        if(!encodedPassword.contains("@")){
            throw new RuntimeException("密码格式不正确!");
        }
        String[] arr = encodedPassword.split("@");
        // 获取盐
        String salt = arr[0];
        // 比较
        return encodedPassword.equals(encode(rawPassword, salt));
    }
}
相关推荐
Sylvia-girl3 小时前
Java——抽象类
java·开发语言
Yana.nice5 小时前
Bash函数详解
开发语言·chrome·bash
Touper.6 小时前
Redis 基础详细介绍(Redis简单介绍,命令行客户端,Redis 命令,Java客户端)
java·数据库·redis
m0_535064606 小时前
C++模版编程:类模版与继承
java·jvm·c++
虾条_花吹雪7 小时前
Using Spring for Apache Pulsar:Message Production
java·ai·中间件
tomorrow.hello7 小时前
Java并发测试工具
java·开发语言·测试工具
Moso_Rx7 小时前
javaEE——synchronized关键字
java·java-ee
晓13137 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊7 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
张小洛7 小时前
Spring AOP 是如何生效的(入口源码级解析)?
java·后端·spring