From 5ff7160871b0ea6cbcc8ead0f0fc40cd15349bae Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 11 Dec 2014 18:56:10 +0000
Subject: [PATCH] #2140 fix - Removed config file from jar and added a new exception for expired licenses

---
 src/main/java/net/curisit/securis/utils/LicUtils.java |   46 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/main/java/net/curisit/securis/utils/LicUtils.java b/src/main/java/net/curisit/securis/utils/LicUtils.java
index 234b14c..4b90d69 100644
--- a/src/main/java/net/curisit/securis/utils/LicUtils.java
+++ b/src/main/java/net/curisit/securis/utils/LicUtils.java
@@ -4,6 +4,9 @@
 import java.nio.charset.Charset;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 import java.util.zip.CRC32;
 
 import org.apache.logging.log4j.LogManager;
@@ -69,9 +72,9 @@
     }
 
     public static Integer getLicenseCodeSuffix(String licCode) {
-        String[] parts = licCode.split("-");
+        String[] parts = splitLicCode(licCode);
 
-        return new Integer(parts[2]);
+        return new Integer(parts[parts.length - 1]);
     }
 
     public static String getLicenseCode(String packCode, Integer licSufixCode) {
@@ -87,17 +90,48 @@
      * @return true if license code format and its CRC are valid
      */
     public static boolean checkValidLicenseCodeCrc(String licCode) {
-        String[] parts = licCode.split("-");
-        if (parts.length != 3) {
+        String[] parts = splitLicCode(licCode);
+        if (parts == null) {
             return false;
         }
         String crc = getLicenseCrc(parts[0], parts[2]);
         return crc.equals(parts[1]);
     }
 
+    /**
+     * We convert the license code in 3 parts: <br>
+     * "P1-344-1 = ["P1", "344", "1"] <br>
+     * "P1-OTHER-344-1 = ["P1-OTHER", "344", "1"] <br>
+     * "P1-1 = null
+     * 
+     * @param code
+     * @return The 2 license code packs or null if code is not valid
+     */
+    private static String[] splitLicCode(String code) {
+        List<Integer> separators = new ArrayList<>();
+        for (int i = 0; i < code.length(); i++) {
+            if (code.charAt(i) == '-') {
+                separators.add(i);
+            }
+        }
+        if (separators.size() < 2) {
+            return null;
+        }
+
+        int i0 = separators.get(separators.size() - 2);
+        int i1 = separators.get(separators.size() - 1);
+        String[] parts = new String[3];
+        parts[0] = code.substring(0, i0);
+        parts[1] = code.substring(i0 + 1, i1);
+        parts[2] = code.substring(i1 + 1);
+
+        return parts;
+    }
+
     public static void main(String[] args) {
-        String code = getLicenseCode("PCK01", 5);
+        String code = getLicenseCode("PC-K01", 5);
         System.out.println(code);
-        System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-"));
+        System.out.println("Is valid ? " + checkValidLicenseCodeCrc(code));
+        System.out.println("Is valid ? " + Arrays.asList(splitLicCode(code)));
     }
 }

--
Gitblit v1.3.2