You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2012/05/04 00:19:59 UTC
svn commit: r1333661 -
/ofbiz/trunk/framework/base/src/org/ofbiz/base/crypto/HashCrypt.java
Author: doogie
Date: Thu May 3 22:19:58 2012
New Revision: 1333661
URL: http://svn.apache.org/viewvc?rev=1333661&view=rev
Log:
OPTIMIZE: Add a getMessageDigest static helper function.
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=1333661&r1=1333660&r2=1333661&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:19:58 2012
@@ -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 MessageDigest.getInstance(type);
+ } catch (NoSuchAlgorithmException 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;
}
- 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));
+ StringUtil.encodeInt(i1, k, digestChars);
+ k += 2;
}
- } catch (NoSuchAlgorithmException e) {
- throw new GeneralRuntimeException("Error while comparing password", e);
+ 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));
}
}