You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/12/08 03:39:57 UTC

[james-project] 04/09: JAMES-3674 Have hashing logic encapsulated with algorithm

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit d0e76a3ab1fa2bb851048f3cc20a04dcbcd8acd7
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Dec 3 11:28:57 2021 +0700

    JAMES-3674 Have hashing logic encapsulated with algorithm
---
 .../org/apache/james/user/lib/model/Algorithm.java | 42 ++++++++++++++++++++++
 .../apache/james/user/lib/model/DefaultUser.java   | 18 +++-------
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/server/data/data-library/src/main/java/org/apache/james/user/lib/model/Algorithm.java b/server/data/data-library/src/main/java/org/apache/james/user/lib/model/Algorithm.java
index 3cca98e..6e0222d 100644
--- a/server/data/data-library/src/main/java/org/apache/james/user/lib/model/Algorithm.java
+++ b/server/data/data-library/src/main/java/org/apache/james/user/lib/model/Algorithm.java
@@ -19,6 +19,12 @@
 
 package org.apache.james.user.lib.model;
 
+import static java.nio.charset.StandardCharsets.ISO_8859_1;
+
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
@@ -27,6 +33,36 @@ import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 
 public class Algorithm {
+    public interface Hasher {
+        static Hasher from(Algorithm algorithm) {
+            return new RegularHashingSpec(algorithm);
+        }
+
+        byte[] digestString(String pass, String salt) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException;
+    }
+
+    public static class RegularHashingSpec implements Hasher {
+        private final Algorithm algorithm;
+
+        public RegularHashingSpec(Algorithm algorithm) {
+            this.algorithm = algorithm;
+        }
+
+        @Override
+        public byte[] digestString(String pass, String salt) throws NoSuchAlgorithmException {
+            MessageDigest md = MessageDigest.getInstance(algorithm.getName());
+            String saltedPass = applySalt(algorithm, pass, salt);
+            return md.digest(saltedPass.getBytes(ISO_8859_1));
+        }
+
+        private String applySalt(Algorithm algorithm, String pass, String salt) {
+            if (algorithm.isSalted()) {
+                return salt + pass;
+            } else {
+                return pass;
+            }
+        }
+    }
 
     public enum HashingMode {
         PLAIN,
@@ -61,10 +97,12 @@ public class Algorithm {
 
     private final String rawValue;
     private final HashingMode hashingMode;
+    private final Hasher hasher;
 
     private Algorithm(String rawValue, HashingMode hashingMode) {
         this.rawValue = rawValue;
         this.hashingMode = hashingMode;
+        this.hasher = Hasher.from(this);
     }
 
     public String asString() {
@@ -87,6 +125,10 @@ public class Algorithm {
         return hashingMode == HashingMode.SALTED || hashingMode == HashingMode.LEGACY_SALTED;
     }
 
+    public Hasher hasher() {
+        return hasher;
+    }
+
     @Override
     public final boolean equals(Object o) {
         if (o instanceof Algorithm) {
diff --git a/server/data/data-library/src/main/java/org/apache/james/user/lib/model/DefaultUser.java b/server/data/data-library/src/main/java/org/apache/james/user/lib/model/DefaultUser.java
index 96740ab..4a802de 100644
--- a/server/data/data-library/src/main/java/org/apache/james/user/lib/model/DefaultUser.java
+++ b/server/data/data-library/src/main/java/org/apache/james/user/lib/model/DefaultUser.java
@@ -25,8 +25,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Serializable;
-import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeUtility;
@@ -129,6 +129,8 @@ public class DefaultUser implements User, Serializable {
         return currentAlgorithm;
     }
 
+
+
     /**
      * Calculate digest of given String using given algorithm. Encode digest in
      * MIME-like base64.
@@ -144,11 +146,9 @@ public class DefaultUser implements User, Serializable {
      */
     static String digestString(String pass, Algorithm algorithm, String salt) throws NoSuchAlgorithmException {
         try {
-            MessageDigest md = MessageDigest.getInstance(algorithm.getName());
-            String saltedPass = applySalt(algorithm, pass, salt);
-            byte[] digest = md.digest(saltedPass.getBytes(ISO_8859_1));
+            byte[] digest = algorithm.hasher().digestString(pass, salt);
             return encodeInBase64(algorithm, digest);
-        } catch (IOException | MessagingException e) {
+        } catch (IOException | MessagingException | InvalidKeySpecException e) {
             throw new RuntimeException("Fatal error", e);
         }
     }
@@ -162,12 +162,4 @@ public class DefaultUser implements User, Serializable {
         }
         return bos.toString(ISO_8859_1);
     }
-
-    static String applySalt(Algorithm algorithm, String pass, String salt) {
-        if (algorithm.isSalted()) {
-            return salt + pass;
-        } else {
-            return pass;
-        }
-    }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org