From ca4f38f1305667758c2c2e90b45cd19bdb532721 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Thu, 18 Sep 2014 14:07:32 +0000
Subject: [PATCH] #0 feature - Fixed some SonarQube issues
---
src/main/java/net/curisit/securis/LicenseValidator.java | 12 ++-
/dev/null | 157 ---------------------------------------
src/main/java/net/curisit/securis/utils/JsonUtils.java | 23 ++++-
src/main/java/net/curisit/securis/LicenseManager.java | 6 +
4 files changed, 28 insertions(+), 170 deletions(-)
diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index c2d87e7..45c71f5 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -88,8 +88,9 @@
*/
public LicenseBean requestLicense() throws SeCurisException {
RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
- if (true)
+ if (true) {
throw new SeCurisException("Action not implemented yet");
+ }
LicenseBean lic = requestLicenseToServer(req);
return lic;
}
@@ -150,8 +151,9 @@
*/
public LicenseBean renew(File licenseFile) throws SeCurisException {
LicenseBean lic = validateLicense(licenseFile);
- if (true)
+ if (true) {
throw new SeCurisException("Action not implemented yet");
+ }
// TODO: Send the current LicenseBean to server to check if a new one is prepared.
return lic;
}
diff --git a/src/main/java/net/curisit/securis/LicenseValidator.java b/src/main/java/net/curisit/securis/LicenseValidator.java
index a9fa2ad..2b95123 100644
--- a/src/main/java/net/curisit/securis/LicenseValidator.java
+++ b/src/main/java/net/curisit/securis/LicenseValidator.java
@@ -16,15 +16,18 @@
private static final Logger log = LogManager.getLogger(LicenseValidator.class);
- public static LicenseValidator singleton = new LicenseValidator();
- public static byte[] LOGO_SECRET;
+ private static LicenseValidator singleton = new LicenseValidator();
+ protected static byte[] LOGO_SECRET;
- private LicenseValidator() {
+ static {
try {
LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
log.error("Unexpected error getting LOGO secret", e);
}
+ }
+
+ private LicenseValidator() {
}
public static LicenseValidator getInstance() {
@@ -57,8 +60,9 @@
public void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException {
RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode);
- if (!currentHW.match(reqBean))
+ if (!currentHW.match(reqBean)) {
throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: " + JsonUtils.toJSON(reqBean, true) + "\n Expected: " + JsonUtils.toJSON(currentHW, true));
+ }
}
}
diff --git a/src/main/java/net/curisit/securis/utils/CryptoHelper.java b/src/main/java/net/curisit/securis/utils/CryptoHelper.java
deleted file mode 100644
index 5bef274..0000000
--- a/src/main/java/net/curisit/securis/utils/CryptoHelper.java
+++ /dev/null
@@ -1,157 +0,0 @@
-package net.curisit.securis.utils;
-
-import java.io.UnsupportedEncodingException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.security.SecureRandom;
-import java.security.spec.InvalidKeySpecException;
-import java.util.Arrays;
-
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.SecretKey;
-import javax.crypto.SecretKeyFactory;
-import javax.crypto.spec.PBEKeySpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import net.curisit.securis.SeCurisException;
-
-import org.apache.commons.net.util.Base64;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
-//import net.curisit.common.ui.Dialogs;
-
-public class CryptoHelper {
-
- private static final Logger log = LogManager.getLogger(SignatureHelper.class);
-
- private static final String KEY_FACTORY = "PBEWITHHMACSHA1";
- private static final String PPROVIDER = "BC";
- private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
-
- private static CryptoHelper singleton = new CryptoHelper();
-
- private String passPhrase = null;
-
- private CryptoHelper() {
- }
-
- public static CryptoHelper getInstance() {
- return singleton;
- }
-
- /**
- * Encrypts a given text with AES algorithm
- *
- * @param plainText
- * @return The encrypted text in Base64
- */
- public String encrypt(String plainText) throws SeCurisException {
- return encrypt(plainText, this.passPhrase);
- }
-
- public String encrypt(String plainText, String pass) throws SeCurisException {
- Cipher aes;
- try {
- aes = Cipher.getInstance(CIPHER_ALGORITHM);
- byte[] salt = getSalt();
- aes.init(Cipher.ENCRYPT_MODE, getSecretKey(salt, pass));
- byte[] ciphertext = aes.doFinal(plainText.getBytes("utf-8"));
-
- return Base64.encodeBase64String(salt) + "\n" + Base64.encodeBase64String(ciphertext);
- } catch (NoSuchAlgorithmException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (NoSuchPaddingException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (InvalidKeyException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (IllegalBlockSizeException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (BadPaddingException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (UnsupportedEncodingException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- }
- }
-
- /**
- * Encrypts a given text with AES algorithm
- *
- * @param plainText
- * in Base64
- * @return
- */
- public String decript(String ciphertext) throws SeCurisException {
- return decript(ciphertext, this.passPhrase);
- }
-
- public String decript(String ciphertext, String pass) throws SeCurisException {
- Cipher aes;
- try {
- aes = Cipher.getInstance(CIPHER_ALGORITHM);
- int sep = ciphertext.indexOf('\n');
- if (sep == -1)
- throw new SeCurisException("Unknown format ciphered text");
- byte[] salt = Base64.decodeBase64(ciphertext.substring(0, sep));
- aes.init(Cipher.DECRYPT_MODE, getSecretKey(salt, pass));
- byte[] encryptedBytes = Base64.decodeBase64(ciphertext.substring(sep + 1));
- String cleartext = new String(aes.doFinal(encryptedBytes));
- return cleartext;
- } catch (NoSuchAlgorithmException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (NoSuchPaddingException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (InvalidKeyException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (IllegalBlockSizeException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- } catch (BadPaddingException e) {
- log.error("Error decrypting text", e);
- throw new SeCurisException("Error decrypting text", e);
- }
- }
-
- private byte[] getSalt() throws SeCurisException {
- byte[] salt = new byte[20];
- new SecureRandom().nextBytes(salt);
- return salt;
- }
-
- private SecretKeySpec getSecretKey(byte[] salt, String pass) throws SeCurisException {
- String passPhrase = pass.replace('a', 'ä');
-
- try {
- int iterations = 10000;
-
- SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_FACTORY, PPROVIDER);
- SecretKey tmp = factory.generateSecret(new PBEKeySpec(passPhrase.toCharArray(), salt, iterations, 128));
- byte[] key = Arrays.copyOf(tmp.getEncoded(), 16);
- SecretKeySpec spec = new SecretKeySpec(key, "AES");
- return spec;
-
- } catch (NoSuchAlgorithmException e) {
- log.error("Error generation secret key", e);
- throw new SeCurisException("Error generation secret key", e);
- } catch (InvalidKeySpecException e) {
- log.error("Error generation secret key", e);
- throw new SeCurisException("Error generation secret key", e);
- } catch (NoSuchProviderException e) {
- log.error("Error generation secret key", e);
- throw new SeCurisException("Error generation secret key", e);
- }
- }
-}
diff --git a/src/main/java/net/curisit/securis/utils/JsonUtils.java b/src/main/java/net/curisit/securis/utils/JsonUtils.java
index 382178f..c134c05 100644
--- a/src/main/java/net/curisit/securis/utils/JsonUtils.java
+++ b/src/main/java/net/curisit/securis/utils/JsonUtils.java
@@ -45,6 +45,7 @@
* @param type
* @return
*/
+ @SuppressWarnings("unchecked")
public static <T> T value(Object value, Class<T> type) {
return (T) value;
@@ -52,8 +53,9 @@
public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
try {
- if (json == null)
+ if (json == null){
return null;
+ }
return MAPPER.readValue(json, type);
} catch (JsonParseException e) {
log.error("Error parsing JSON string to obejct: {}", json, e);
@@ -78,8 +80,9 @@
public static String toJSON(Object obj) throws SeCurisException {
// and could also do other configuration...
try {
- if (obj == null)
+ if (obj == null) {
return null;
+ }
return MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error("Error formating JSON from object: {}", obj, e);
@@ -100,8 +103,9 @@
public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
// and could also do other configuration...
try {
- if (obj == null)
+ if (obj == null) {
return null;
+ }
MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
String json = MAPPER.writeValueAsString(obj);
MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
@@ -124,11 +128,13 @@
* String with json format
* @return
*/
+ @SuppressWarnings("unchecked")
public static Map<String, Object> json2map(String json) throws JsonParseException {
try {
- if (json == null)
+ if (json == null) {
return null;
+ }
return MAPPER.readValue(json, Map.class);
} catch (JsonParseException e) {
log.error("Error parsing JSON string to map: {}", json, e);
@@ -149,8 +155,9 @@
public static String map2json(Map<String, Object> map) {
// and could also do other configuration...
try {
- if (map == null)
+ if (map == null) {
return null;
+ }
return MAPPER.writeValueAsString(map);
} catch (JsonProcessingException e) {
log.error("Error formating JSON from map: {}", map, e);
@@ -170,15 +177,17 @@
* String with json format
* @return
*/
- public static List<Object> json2list(String json) {
+ @SuppressWarnings("unchecked")
+ public static List<Object> json2list(String json) throws SeCurisException {
try {
return MAPPER.readValue(json, List.class);
} catch (JsonParseException e) {
log.error("Error converting JSON string to object {}", json, e);
+ throw new SeCurisException("Error converting JSON to object", e);
} catch (IOException e) {
log.error("Error converting JSON string to object {}", json, e);
+ throw new SeCurisException("Error converting JSON to object", e);
}
- return null;
}
public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
--
Gitblit v1.3.2