| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.utils; |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.math.BigInteger; |
|---|
| 4 | +import java.nio.charset.Charset; |
|---|
| 4 | 5 | import java.security.MessageDigest; |
|---|
| 5 | 6 | import java.security.NoSuchAlgorithmException; |
|---|
| 7 | +import java.util.zip.CRC32; |
|---|
| 6 | 8 | |
|---|
| 7 | 9 | import org.apache.logging.log4j.LogManager; |
|---|
| 8 | 10 | import org.apache.logging.log4j.Logger; |
|---|
| 9 | 11 | |
|---|
| 10 | 12 | public class LicUtils { |
|---|
| 11 | 13 | |
|---|
| 12 | | - private static final Logger LOG = LogManager.getLogger(LicUtils.class); |
|---|
| 14 | + private static final Logger LOG = LogManager.getLogger(LicUtils.class); |
|---|
| 13 | 15 | |
|---|
| 14 | | - public static String md5(String str) { |
|---|
| 15 | | - try { |
|---|
| 16 | | - MessageDigest mDigest = MessageDigest.getInstance("MD5"); |
|---|
| 17 | | - mDigest.update(str.getBytes(), 0, str.length()); |
|---|
| 18 | | - BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 19 | | - return String.format("%1$032x", i); |
|---|
| 20 | | - } catch (NoSuchAlgorithmException e) { |
|---|
| 21 | | - LOG.error("Error generating MD5 for string: " + str, e); |
|---|
| 22 | | - } |
|---|
| 23 | | - return null; |
|---|
| 24 | | - } |
|---|
| 16 | + public static String md5(String str) { |
|---|
| 17 | + try { |
|---|
| 18 | + MessageDigest mDigest = MessageDigest.getInstance("MD5"); |
|---|
| 19 | + mDigest.update(str.getBytes(DEFAULT_CHARSET), 0, str.length()); |
|---|
| 20 | + BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 21 | + return String.format("%1$032x", i); |
|---|
| 22 | + } catch (NoSuchAlgorithmException e) { |
|---|
| 23 | + LOG.error("Error generating MD5 for string: " + str, e); |
|---|
| 24 | + } |
|---|
| 25 | + return null; |
|---|
| 26 | + } |
|---|
| 25 | 27 | |
|---|
| 26 | | - public static String sha256(String str) { |
|---|
| 27 | | - return sha256(str.getBytes()); |
|---|
| 28 | | - } |
|---|
| 28 | + public static String sha256(String str) { |
|---|
| 29 | + return sha256(str.getBytes(DEFAULT_CHARSET)); |
|---|
| 30 | + } |
|---|
| 29 | 31 | |
|---|
| 30 | | - public static String sha256(byte[] bytes) { |
|---|
| 31 | | - try { |
|---|
| 32 | | - MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); |
|---|
| 33 | | - mDigest.update(bytes, 0, bytes.length); |
|---|
| 34 | | - BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 35 | | - return String.format("%1$064x", i); |
|---|
| 36 | | - } catch (NoSuchAlgorithmException e) { |
|---|
| 37 | | - LOG.error("Error generating SHA-256 for bytes: " + bytes, e); |
|---|
| 38 | | - } |
|---|
| 39 | | - return null; |
|---|
| 40 | | - } |
|---|
| 32 | + public static String sha256(byte[] bytes) { |
|---|
| 33 | + try { |
|---|
| 34 | + MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); |
|---|
| 35 | + mDigest.update(bytes, 0, bytes.length); |
|---|
| 36 | + BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 37 | + return String.format("%1$064x", i); |
|---|
| 38 | + } catch (NoSuchAlgorithmException e) { |
|---|
| 39 | + LOG.error("Error generating SHA-256 for bytes: " + bytes, e); |
|---|
| 40 | + } |
|---|
| 41 | + return null; |
|---|
| 42 | + } |
|---|
| 41 | 43 | |
|---|
| 42 | | - public static String sha256(byte[]... bytes) { |
|---|
| 43 | | - try { |
|---|
| 44 | | - MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); |
|---|
| 45 | | - for (byte[] bs : bytes) { |
|---|
| 46 | | - mDigest.update(bs, 0, bs.length); |
|---|
| 47 | | - } |
|---|
| 48 | | - BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 49 | | - return String.format("%1$064x", i); |
|---|
| 50 | | - } catch (NoSuchAlgorithmException e) { |
|---|
| 51 | | - LOG.error("Error generating SHA-256 for bytes: " + bytes, e); |
|---|
| 52 | | - } |
|---|
| 53 | | - return null; |
|---|
| 54 | | - } |
|---|
| 44 | + public static String sha256(byte[]... bytes) { |
|---|
| 45 | + try { |
|---|
| 46 | + MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); |
|---|
| 47 | + for (byte[] bs : bytes) { |
|---|
| 48 | + mDigest.update(bs, 0, bs.length); |
|---|
| 49 | + } |
|---|
| 50 | + BigInteger i = new BigInteger(1, mDigest.digest()); |
|---|
| 51 | + return String.format("%1$064x", i); |
|---|
| 52 | + } catch (NoSuchAlgorithmException e) { |
|---|
| 53 | + LOG.error("Error generating SHA-256 for bytes: " + bytes, e); |
|---|
| 54 | + } |
|---|
| 55 | + return null; |
|---|
| 56 | + } |
|---|
| 55 | 57 | |
|---|
| 58 | + private final static Charset DEFAULT_CHARSET = Charset.forName("utf-8"); |
|---|
| 59 | + |
|---|
| 60 | + public static String getLicenseCrc(String packCode, String licSufixCode) { |
|---|
| 61 | + CRC32 crc = new CRC32(); |
|---|
| 62 | + crc.update(packCode.getBytes(DEFAULT_CHARSET)); |
|---|
| 63 | + crc.update(licSufixCode.getBytes(DEFAULT_CHARSET)); |
|---|
| 64 | + return String.format("%03d", crc.getValue() % 1000); |
|---|
| 65 | + } |
|---|
| 66 | + |
|---|
| 67 | + public static String getLicenseCrc(String packCode, Integer licSufixCode) { |
|---|
| 68 | + return getLicenseCrc(packCode, licSufixCode.toString()); |
|---|
| 69 | + } |
|---|
| 70 | + |
|---|
| 71 | + public static Integer getLicenseCodeSuffix(String licCode) { |
|---|
| 72 | + String[] parts = licCode.split("-"); |
|---|
| 73 | + |
|---|
| 74 | + return new Integer(parts[2]); |
|---|
| 75 | + } |
|---|
| 76 | + |
|---|
| 77 | + public static String getLicenseCode(String packCode, Integer licSufixCode) { |
|---|
| 78 | + String crc = getLicenseCrc(packCode, licSufixCode); |
|---|
| 79 | + return String.format("%s-%s-%s", packCode, crc, licSufixCode); |
|---|
| 80 | + } |
|---|
| 81 | + |
|---|
| 82 | + /** |
|---|
| 83 | + * Check if the license code is valid according to the format, the CRC is |
|---|
| 84 | + * validated |
|---|
| 85 | + * |
|---|
| 86 | + * @param licCode |
|---|
| 87 | + * @return true if license code format and its CRC are valid |
|---|
| 88 | + */ |
|---|
| 89 | + public static boolean checkValidLicenseCodeCrc(String licCode) { |
|---|
| 90 | + String[] parts = licCode.split("-"); |
|---|
| 91 | + if (parts.length != 3) { |
|---|
| 92 | + return false; |
|---|
| 93 | + } |
|---|
| 94 | + String crc = getLicenseCrc(parts[0], parts[2]); |
|---|
| 95 | + return crc.equals(parts[1]); |
|---|
| 96 | + } |
|---|
| 97 | + |
|---|
| 98 | + public static void main(String[] args) { |
|---|
| 99 | + String code = getLicenseCode("PCK01", 5); |
|---|
| 100 | + System.out.println(code); |
|---|
| 101 | + System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-")); |
|---|
| 102 | + } |
|---|
| 56 | 103 | } |
|---|