前言

工作中需要用到,不需要用uuid这种太长的,但是需要有自己特色标识的,于是晚上搜集了部分算法和思路,加工出来一个唯一随机id生成类

思路

改造过的Caesar算法,利用随机数加密,生成前12位 + 后4位随机数,经测试大量数据和实际使用验证,无重复值出现,可以放心使用

1、代码

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// key  
private static String KEYS = "0123456789qwertyuiopasdfghjklzxcvbnm";  
  
/** 
 * 唯一id生成工具类,12位字母+数字 
 *  
 * @param id 随机整数即可 
 */  
public static String getRandomId(int id)  
{  
    String ret = null;  
    String num = String.format("%09d", id);  
    Random random = new Random();  
    int key = random.nextInt(10);  
    int seed = random.nextInt(100);  
    Caesar caesar = new Caesar(KEYS, seed);  
    num = caesar.encode(key, num);  
    ret = num + String.format("%01d", key) + String.format("%02x", seed);  
  
    return ret;  
}  
  
/** 
 * id唯一值生成工具类<br> 
 * 格式:test_xxxx,总共21位随机码  
 * @return test_+16位随机码 
 */  
public static String getID()  
{  
    // 前缀  
    String prefix = "test_";  
    // 随机码  
    String main = TestRandomUUID.getRandomId(new Random().nextInt(1000000));  
    String end = RandomUtils.getRandomAlphamericStrNew(4);  
      
    return prefix + main + end;  
}  

public class RandomUtils  
{  
     /** 
     * 获得特定长度的一串随机数字 
     *  
     * @param length  
     * @return 
     */  
    public static String getRandomNumberStr(int length)  
    {  
        StringBuilder builder = new StringBuilder();  
        Random random = new Random();  
        for (int i = 0; i < length; i++)  
        {  
            builder.append(random.nextInt(10));  
        }  
        return builder.toString();  
    }  
  
    /** 
     * 获得特定长度的一个随机字母数字混编字符串(所包含的字符包括0-9A-Z) 
     *  
     * @param length 
     * @return 
     */  
    public static String getRandomAlphamericStr(int length)  
    {  
        StringBuilder builder = new StringBuilder();  
        Random random = new Random();  
        for (int i = 0; i < length; i++)  
        {  
            int n = random.nextInt(36);  
            if (n < 10)  
            {  
                builder.append(n);  
            }  
            else  
            {  
                builder.append((char) (n + 55));  
            }  
        }  
        return builder.toString();  
    }  
  
    /** 
     * 获得特定长度的一个随机字母数字混编字符串(所包含的字符包括0-9a-z) 
     *  
     * @param length 
     * @return 
     */  
    public static String getRandomAlphamericStrNew(int length)  
    {  
        StringBuilder builder = new StringBuilder();  
        Random random = new Random();  
        for (int i = 0; i < length; i++)  
        {  
            int n = random.nextInt(36);  
            if (n < 10)  
            {  
                builder.append(n);  
            }  
            else  
            {  
                builder.append((char) (n + 87));  
            }  
        }  
        return builder.toString();  
    }  
  
    /** 
     * 获得特定长度的一个随机十六进制数字字母混编字符串(所包含的字符包括0-9A-F) 
     *  
     * @param length 
     * @return 
     */  
    public static String getRandomHexStr(int length)  
    {  
        StringBuilder builder = new StringBuilder();  
        Random random = new Random();  
        for (int i = 0; i < length; i++)  
        {  
            int n = random.nextInt(16);  
            if (n < 10)  
            {  
                builder.append(n);  
            }  
            else  
            {  
                builder.append((char) (n + 55));  
            }  
        }  
        return builder.toString();  
    }  
  
    public static void main(String[] args)  
    {  
        for (int i = 0; i < 100; i++)  
        {  
            System.out.println(RandomUtils.getRandomAlphamericStrNew(4));  
            // String num = String.format("%09d", new Random().nextInt(100000));  
            // System.out.println(num);  
        }  
  
    }  
}  

2、凯撒算法工具类

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
public class Caesar  
{  
    private String table;  
    private int seedA = 1103515245;  
    private int seedB = 12345;  
  
    public Caesar(String table, int seed)  
    {  
        this.table = chaos(table, seed, table.length());  
    }  
  
    public Caesar(String table)  
    {  
        this(table, 11);  
    }  
  
    public Caesar()  
    {  
        this(11);  
    }  
  
    public Caesar(int seed)  
    {  
        this("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", seed);  
    }  
  
    public char dict(int i, boolean reverse)  
    {  
        int s = table.length(), index = reverse ? s - i : i;  
        return table.charAt(index);  
    }  
  
    public int dict(char c, boolean reverse)  
    {  
        int s = table.length(), index = table.indexOf(c);  
        return reverse ? s - index : index;  
    }  
  
    public int seed(int seed)  
    {  
        long temp = seed;  
        return (int) ((temp * seedA + seedB) & 0x7fffffffL);  
    }  
  
    public String chaos(String data, int seed, int cnt)  
    {  
        StringBuffer buf = new StringBuffer(data);  
        char tmp;  
        int a, b, r = data.length();  
        for (int i = 0; i < cnt; i += 1)  
        {  
            seed = seed(seed);  
            a = seed % r;  
            seed = seed(seed);  
            b = seed % r;  
            tmp = buf.charAt(a);  
            buf.setCharAt(a, buf.charAt(b));  
            buf.setCharAt(b, tmp);  
        }  
        return buf.toString();  
    }  
  
    public String crypto(boolean reverse, int key, String text)  
    {  
        String ret = null;  
        StringBuilder buf = new StringBuilder();  
        int m, s = table.length(), e = text.length();  
  
        for (int i = 0; i < e; i += 1)  
        {  
            m = dict(text.charAt(i), reverse);  
            if (m < 0)  
            {  
                break;  
            }  
            m = m + key + i;  
            buf.append(dict(m % s, reverse));  
        }  
        if (buf.length() == e)  
        {  
            ret = buf.toString();  
        }  
        return ret;  
    }  
  
    public String encode(int key, String text)  
    {  
        return crypto(false, key, text);  
  
    }  
  
    public String decode(int key, String text)  
    {  
        return crypto(true, key, text);  
    }  
  
    public static void main(String[] args)  
    {  
        Caesar caesar = new Caesar("0123456789qwertyuiopasdfghjklzxcvbnm");  
        String data = caesar.encode(32, "23423");  
        System.out.println(data);  
        String pass = caesar.decode(32, data);  
        System.out.println(pass);  
  
    }  
}