cy
2022-06-27 150ced737ad5d16d476df143c7e4238fc6b8b998
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
package cn.ksource.core.util;
 
import java.security.MessageDigest;
import java.util.UUID;
 
/**
 * Created by chenlong
 * Date:2017/7/30
 * time:11:10
 */
public class GenerateUtil {
 
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
    public static String generate16Id() {
        String uuid =  generateUUID();
        String nextId = null;
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(uuid.getBytes());
            byte tmp[] = md.digest();
            char str[] = new char[16];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                //只取高位
                str[k++] = hexDigits[(byte0 >>> 4 & 0xf) % 10];
                // str[k++] = hexDigits[byte0 & 0xf];
            }
            nextId = new String(str);  // 换后的结果转换为字符串
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nextId;
    }
 
    public static void main(String[] args) {
        for (int i = 0; i < 200; i++) {
            System.out.println(generate16Id());
        }
    }
 
}