-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicenseKeyGenerator.java
More file actions
65 lines (46 loc) · 2 KB
/
Copy pathLicenseKeyGenerator.java
File metadata and controls
65 lines (46 loc) · 2 KB
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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.UUID;
final class LicenseKeyGenerator {
private static final String ALGORITHM = "SHA-256";
LicenseKeyGenerator() {
}
void generateLicenseKeyForUser() throws NoSuchAlgorithmException {
LocalDate expirationDate = LocalDate.of(2027, 10, 14);
String featureFlags = "USER,SUPPORT=YES";
generateLicenseKey(expirationDate, featureFlags);
}
void generateLicenseKeyForProUser() throws NoSuchAlgorithmException {
LocalDate expirationDate = LocalDate.of(2030, 10, 14);
String featureFlags = "PRO-USER,SUPPORT=YES";
generateLicenseKey(expirationDate, featureFlags);
}
void generateLicenseKey(LocalDate expirationDate, String featureFlags) throws NoSuchAlgorithmException {
final String uniqueId = getRandomtUuid().toString();
// Combine the components
StringBuilder licenseKey = new StringBuilder();
licenseKey.append(uniqueId).append("-");
licenseKey.append(expirationDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")))
.append("-");
licenseKey.append(featureFlags);
// Generate a checksum/hash
MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
byte[] hashBytes = digest.digest(licenseKey.toString().getBytes());
String checksum = bytesToHex(hashBytes);
final String output = licenseKey.append("-").append(checksum).toString();
System.out.println("Generated License Key: " + output);
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
private UUID getRandomtUuid() {
UUID uuid = UUID.randomUUID();
System.out.println("Generated UUID: " + uuid.toString());
return uuid;
}
}