Author: doogie Date: Thu May 3 22:13:04 2012 New Revision: 1333654 URL: http://svn.apache.org/viewvc?rev=1333654&view=rev Log: OPTIMIZE: Switch internal getCrypted to take a byte array, and use String.getBytes(Charset) to fetch the bytes. Also switches the one case of getBytes("UTF-8") to the Charset version.
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java?rev=1333654&r1=1333653&r2=1333654&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java Thu May 3 22:13:04 2012 @@ -30,6 +30,7 @@ import org.apache.commons.lang.RandomStr import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralRuntimeException; import org.ofbiz.base.util.StringUtil; +import static org.ofbiz.base.util.UtilIO.UTF8; import org.ofbiz.base.util.UtilValidate; /** @@ -79,7 +80,7 @@ public class HashCrypt { 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)); + return hashed.equals(getCrypted(hashType, salt, password.getBytes(UTF8))); } else { String hashType = defaultCrypt; String hashed = crypted; @@ -102,20 +103,18 @@ public class HashCrypt { public static String cryptPassword(String hashType, String salt, String password) { StringBuilder sb = new StringBuilder(); sb.append("$").append(hashType).append("$").append(salt).append("$"); - sb.append(getCrypted(hashType, salt, password)); + sb.append(getCrypted(hashType, salt, password.getBytes(UTF8))); return sb.toString(); } - private static String getCrypted(String hashType, String salt, String password) { + private static String getCrypted(String hashType, String salt, byte[] bytes) { try { MessageDigest messagedigest = MessageDigest.getInstance(hashType); - messagedigest.update(salt.getBytes("UTF-8")); - messagedigest.update(password.getBytes("UTF-8")); + messagedigest.update(salt.getBytes(UTF8)); + messagedigest.update(bytes); return Base64.encodeBase64URLSafeString(messagedigest.digest()).replace('+', '.'); } catch (NoSuchAlgorithmException e) { throw new GeneralRuntimeException("Error while comparing password", e); - } catch (UnsupportedEncodingException e) { - throw new GeneralRuntimeException("Error while comparing password", e); } }