svn commit: r1818501 - in /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto: BlowFishCrypt.java DesCrypt.java HashCrypt.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1818501 - in /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto: BlowFishCrypt.java DesCrypt.java HashCrypt.java

mbrohl
Author: mbrohl
Date: Sun Dec 17 18:51:18 2017
New Revision: 1818501

URL: http://svn.apache.org/viewvc?rev=1818501&view=rev
Log:
Improved: General refactoring and code improvements, package
org.apache.ofbiz.base.crypto.
(OFBIZ-9942)

Thanks Dennis Balkir for reporting and providing the patches.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/BlowFishCrypt.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/DesCrypt.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/BlowFishCrypt.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/BlowFishCrypt.java?rev=1818501&r1=1818500&r2=1818501&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/BlowFishCrypt.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/BlowFishCrypt.java Sun Dec 17 18:51:18 2017
@@ -121,8 +121,9 @@ public class BlowFishCrypt {
     }
 
     private byte[] crypt(byte[] bytes, int mode) throws Exception {
-        if (secretKeySpec == null)
+        if (secretKeySpec == null) {
             throw new Exception("SecretKey cannot be null.");
+        }
         Cipher cipher = Cipher.getInstance("Blowfish");
 
         cipher.init(mode, secretKeySpec);

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/DesCrypt.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/DesCrypt.java?rev=1818501&r1=1818500&r2=1818501&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/DesCrypt.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/DesCrypt.java Sun Dec 17 18:51:18 2017
@@ -55,11 +55,7 @@ public class DesCrypt {
         byte[] encBytes = null;
         try {
             encBytes = cipher.doFinal(bytes);
-        } catch (IllegalStateException e) {
-            throw new GeneralException(e);
-        } catch (IllegalBlockSizeException e) {
-            throw new GeneralException(e);
-        } catch (BadPaddingException e) {
+        } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException e) {
             throw new GeneralException(e);
         }
         return encBytes;
@@ -70,11 +66,7 @@ public class DesCrypt {
         byte[] decBytes = null;
         try {
             decBytes = cipher.doFinal(bytes);
-        } catch (IllegalStateException e) {
-            throw new GeneralException(e);
-        } catch (IllegalBlockSizeException e) {
-            throw new GeneralException(e);
-        } catch (BadPaddingException e) {
+        } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException e) {
             throw new GeneralException(e);
         }
         return decBytes;
@@ -105,9 +97,8 @@ public class DesCrypt {
                 throw new GeneralException(e);
             }
             return key;
-        } else {
-            throw new GeneralException("Not a valid DESede key!");
         }
+        throw new GeneralException("Not a valid DESede key!");
     }
 
     // return a cipher for a key - DESede/CBC/PKCS5Padding IV = 0
@@ -119,16 +110,12 @@ public class DesCrypt {
         Cipher encCipher = null;
         try {
             encCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
-        } catch (NoSuchAlgorithmException e) {
-            throw new GeneralException(e);
-        } catch (NoSuchPaddingException e) {
+        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
             throw new GeneralException(e);
         }
         try {
             encCipher.init(mode, key, iv);
-        } catch (InvalidKeyException e) {
-            throw new GeneralException(e);
-        } catch (InvalidAlgorithmParameterException e) {
+        } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
             throw new GeneralException(e);
         }
         return encCipher;

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java?rev=1818501&r1=1818500&r2=1818501&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java Sun Dec 17 18:51:18 2017
@@ -48,7 +48,7 @@ public class HashCrypt {
     public static final String CRYPT_CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
 
     private static final String PBKDF2_SHA1 ="PBKDF2-SHA1";
-    private static final String PBKDF2_SHA256 ="PBKDF2-SHA256";
+    private static final String PBKDF2_SHA256 ="PBKDF2-SHA256";
     private static final String PBKDF2_SHA384 ="PBKDF2-SHA384";
     private static final String PBKDF2_SHA512 ="PBKDF2-SHA512";
     private static final int PBKDF2_ITERATIONS = UtilProperties.getPropertyAsInteger("security.properties", "password.encrypt.pbkdf2.iterations", 10000);
@@ -200,7 +200,7 @@ public class HashCrypt {
             throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e);
         }
     }
-    
+
     public static boolean doComparePbkdf2(String crypted, String password){
         try {
             int typeEnd = crypted.indexOf("}");
@@ -221,17 +221,17 @@ public class HashCrypt {
                 case "SHA512":
                     hashType = "PBKDF2WithHmacSHA512";
                     break;
-                default:  
+                default:
                     hashType = "PBKDF2WithHmacSHA1";
             }
             SecretKeyFactory skf = SecretKeyFactory.getInstance(hashType);
             byte[] testHash = skf.generateSecret(spec).getEncoded();
             int diff = hash.length ^ testHash.length;
-            
+
             for (int i = 0; i < hash.length && i < testHash.length; i++) {
                 diff |= hash[i] ^ testHash[i];
             }
-            
+
             return diff == 0;
         } catch (NoSuchAlgorithmException e) {
             throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e);
@@ -239,7 +239,7 @@ public class HashCrypt {
             throw new GeneralRuntimeException("Error while creating SecretKey", e);
         }
     }
-    
+
     private static String getSalt() {
         try {
             SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
@@ -250,14 +250,18 @@ public class HashCrypt {
             throw new GeneralRuntimeException("Error while creating salt", e);
         }
     }
-    
+
     public static String digestHash(String hashType, String code, String str) {
-        if (str == null) return null;
+        if (str == null) {
+            return null;
+        }
         byte[] codeBytes;
         try {
-            if (code == null)
+            if (code == null) {
                 codeBytes = str.getBytes(UtilIO.getUtf8());
-            else codeBytes = str.getBytes(code);
+            } else {
+                codeBytes = str.getBytes(code);
+            }
         } catch (UnsupportedEncodingException e) {
             throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
         }
@@ -331,8 +335,12 @@ public class HashCrypt {
     }
 
     public static String digestHashOldFunnyHex(String hashType, String str) {
-        if (UtilValidate.isEmpty(hashType)) hashType = "SHA";
-        if (str == null) return null;
+        if (UtilValidate.isEmpty(hashType)) {
+            hashType = "SHA";
+        }
+        if (str == null) {
+            return null;
+        }
         try {
             MessageDigest messagedigest = MessageDigest.getInstance(hashType);
             byte[] strBytes = str.getBytes(UtilIO.getUtf8());
@@ -349,8 +357,8 @@ public class HashCrypt {
     private static String oldFunnyHex(byte[] bytes) {
         int k = 0;
         char[] digestChars = new char[bytes.length * 2];
-        for (int l = 0; l < bytes.length; l++) {
-            int i1 = bytes[l];
+        for (byte b : bytes) {
+            int i1 = b;
 
             if (i1 < 0) {
                 i1 = 127 + i1 * -1;