郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
48
49
50
51
52
53
54
55
56
57
package com.gkhy.safePlatform.account.utils;
 
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
 
import java.util.Random;
 
/**
* @Description: 密码 盐 生成
* @date 2022/6/21 18:51
*/
public class PasswordUtil {
 
 
    /**
    * @Description: 编码加密
    */
 
    public static String SHA256Encode(String content) {
        return Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString();
    }
 
 
    /**
    * @Description:  密码判断  参数密码, 盐 和数据库密码
    */
    public static boolean match(String paramPassword, String salt, String dbPassword) {
        String makePassword = makePassword(paramPassword, salt);
        return dbPassword.equals(makePassword);
    }
 
    /**
    * @Description: 密码生成
    */
 
    public static String makePassword(String paramPassword, String salt) {
        String firstResult = SHA256Encode(paramPassword);
        return SHA256Encode(firstResult + salt);
    }
 
 
    /**
    * @Description: 盐生成
    */
    public static String makeSalt() {
        Random random = new Random();
        long l = random.nextInt(10) + System.nanoTime() + random.nextInt(10);
        return SHA256Encode(l + "");
    }
 
    public static void main(String[] args) {
        String r1 = SHA256Encode("r");
        String r = makePassword("123", r1);
        System.out.println(r1);
        System.out.println(r);
    }
}