From c8de3a22aaeded0709a7d3d6ffbe7dbb5d8566e8 Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Thu, 3 May 2012 22:19:58 +0000 Subject: [PATCH] OPTIMIZE: Add a getMessageDigest static helper function. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1333661 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/base/crypto/HashCrypt.java | 100 +++++++++--------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java b/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java index d05859dc22..8c07c697ff 100644 --- a/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java +++ b/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java @@ -42,56 +42,60 @@ public class HashCrypt { public static final String module = HashCrypt.class.getName(); public static final String CRYPT_CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; - public static boolean comparePassword(String crypted, String defaultCrypt, String password) { + public static MessageDigest getMessageDigest(String type) { try { - if (crypted.startsWith("{")) { - int typeEnd = crypted.indexOf("}"); - String hashType = crypted.substring(1, typeEnd); - String hashed = crypted.substring(typeEnd + 1); - MessageDigest messagedigest = MessageDigest.getInstance(hashType); - // FIXME: should have been getBytes("UTF-8") originally - messagedigest.update(password.getBytes()); - byte[] digestBytes = messagedigest.digest(); - char[] digestChars = Hex.encodeHex(digestBytes); - String checkCrypted = new String(digestChars); - if (hashed.equals(checkCrypted)) { - return true; - } - // This next block should be removed when all {prefix}oldFunnyHex are fixed. - int k = 0; - digestChars = new char[digestBytes.length * 2]; - for (int l = 0; l < digestBytes.length; l++) { - int i1 = digestBytes[l]; - - if (i1 < 0) { - i1 = 127 + i1 * -1; - } - StringUtil.encodeInt(i1, k, digestChars); - k += 2; - } - if (hashed.equals(new String(digestChars))) { - Debug.logWarning("Warning: detected oldFunnyHex password prefixed with a hashType; this is not valid, please update the value in the database with ({%s}%s)", module, hashType, checkCrypted); - return true; - } - return false; - } else if (crypted.startsWith("$")) { - int typeEnd = crypted.indexOf("$", 1); - int saltEnd = crypted.indexOf("$", typeEnd + 1); - String hashType = crypted.substring(1, typeEnd); - String salt = crypted.substring(typeEnd + 1, saltEnd); - String hashed = crypted.substring(saltEnd + 1); - return hashed.equals(getCrypted(hashType, salt, password.getBytes(UTF8))); - } else { - String hashType = defaultCrypt; - String hashed = crypted; - MessageDigest messagedigest = MessageDigest.getInstance(hashType); - // FIXME: should have been getBytes("UTF-8") originally - messagedigest.update(password.getBytes()); - char[] digestChars = Hex.encodeHex(messagedigest.digest()); - return hashed.equals(new String(digestChars)); - } + return MessageDigest.getInstance(type); } catch (NoSuchAlgorithmException e) { - throw new GeneralRuntimeException("Error while comparing password", e); + throw new GeneralRuntimeException("Could not load digestor(" + type + ")", e); + } + } + + public static boolean comparePassword(String crypted, String defaultCrypt, String password) { + if (crypted.startsWith("{")) { + int typeEnd = crypted.indexOf("}"); + String hashType = crypted.substring(1, typeEnd); + String hashed = crypted.substring(typeEnd + 1); + MessageDigest messagedigest = getMessageDigest(hashType); + // FIXME: should have been getBytes("UTF-8") originally + messagedigest.update(password.getBytes()); + byte[] digestBytes = messagedigest.digest(); + char[] digestChars = Hex.encodeHex(digestBytes); + String checkCrypted = new String(digestChars); + if (hashed.equals(checkCrypted)) { + return true; + } + // This next block should be removed when all {prefix}oldFunnyHex are fixed. + int k = 0; + digestChars = new char[digestBytes.length * 2]; + for (int l = 0; l < digestBytes.length; l++) { + int i1 = digestBytes[l]; + + if (i1 < 0) { + i1 = 127 + i1 * -1; + } + StringUtil.encodeInt(i1, k, digestChars); + k += 2; + } + if (hashed.equals(new String(digestChars))) { + Debug.logWarning("Warning: detected oldFunnyHex password prefixed with a hashType; this is not valid, please update the value in the database with ({%s}%s)", module, hashType, checkCrypted); + return true; + } + return false; + } else if (crypted.startsWith("$")) { + int typeEnd = crypted.indexOf("$", 1); + int saltEnd = crypted.indexOf("$", typeEnd + 1); + String hashType = crypted.substring(1, typeEnd); + String salt = crypted.substring(typeEnd + 1, saltEnd); + String hashed = crypted.substring(saltEnd + 1); + return hashed.equals(getCrypted(hashType, salt, password.getBytes(UTF8))); + } else { + String hashType = defaultCrypt; + String hashed = crypted; + MessageDigest messagedigest = getMessageDigest(hashType); + // FIXME: should have been getBytes("UTF-8") originally + messagedigest.update(password.getBytes()); + char[] digestChars = Hex.encodeHex(messagedigest.digest()); + return hashed.equals(new String(digestChars)); } }