我试图在Java中模拟Drupal 7检查正确密码的方式。在这里找到了一些代码作为指南:https://github.com/CraftFire/AuthDB-Legacy/blob/master/src/main/java/com/authdb/scripts/cms/Drupal.java,并提取了所需的代码。
然而问题在于,当我提供密码和散列版本(以提取所需的盐和迭代次数)时,我得到了不同的结果。
这些密码是使用Drupal password-hash脚本生成的,结果如下:
如您所见,这几乎是相同的...
有人知道如何修复最后一部分吗? 表单 new String(SHA512(salt + password,'Whatevercodec')) 并没有帮助我... 谢谢!
然而问题在于,当我提供密码和散列版本(以提取所需的盐和迭代次数)时,我得到了不同的结果。
这些密码是使用Drupal password-hash脚本生成的,结果如下:
Expected value = $S$DxVn7wubSRzoK9X2pkGx4njeDRkLEgdqPphc2ZXkkb8Viy8JEGf3
Calculated value = $S$DxVn7wubSpQ1CpUnBZZHNqIXMp2XMVZHMYBqAs24NsUHMY7HBkYn
Expected value = $S$DOASeKfBzZoqgSRl/mBnK06GlLESyMHZ81jyUueEBiCrkkxxArpR
Calculated value = $S$DOASeKfBzs.XMVZ1NkYXNmIqMpEHAoEaMYJ1NmUHCZJaBZFnAZFX
请问有人能帮我,并告诉我这里我做错了什么吗? 谢谢。
代码:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class test {
public static void main(String args[]) {
// Passwords and hashes generated by Drupal.
checkPassword("test" , "$S$DxVn7wubSRzoK9X2pkGx4njeDRkLEgdqPphc2ZXkkb8Viy8JEGf3");
checkPassword("barbaz", "$S$DOASeKfBzZoqgSRl/mBnK06GlLESyMHZ81jyUueEBiCrkkxxArpR");
}
private static String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final int DRUPAL_HASH_LENGTH = 55;
private static int password_get_count_log2(String setting) { return itoa64.indexOf(setting.charAt(3)); }
/**
* Note: taken from the default Drupal 7 password algorithm
* @param candidate
* the clear text password
* @param saltedEncryptedPassword
* the salted encrypted password string to check => NEEDS TO BE THE DEFAULT DRUPAL 7 PASSWORD HASH.
* @return true if the candidate matches, false otherwise.
*/
public static boolean checkPassword(String candidate, String saltedEncryptedPassword) {
if (candidate == null) {
return false;
}
if (saltedEncryptedPassword == null) {
return false;
}
String hash = password_crypt(candidate, saltedEncryptedPassword);
System.out.println("Tested value = " + saltedEncryptedPassword);
System.out.println("Calced value = " + hash);
return hash == saltedEncryptedPassword;
}
public static String SHA512(String text) {
byte[] sha1hash = new byte[40];
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(text.getBytes("UTF-8"), 0, text.length());
sha1hash = md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return convertToHex(sha1hash);
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
}
while(two_halfs++ < 1);
}
return buf.toString();
}
private static String password_crypt(String password, String setting) {
// The first 12 characters of an existing hash are its setting string.
setting = setting.substring(0, 12);
int count_log2 = password_get_count_log2(setting);
String salt = setting.substring(4, 12);
// Hashes must have an 8 character salt.
if (salt.length() != 8) {
return null;
}
// Convert the base 2 logarithm into an integer.
int count = 1 << count_log2;
String hash;
try {
hash = SHA512(salt + password);
do {
hash = SHA512(hash + password);
} while (--count >= 0);
} catch(Exception e) {
return null;
}
int len = hash.length();
String output = setting + password_base64_encode(hash, len);
return (output.length() > 0) ? output.substring(0, DRUPAL_HASH_LENGTH) : null;
}
private static String password_base64_encode(String input, int count) {
StringBuffer output = new StringBuffer();
int i = 0, value;
do {
value = input.charAt(i++);
output.append(itoa64.charAt(value & 0x3f));
if (i < count) {
value |= input.charAt(i) << 8;
}
output.append(itoa64.charAt((value >> 6) & 0x3f));
if (i++ >= count) {
break;
}
if (i < count) {
value |= input.charAt(i) << 16;
}
output.append(itoa64.charAt((value >> 12) & 0x3f));
if (i++ >= count) {
break;
}
output.append(itoa64.charAt((value >> 18) & 0x3f));
} while (i < count);
return output.toString();
}
}
我是一位有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:
-- ps. 我已经看到的一件事是以下内容: 考虑下面这些函数:
public String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
}
while(two_halfs++ < 1);
}
return buf.toString();
}
并且
public String convertToHex(byte [] raw) {
StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
int hiVal = (b & 0xF0) >> 4;
int loVal = b & 0x0F;
hex.append((char) ('0' + (hiVal + (hiVal / 10 * 7))));
hex.append((char) ('0' + (loVal + (loVal / 10 * 7))));
}
return hex.toString();
}
第一个函数返回小写字符串,第二个函数返回大写字符串。不知道该使用哪个,两者最终返回的结果都不令人满意。
-编辑-
接近成功了?...
又向前迈进了一步,问题有所变化... 在Drupal中使用以下函数:
$hash = hash($algo, $salt . $password, TRUE);
返回的是
'���Y�emb
ӈ3����4��q����h�osab��V�!IS�uC�*[�
正如您所看到的,我们不需要十六进制版本,因为您将得到完全不同的哈希返回值... 因此,我改变了Java中的代码:
public byte[] SHA512(String text) {
byte[] sha1hash = new byte[50];
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(text.getBytes("UTF-8"), 0, text.length());
sha1hash = md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return sha1hash;
}
--snip--
hash = new String(SHA512(salt + password));
System.out.println(hash);
返回:
'���Y�emb
ӈ3����4��q���h�osab��V�!IS�uC�*[�
如您所见,这几乎是相同的...
php: ӈ3����4��q����h�osab��V�!IS�uC�*[�
java: ӈ3����4��q���h�osab��V�!IS�uC�*[�
有人知道如何修复最后一部分吗? 表单 new String(SHA512(salt + password,'Whatevercodec')) 并没有帮助我... 谢谢!