EncryptUtil.java
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.zhongzhi.common.utils;
import com.amdelamar.jhash.Hash;
import com.amdelamar.jhash.algorithms.Type;
import com.amdelamar.jhash.exception.InvalidHashException;
/**
* @description: 加密
* @author DengMin
* @date 2025/5/11 16:19
* @version 1.0
*/
public class EncryptUtil {
/**
* 设置密文密码
*
* @param password 原始密码
* @return 加密密码
*/
public static String encrypt(String password) {
char[] chars = password.toCharArray();
return Hash.password(chars).algorithm(Type.PBKDF2_SHA256).create();
}
/**
* 验证加密密码
*
* @param encryptedPassword 密文密码
* @param plainPassword 明文密码
* @return 验证是否成功
*/
public static boolean verify(String encryptedPassword, String plainPassword) {
char[] chars = plainPassword.toCharArray();
try {
return Hash.password(chars).algorithm(Type.PBKDF2_SHA256).verify(encryptedPassword);
} catch (InvalidHashException e) {
return false;
}
}
public static void main(String[] args) {
System.out.println(encrypt("123456"));
}
}