首页>代码>基于springboot+layui实现的医院信息管理系统>/src/main/java/com/aaa/config/UserCredentialsMatcher.java
package com.aaa.config;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

public class UserCredentialsMatcher {
    /**
     * 随机生成 salt 需要指定 它的字符串的长度
     *
     * @param len 字符串的长度
     * @return salt
     */
    public static String generateSalt(int len) {
        //一个Byte占两个字节
        int byteLen = len >> 1;
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        return secureRandom.nextBytes(byteLen).toHex();
    }

    /**
     * 获取加密后的密码,使用默认hash迭代的次数 1 次
     *
     * @param hashAlgorithm hash算法名称 MD2、MD5、SHA-1、SHA-256、SHA-384、SHA-512、etc。
     * @param password      需要加密的密码
     * @param salt          盐
     * @return 加密后的密码
     */
    public static String encryptPassword(String hashAlgorithm, String password, String salt) {
        return encryptPassword("MD5", password, salt, 2);
    }

    /**
     * 获取加密后的密码,需要指定 hash迭代的次数
     *
     * @param hashAlgorithm  hash算法名称 MD2、MD5、SHA-1、SHA-256、SHA-384、SHA-512、etc。
     * @param password       需要加密的密码
     * @param salt           盐
     * @param hashIterations hash迭代的次数
     * @return 加密后的密码
     */
    public static String encryptPassword(String hashAlgorithm, String password, String salt, int hashIterations) {
        SimpleHash hash = new SimpleHash(hashAlgorithm, password, salt, hashIterations);
        return hash.toString();
    }
}
最近下载更多
zhaopb  LV8 5月17日
zhaorrr  LV2 2024年11月7日
yyhrhv  LV8 2024年8月31日
叁卍石  LV5 2024年8月21日
TY0165  LV20 2024年6月21日
pxc1994  LV9 2024年5月14日
微信网友_6927932988952576  LV12 2024年3月31日
提灯寻白鹿  LV3 2024年3月27日
qqttuu  LV3 2024年3月4日
Hopu2024  LV2 2024年2月22日
最近浏览更多
2225906816 5月26日
暂无贡献等级
zxdfdcfffddssd 5月21日
暂无贡献等级
123456789x  LV1 5月19日
zhaopb  LV8 5月17日
TaoTaot 4月7日
暂无贡献等级
小宋敲代码 3月25日
暂无贡献等级
gxyhk666 3月25日
暂无贡献等级
blanknb 3月18日
暂无贡献等级
尼可曲奇 3月3日
暂无贡献等级
yvimib  LV3 3月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友