JAMES-2472 refactor hashPassword
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/18f5da3a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/18f5da3a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/18f5da3a Branch: refs/heads/master Commit: 18f5da3aac7d298caaebe349713059060b8497c3 Parents: c140340 Author: Matthieu Baechler <[email protected]> Authored: Tue Jul 17 16:06:42 2018 +0200 Committer: Matthieu Baechler <[email protected]> Committed: Fri Jul 20 10:03:52 2018 +0200 ---------------------------------------------------------------------- .../apache/james/user/jpa/model/JPAUser.java | 34 +++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/18f5da3a/server/data/data-jpa/src/main/java/org/apache/james/user/jpa/model/JPAUser.java ---------------------------------------------------------------------- diff --git a/server/data/data-jpa/src/main/java/org/apache/james/user/jpa/model/JPAUser.java b/server/data/data-jpa/src/main/java/org/apache/james/user/jpa/model/JPAUser.java index 5e2c1f1..07c9a1c 100644 --- a/server/data/data-jpa/src/main/java/org/apache/james/user/jpa/model/JPAUser.java +++ b/server/data/data-jpa/src/main/java/org/apache/james/user/jpa/model/JPAUser.java @@ -19,6 +19,8 @@ package org.apache.james.user.jpa.model; +import java.util.function.Function; + import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; @@ -52,19 +54,27 @@ public class JPAUser implements User { */ @VisibleForTesting static String hashPassword(String password, String alg) { - String newPass; - if (alg == null || alg.equals("MD5")) { - newPass = DigestUtils.md5Hex(password); - } else if (alg.equals("NONE")) { - newPass = "password"; - } else if (alg.equals("SHA-256")) { - newPass = DigestUtils.sha256Hex(password); - } else if (alg.equals("SHA-512")) { - newPass = DigestUtils.sha512Hex(password); - } else { - newPass = DigestUtils.sha1Hex(password); + return chooseHashFunction(alg).apply(password); + } + + interface HashFunction extends Function<String, String> {} + + private static HashFunction chooseHashFunction(String algorithm) { + if (algorithm == null) { + return DigestUtils::md5Hex; + } + switch (algorithm) { + case "MD5": + return DigestUtils::md5Hex; + case "NONE": + return (password) -> "password"; + case "SHA-256": + return DigestUtils::sha256Hex; + case "SHA-512": + return DigestUtils::sha512Hex; + default: + return DigestUtils::sha1Hex; } - return newPass; } /** Prevents concurrent modification */ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
