- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试模仿 Drupal 7 在 Java 中检查正确密码的方式。在这里找到一些代码作为指南:https://github.com/CraftFire/AuthDB-Legacy/blob/master/src/main/java/com/authdb/scripts/cms/Drupal.java 并提取了我需要的代码。
然而,当我提供密码和散列版本时(为了提取盐和所需的迭代量),我得到了不同的结果。
密码是使用 Drupal 密码哈希脚本生成的结果:
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();
}
}
--附言。我已经看到的一件事是:考虑这些函数:
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'));没有帮助我...谢谢!
最佳答案
我建议你这样做:
import java.security.NoSuchAlgorithmException;
public class hash {
private static final int DRUPAL_HASH_LENGTH = 55;
private static String _password_itoa64() {
return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}
public static void main(String args[]) throws Exception {
// Passwords and hashes generated by Drupal.
checkPassword("adrian", "$S$DNbBTrkalsPChLsqajHUQS18pBBxzSTQW0310SzivTy7HDQ.zgyG");
checkPassword("test" , "$S$DxVn7wubSRzoK9X2pkGx4njeDRkLEgdqPphc2ZXkkb8Viy8JEGf3");
checkPassword("barbaz", "$S$DOASeKfBzZoqgSRl/mBnK06GlLESyMHZ81jyUueEBiCrkkxxArpR");
}
private static int password_get_count_log2(String setting) {
return _password_itoa64().indexOf(setting.charAt(3));
}
private static byte[] sha512(String input) {
try {
return java.security.MessageDigest.getInstance("SHA-512").digest(input.getBytes());
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return new byte[0];
}
private static byte[] sha512(byte[] input) {
try {
return java.security.MessageDigest.getInstance("SHA-512").digest(input);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return new byte[0];
}
/**
* 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) throws Exception {
if (candidate == null || saltedEncryptedPassword == null) {
return false;
}
String hash = password_crypt(candidate, saltedEncryptedPassword);
System.out.println("Expected value = " + saltedEncryptedPassword);
System.out.println("Calced value = " + hash);
System.out.println("Result Good? = " + saltedEncryptedPassword.equalsIgnoreCase(hash));
return saltedEncryptedPassword.equalsIgnoreCase(hash);
}
private static String password_crypt(String password, String passwordHash) throws Exception {
// The first 12 characters of an existing hash are its setting string.
passwordHash = passwordHash.substring(0, 12);
int count_log2 = password_get_count_log2(passwordHash);
String salt = passwordHash.substring(4, 12);
// Hashes must have an 8 character salt.
if (salt.length() != 8) {
return null;
}
int count = 1 << count_log2;
byte[] hash;
try {
hash = sha512(salt.concat(password));
do {
hash = sha512(joinBytes(hash, password.getBytes("UTF-8")));
} while (--count > 0);
} catch (Exception e) {
System.out.println("error " + e.toString());
return null;
}
String output = passwordHash + _password_base64_encode(hash, hash.length);
return (output.length() > 0) ? output.substring(0, DRUPAL_HASH_LENGTH) : null;
}
private static byte[] joinBytes(byte[] a, byte[] b) {
byte[] combined = new byte[a.length + b.length];
System.arraycopy(a, 0, combined, 0, a.length);
System.arraycopy(b, 0, combined, a.length, b.length);
return combined;
}
private static String _password_base64_encode(byte[] input, int count) throws Exception {
StringBuffer output = new StringBuffer();
int i = 0;
CharSequence itoa64 = _password_itoa64();
do {
long value = SignedByteToUnsignedLong(input[i++]);
output.append(itoa64.charAt((int) value & 0x3f));
if (i < count) {
value |= SignedByteToUnsignedLong(input[i]) << 8;
}
output.append(itoa64.charAt((int) (value >> 6) & 0x3f));
if (i++ >= count) {
break;
}
if (i < count) {
value |= SignedByteToUnsignedLong(input[i]) << 16;
}
output.append(itoa64.charAt((int) (value >> 12) & 0x3f));
if (i++ >= count) {
break;
}
output.append(itoa64.charAt((int) (value >> 18) & 0x3f));
} while (i < count);
return output.toString();
}
public static long SignedByteToUnsignedLong(byte b) {
return b & 0xFF;
}
}
关于Drupal 密码的 Java 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736555/
Internal Server Error: /admin/account/customuser/add/ Traceback (most recent call last): File "C:\
有问题!虽然我发现几乎相似的线程但没有帮助:( 我编写了一个 php 脚本来从我的 MySQL 数据库中获取注册用户的数量。该脚本在我的本地主机上运行良好;它使用给定的用户名、密码和主机名,分别是“r
我正在做一项基于密码的作业,我将 key 和消息放在单独的数组中。我想创建第三个数组,其中包含围绕消息大小的 key ,如下所示: message keykeyk 我已经在这个问题上苦苦挣扎了一段时间
我的几个客户要求我实现图形密码检查器,例如 关于如何实现这种 UI 有什么想法吗? 最佳答案 试着看看这个:https://code.google.com/p/android-lockpattern/
我正在使用 MAMP,每次登录 phpMyAdmin 时,都会收到以下错误/警告消息: the configuration file now needs a secret passphrase (bl
我正在尝试通过将 Visual Studio 2013 连接到我的测试机来调试 WDF 驱动程序。它创建一个名为 WDKRemoteUser 的用户,并在进行测试时尝试自动登录。有人知道这个用户的密码
使用具有指定用户名和密码的 SVN 提交。我希望服务器抛出错误;所以我可以告诉我的用户他/她的密码错误。 相反,在使用错误密码提交后: svn commit "test_file.txt" --use
我正在尝试实现 friend 推荐。 它从节点“你”开始。而且,我想找到节点“安娜”。 换句话说,这是我的两个或更多 friend 共同认识的人。上面的示例节点是 Anna。 如果您的帮助,我将不胜感
我都尝试过 wget --user=myuser --password=mypassword myfile 和 wget --ftp-user=myuser --ftp-password=mypass
我的一位用户提示说,每当他尝试使用默认管理界面(Django 的管理员)添加新用户(auth.User)时,新用户名和密码都会自动填充他自己的。 问题是他在登录时要求 Firefox 记住他的用户名/
我们正在开发一款应用(当然)用于应用购买 (IAP)。我已完成指南中的所有操作以启用 iap,并且一切正常,直到我想要购买为止。 部分代码: MainViewController.m -(vo
我试图创建两个可选匹配项的并集(如下所示),但我得到的不是并集,而是两者的交集。我应该如何更改此查询以获得所需的联合? optional match (a:PA)-[r2:network*2]-(b:
我想将Ansible用作另一个Python软件的一部分。在该软件中,我有一个包含其用户名/密码的主机列表。 有没有一种方法可以将SSH连接的用户/密码传递给Ansible ad-hoc命令或以加密方式
嗨,我在使用xampp的Apache Web服务器上收到错误500。直到我使用.htaccess,.htpasswd文件,错误才出现。我搜索了,但找不到语法错误。我只有1张图片和要保护的索引文件。以下
我一直使用它来编辑用户帐户信息: $this->validate($request, [ 'password' => 'min:6', 'password_confirmation'
我需要使用InstallUtil来安装C# Windows服务。我需要设置服务登录凭据(用户名和密码)。这一切都需要默默地完成。 有没有办法做这样的事情: installutil.exe myserv
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
如果我有一个随机的、16 个字符长的字母数字盐(不同大小写),它是为每个用户生成和存储的,我是否还需要一个站点范围的盐? 换句话说,这样好吗? sha1($user_salt . $password)
我正在开发一个空白程序,该程序将允许用户创建一个帐户,以便他们可以存储其余额和提款/存款。用户输入用户名和密码后,如何存储这些信息以便用户可以登录并查看其余额?我不一定要尝试使其非常安全,我只是希望能
我正在尝试寻找一种通用方法来搜索没有链接到另一个节点或节点集的节点或节点集。例如,我能够找到特定类型(例如 :Style)的所有节点,这些节点以某种方式连接到一组特定的节点(例如 :MetadataR
我是一名优秀的程序员,十分优秀!