whyczh
2021-09-08 8c51c3807befed873a7ab86c0e1e52dd189a8b3f
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
package com.whyc.util;
 
import com.whyc.constant.SuperConstant;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Description:摘要
 */
public class DigestsUtil {
 
    /**
     * @Description sha1方法
     * @param input 需要散列字符串
     * @param salt 盐字符串
     * @return
     */
    public static String sha1(String input, String salt) {
       return new SimpleHash(SuperConstant.HASH_ALGORITHM, input, salt,SuperConstant.HASH_INTERATIONS).toString();
    }
 
    /**
     * @Description 随机获得salt字符串
     * @return
     */
    public static String generateSalt(){
        SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        String dymSalt = Base64.encodeToString((randomNumberGenerator.nextBytes().toHex() + SuperConstant.SALT).getBytes());
        return dymSalt;
    }
 
 
    /**
     * @Description 生成密码字符密文 和 动态salt密文
     * @param
     * @return
     */
    public static Map<String,String> encryptPassword(String passwordPlain) {
       Map<String,String> map = new HashMap<>();
       String salt = generateSalt();
       String password =sha1(passwordPlain,salt);
       map.put("salt", salt);
       map.put("password", password);
       return map;
    }
 
}