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(); } }
最近下载更多
qqttuu LV3
3月4日
Hopu2024 LV2
2月22日
xk16675552250 LV33
1月5日
小安同学 LV7
2023年12月30日
阿桐吖 LV2
2023年12月29日
牛Bgtp LV1
2023年12月22日
lllxxxwofhoa LV2
2023年12月10日
2010160433 LV3
2023年12月7日
lvhongquan LV11
2023年9月19日
yqyqyqyqy LV4
2023年7月23日
最近浏览更多
ggjkvyj
3月16日
暂无贡献等级
微信网友_6909455156350976 LV2
3月16日
棒棒糖123
3月14日
暂无贡献等级
miku007
3月13日
暂无贡献等级
谢谢谢谢谢谢你 LV6
2月28日
Hopu2024 LV2
2月22日
FF加菲猫 LV4
2月15日
khhuyvhgcd LV1
2月11日
ljhgff LV1
2月10日
Gin19960217 LV3
1月22日