You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/11/30 02:39:23 UTC

[7/9] james-project git commit: JAMES-2240 Fix use of MD5 for checksum to index email body

JAMES-2240 Fix use of MD5 for checksum to index email body


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4762638a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4762638a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4762638a

Branch: refs/heads/master
Commit: 4762638a94b2ce9b85240f8af109298d1db56267
Parents: d6d0e67
Author: Thibaut SAUTEREAU <ts...@linagora.com>
Authored: Tue Nov 28 14:47:28 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:37:29 2017 +0700

----------------------------------------------------------------------
 .../mailrepository/file/MBoxMailRepository.java | 52 ++++++--------------
 1 file changed, 16 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4762638a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
index 2ea4417..5bb7d7c 100755
--- a/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
+++ b/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/MBoxMailRepository.java
@@ -51,8 +51,6 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -72,13 +70,15 @@ import javax.mail.internet.MimeMessage;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.io.FileUtils;
-import org.apache.james.server.core.MailImpl;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.server.core.MailImpl;
 import org.apache.mailet.Mail;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.hash.Hashing;
+
 /**
  * Implementation of a MailRepository using UNIX mbox files.
  * 
@@ -136,8 +136,8 @@ public class MBoxMailRepository implements MailRepository, Configurable {
     private static boolean BUFFERING = true;
 
     /**
-     * The internal list of the emails The key is an adapted MD5 checksum of the
-     * mail
+     * The internal list of emails.
+     * The key is an adapted SHA-256 fingerprint of the email body.
      */
     private Hashtable<String, Long> mList = null;
     /**
@@ -250,20 +250,10 @@ public class MBoxMailRepository implements MailRepository, Configurable {
     }
 
     /**
-     * Generate a hex representation of an MD5 checksum on the emailbody
-     * 
-     * @param emailBody
-     * @return A hex representation of the text
-     * @throws NoSuchAlgorithmException
+     * Generate a hex representation of a SHA-256 checksum on the email body
      */
-    private String generateKeyValue(String emailBody) throws NoSuchAlgorithmException {
-        // MD5 the email body for a reilable (ha ha) key
-        byte[] digArray = MessageDigest.getInstance("MD5").digest(emailBody.getBytes());
-        StringBuilder digest = new StringBuilder();
-        for (byte aDigArray : digArray) {
-            digest.append(Integer.toString(aDigArray, Character.MAX_RADIX).toUpperCase(Locale.US));
-        }
-        return digest.toString();
+    private String generateKeyValue(String emailBody) {
+        return Hashing.sha256().hashUnencodedChars(emailBody).toString();
     }
 
     /**
@@ -440,13 +430,9 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                 }
 
                 public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
-                    try {
-                        if (key.equals(generateKeyValue(bodyText))) {
-                            LOGGER.debug(this.getClass().getName() + " Located message. Returning MIME message");
-                            return convertTextToMimeMessage(bodyText);
-                        }
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
+                    if (key.equals(generateKeyValue(bodyText))) {
+                        LOGGER.debug(this.getClass().getName() + " Located message. Returning MIME message");
+                        return convertTextToMimeMessage(bodyText);
                     }
                     return null;
                 }
@@ -498,16 +484,12 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                 }
 
                 public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
-                    try {
-                        String key = generateKeyValue(bodyText);
-                        mList.put(key, messageStart);
-                        if ((LOGGER.isDebugEnabled())) {
-                            LOGGER.debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
-                        }
-
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
+                    String key = generateKeyValue(bodyText);
+                    mList.put(key, messageStart);
+                    if ((LOGGER.isDebugEnabled())) {
+                        LOGGER.debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
                     }
+
                     return null;
                 }
             });
@@ -722,8 +704,6 @@ public class MBoxMailRepository implements MailRepository, Configurable {
                             outputFile.writeBytes(bodyText);
 
                         }
-                    } catch (NoSuchAlgorithmException e) {
-                        LOGGER.error("MD5 not supported! ", e);
                     } catch (IOException e) {
                         LOGGER.error("Unable to write file (General I/O problem) " + mboxFile, e);
                     }


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