PHP 脚本来查找 MD5 是否散列到自身?

发布于 2024-10-18 06:42:28 字数 689 浏览 0 评论 0原文

这基本上是一个假设的程序 - 有 2^128 种组合(或者是 2^128-1?不确定。或者应该用 127 替换 128?),但它不起作用。

<?php

$end = (int)base_convert("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",36,10);
$count = 0;
for($count = 0; $count < $end; $count++) {
    $startHash = base_convert((string)$count,10,36);
    //Add zeros to the beginning if the length is less than 32
    while(strlen($starthash) < 32) {
        $starthash = "0" + $startHash;
    }
    $endHash = md5($startHash);
    if($startHash == $endHash) {
        file_put_contents("MD5.txt", file_get_contents("MD5.txt") + $startHash + "\n");
    }
}

?>

我什至不知道为什么;它甚至不会抛出任何错误。我期望它使用 100% 的 CPU,但不会使用超过 0.1%。

你知道发生了什么事吗?

This is basically a hypothetical program- there are 2^128 combinations (Or is it 2^128-1? Not sure. Or should the 128 be replaced with a 127?) but it doesn't work.

<?php

$end = (int)base_convert("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",36,10);
$count = 0;
for($count = 0; $count < $end; $count++) {
    $startHash = base_convert((string)$count,10,36);
    //Add zeros to the beginning if the length is less than 32
    while(strlen($starthash) < 32) {
        $starthash = "0" + $startHash;
    }
    $endHash = md5($startHash);
    if($startHash == $endHash) {
        file_put_contents("MD5.txt", file_get_contents("MD5.txt") + $startHash + "\n");
    }
}

?>

I'm not even sure why; it doesn't even throw any errors. What I'm expecting is that it uses 100% of the CPU, but it doesn't use more than 0.1%.

Do you have any idea what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑶笙 2024-10-25 06:42:28

就像迈克说的,你的外壳是错误的。但除此之外,md5 哈希实际上是一个 16 个字符的值,通常用 32 个字节表示,使其仅由字母和数字组成。在这种情况下,它总是由数字 0 到 9 和字母 A 到 F(或者是 a 到 f?)组成,因此您实际上只需要检查这些。您现在错误地假设 32 字符 md5 代码是 36 进制数字。

md5 函数有第二个参数,可让您获取 16 字节原始代码。

您可能面临的另一个问题是 PHP 速度不太快并且通常会超时。最好用不同的语言编写程序,添加一些线程,并定期保存该值,以便在关闭后可以恢复程序。

[编辑]
也许这会让您知道要等待多长时间。 md5 = 16 字节 = 4 个整数,每个整数最多 40 亿 ($FFFFFFFF)。因此,要循环遍历这 16 个字节的所有值,您需要嵌套四个 for 循环,每个循环运行 40 亿:

<?
for($a = 0; $a <= 0xFFFFFFFF; $a++){
    for($b = 0; $b <= 0xFFFFFFFF; $b++){
        for($c = 0; $d <= 0xFFFFFFFF; $c++){
            for($d = 0; $d <= 0xFFFFFFFF; $d++){
                $code = 
                    str_pad(dechex($a), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($b), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($c), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($d), 8, "0", STR_PAD_LEFT);
                $md5code = md5($code);
                if ($code == $md5code)
                {
                    echo $code . "\n"; // Found one
                }
            }
        }
    }
}
echo 'done';

Like Mike says, your casing is wrong. But besides that, an md5 hash is actually a 16 character value which is often represented in 32 bytes to let it consist of only letters and numbers. It will, in that case, always consist of numbers 0 to 9 and letters A to F (or is it a to f?), so you actually only need to check for those. You now make the wrong assumption that the 32 character md5 code is a base 36 number.

The md5 function has a second parameter that allows you to get the 16 byte raw code.

Another problem you may face is that PHP is not so fast and usually has a time-out. It may be better to write a program in a different language, add some threading, and save the value regularly, so you can resume the program after shutdown.

[edit]
Maybe this will give you an idea of how long to wait. md5 = 16 bytes = 4 integer with a maximum of 4 billion ($FFFFFFFF) each. So to loop through all values of these 16 bytes, you need to nest four for loops that each run to 4 billion:

<?
for($a = 0; $a <= 0xFFFFFFFF; $a++){
    for($b = 0; $b <= 0xFFFFFFFF; $b++){
        for($c = 0; $d <= 0xFFFFFFFF; $c++){
            for($d = 0; $d <= 0xFFFFFFFF; $d++){
                $code = 
                    str_pad(dechex($a), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($b), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($c), 8, "0", STR_PAD_LEFT).
                    str_pad(dechex($d), 8, "0", STR_PAD_LEFT);
                $md5code = md5($code);
                if ($code == $md5code)
                {
                    echo $code . "\n"; // Found one
                }
            }
        }
    }
}
echo 'done';
一梦等七年七年为一梦 2024-10-25 06:42:28

我看到的主要问题是您与 $startHash 的情况不一致。 php 中的变量区分大小写。

The main problem I see is that you aren't consistent with the case of $startHash. Variables in php are case-sensitive.

随波逐流 2024-10-25 06:42:28

您的循环变量 $count 是一个普通的 PHP 整数。它永远不会增长到 128 位。您应该使用二进制字符串或更好:

$end = gmp_init("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
$count = gmp_init("0");

for ( ; gmp_cmp($count, $end) == 0; $count=gmp_add($count, 1)) {

而且您似乎正在尝试查看哈希值的十六进制表示是否与其自身哈希值相同。所以你应该 gmp+base_convert 到 16 也许。

最后尝试 file_put_contents($fn, $data, FILE_APPEND)

但我怀疑你会得到结果。即使 CPU 使用率应该增加。

Your loop variable $count is an ordinary PHP integer. It will never grow to 128 bits. You should use a binary string or better yet:

$end = gmp_init("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
$count = gmp_init("0");

for ( ; gmp_cmp($count, $end) == 0; $count=gmp_add($count, 1)) {

And it seems you are trying to see if the hex representation of the hash hashes to itself. So you should gmp+base_convert to 16 maybe.

Finally try file_put_contents($fn, $data, FILE_APPEND)

I doubt you'll get an result however. Even if CPU usage should increase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文