2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
package com.landtool.lanbase.common.shiro;
 
import java.security.MessageDigest;
import java.util.UUID;
 
import com.landtool.lanbase.common.exception.LanbaseException;
 
/**
 * @author lanbase
 * @Description: TODO(生成token)
 * @date 2017-6-23 15:07
 */
public class TokenGenerator {
 
    public static String generateValue() {
        return generateValue(UUID.randomUUID().toString());
    }
 
    private static final char[] hexCode = "0123456789abcdef".toCharArray();
 
    public static String toHexString(byte[] data) {
        if(data == null) {
            return null;
        }
        StringBuilder r = new StringBuilder(data.length*2);
        for ( byte b : data) {
            r.append(hexCode[(b >> 4) & 0xF]);
            r.append(hexCode[(b & 0xF)]);
        }
        return r.toString();
    }
 
    public static String generateValue(String param) {
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(param.getBytes());
            byte[] messageDigest = algorithm.digest();
            return toHexString(messageDigest);
        } catch (Exception e) {
            throw new LanbaseException("生成Token失败", e);
        }
    }
}