简体   繁体   English

java md5 hash, php函数检查

[英]java md5 hash, php function to check

so here is a java function that already exists所以这是一个已经存在的java函数

StringBuffer sbTemp = new StringBuffer();
byte[] btTypeInPasswd = this.md.digest(this.password.getBytes());

for (int i = 0; i < btTypeInPasswd.length; i++)
  sbTemp = sbTemp.append(btTypeInPasswd[i]);

this.password = sbTemp.toString();

this.password would then contain a string like follows if the password was test: -8774-113-27-52-79-101-9028768115-45-111-23-121-10447-69-45如果密码为 test,则 this.password 将包含如下字符串:-8774-113-27-52-79-101-9028768115-45-111-23-121-10447-69-45

What I need is a PHP function to produce the same result, can this be done?我需要的是一个 PHP 函数来产生相同的结果,这可以做到吗?

I can NOT edit the java .class file as this is part of an existing application I wish to work along-side by accessing it's existing database.无法编辑 java .class 文件,因为它是现有应用程序的一部分,我希望通过访问它的现有数据库来协同工作。

This is what I tried.这是我尝试过的。

$bytes = md5("test", true);
$bArray = array();

for ($x = 0; $x < strlen($bytes); $x++)
    $bArray[$x] = ord(substr($bytes, $x, 1));

print "<pre>" . implode('-', $numbers) . "</pre>";

However using the same password "test" it produces: 9-143-107-205-70-33-211-115-202-222-78-131-38-39-180-246 not at all like -8774-113-27-52-79-101-9028768115-45-111-23-121-10447-69-45但是使用相同的密码“测试”它会产生:9-143-107-205-70-33-211-115-202-222-78-131-38-39-180-246 一点也不像 -8774-113 -27-52-79-101-9028768115-45-111-23-121-10447-69-45

Thanks.谢谢。

So no thanks to trolls, if anyone else needs to know how to do this here is how.所以不用感谢巨魔,如果其他人需要知道如何做到这一点,这里是如何做到的。

function md5_hash($password)
{
    $hashArray = array();
    $hash = md5($password);

    for ($x = 0; $x < strlen($hash); $x += 2)
        $hashArray[$x] = (($dec = hexdec(substr($hash, $x, 2))) > 127 ? $dec - 256 : $dec);

    return implode("", $hashArray);
}

Using the password "test" will result in the above function returning a string: 9-113107-517033-45115-54-3478-1253839-76-10使用密码“test”将导致上述函数返回一个字符串:9-113107-517033-45115-54-3478-1253839-76-10

在一行上面的md5_hash函数,您可以执行以下操作:

$password  = implode("", unpack('c*', md5($password,true)));

PHP has a built-in md5() function that given a string returns a 32 character hexadecimal digest, eg: PHP 有一个内置的md5()函数,它给定一个字符串返回一个 32 个字符的十六进制摘要,例如:

<? echo md5('test'); ?>

gives:给出:

098f6bcd4621d373cade4e832627b4f6

So if you fix your Java code to output hex instead of decimal, the results should match.因此,如果您修复 Java 代码以输出十六进制而不是十进制,结果应该匹配。

$md5 = md5('...... your string here ....');
$numbers = array();
for ($i = 0; $i < strlen($md5); $i++) {
   $numbers[i] = ord(substr($md5, $i, 1));
}

echo implode('-', $numbers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM