| .. | .. |
|---|
| 4 | 4 | import java.nio.charset.Charset; |
|---|
| 5 | 5 | import java.security.MessageDigest; |
|---|
| 6 | 6 | import java.security.NoSuchAlgorithmException; |
|---|
| 7 | +import java.util.ArrayList; |
|---|
| 8 | +import java.util.Arrays; |
|---|
| 9 | +import java.util.List; |
|---|
| 7 | 10 | import java.util.zip.CRC32; |
|---|
| 8 | 11 | |
|---|
| 9 | 12 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 69 | 72 | } |
|---|
| 70 | 73 | |
|---|
| 71 | 74 | public static Integer getLicenseCodeSuffix(String licCode) { |
|---|
| 72 | | - String[] parts = licCode.split("-"); |
|---|
| 75 | + String[] parts = splitLicCode(licCode); |
|---|
| 73 | 76 | |
|---|
| 74 | | - return new Integer(parts[2]); |
|---|
| 77 | + return new Integer(parts[parts.length - 1]); |
|---|
| 75 | 78 | } |
|---|
| 76 | 79 | |
|---|
| 77 | 80 | public static String getLicenseCode(String packCode, Integer licSufixCode) { |
|---|
| .. | .. |
|---|
| 87 | 90 | * @return true if license code format and its CRC are valid |
|---|
| 88 | 91 | */ |
|---|
| 89 | 92 | public static boolean checkValidLicenseCodeCrc(String licCode) { |
|---|
| 90 | | - String[] parts = licCode.split("-"); |
|---|
| 91 | | - if (parts.length != 3) { |
|---|
| 93 | + String[] parts = splitLicCode(licCode); |
|---|
| 94 | + if (parts == null) { |
|---|
| 92 | 95 | return false; |
|---|
| 93 | 96 | } |
|---|
| 94 | 97 | String crc = getLicenseCrc(parts[0], parts[2]); |
|---|
| 95 | 98 | return crc.equals(parts[1]); |
|---|
| 96 | 99 | } |
|---|
| 97 | 100 | |
|---|
| 101 | + /** |
|---|
| 102 | + * We convert the license code in 3 parts: <br> |
|---|
| 103 | + * "P1-344-1 = ["P1", "344", "1"] <br> |
|---|
| 104 | + * "P1-OTHER-344-1 = ["P1-OTHER", "344", "1"] <br> |
|---|
| 105 | + * "P1-1 = null |
|---|
| 106 | + * |
|---|
| 107 | + * @param code |
|---|
| 108 | + * @return The 2 license code packs or null if code is not valid |
|---|
| 109 | + */ |
|---|
| 110 | + private static String[] splitLicCode(String code) { |
|---|
| 111 | + List<Integer> separators = new ArrayList<>(); |
|---|
| 112 | + for (int i = 0; i < code.length(); i++) { |
|---|
| 113 | + if (code.charAt(i) == '-') { |
|---|
| 114 | + separators.add(i); |
|---|
| 115 | + } |
|---|
| 116 | + } |
|---|
| 117 | + if (separators.size() < 2) { |
|---|
| 118 | + return null; |
|---|
| 119 | + } |
|---|
| 120 | + |
|---|
| 121 | + int i0 = separators.get(separators.size() - 2); |
|---|
| 122 | + int i1 = separators.get(separators.size() - 1); |
|---|
| 123 | + String[] parts = new String[3]; |
|---|
| 124 | + parts[0] = code.substring(0, i0); |
|---|
| 125 | + parts[1] = code.substring(i0 + 1, i1); |
|---|
| 126 | + parts[2] = code.substring(i1 + 1); |
|---|
| 127 | + |
|---|
| 128 | + return parts; |
|---|
| 129 | + } |
|---|
| 130 | + |
|---|
| 98 | 131 | public static void main(String[] args) { |
|---|
| 99 | | - String code = getLicenseCode("PCK01", 5); |
|---|
| 132 | + String code = getLicenseCode("PC-K01", 5); |
|---|
| 100 | 133 | System.out.println(code); |
|---|
| 101 | | - System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-")); |
|---|
| 134 | + System.out.println("Is valid ? " + checkValidLicenseCodeCrc(code)); |
|---|
| 135 | + System.out.println("Is valid ? " + Arrays.asList(splitLicCode(code))); |
|---|
| 102 | 136 | } |
|---|
| 103 | 137 | } |
|---|