生成激活码

生成激活码

实现方式

使用 Java 自带的 SecureRandom 类可以生成随机码,用于实现卡券等功能。

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
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

public class ActivationCodeUtil {

private static final String COMPLEX_CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

private static final String SIMPLE_CODE = "0123456789";

private static final int SWITCH_SIZE = 999;

private static final int CODE_LENGTH = 6;

private static final int MAX_TICKET_NUM = 9999;

public static Set<String> generate(Integer num) {
SecureRandom random = new SecureRandom();
Set<String> result = new HashSet<>();
while (result.size() < num) {
String codeSet = num < SWITCH_SIZE ? SIMPLE_CODE : COMPLEX_CODE;
StringBuilder activationCode = new StringBuilder();
for (int i = 0; i < CODE_LENGTH; i++) {
int index = random.nextInt(codeSet.length());
activationCode.append(codeSet.charAt(index));
}
result.add(activationCode.toString());
}
return result;
}

}

生成激活码
https://wangqian0306.github.io/2025/activation/
作者
WangQian
发布于
2025年1月2日
许可协议