OPTIMIZE: Add a getMessageDigest static helper function.
git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1333661 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user