算法排列数组构建php

时间:2015-08-14 09:38:06

标签: php

我实际上坚持了一个想法。所以我想创建的是以下内容:

1)创建一个哈希算法数组,如:

$methods =  array('md5()', 'base64_encode()', 'hex2bin()');

2)循环算法排列并生成如下输出:

方法:md5> md5> md5> base64_encode> md5 =输出md5(md5(md5(base64_encode(hex2bin(md5($value))))));

的哈希值

使用的阵列位置的数量应该是随机的,也是顺序。

例如:

输出1:md5(md5($value));

输出2:md5(base64_encode(md5($value)));

等等......

我的问题如下:我已经尝试将数量的项目放在每个数组位置的末尾,就像你在代码中看到的那样。但不知何故,这是结果:http://pr0b.com/sqlx/documents/list/hashr.php

它遗憾地把括号放在每个项目上。代码如下:

<?php

    $pass = 'test';
    $array_elems_to_combine = array('md5(', 'base64_encode(', 'hex2bin(');
    $size = rand(0,10);
    $current_set = array('');

    for ($i = 0; $i < $size; $i++) {
        $tmp_set = array();
        foreach ($current_set as $curr_elem) {
            foreach ($array_elems_to_combine as $new_elem) {
                $tmp_set[] = $curr_elem . $new_elem . $pass . str_repeat(')', $size);
            }
        }
        $current_set = $tmp_set;
    }

    foreach ($current_set as $key) {
        echo($key) . '</br>';
    }

?>

1 个答案:

答案 0 :(得分:1)

怎么样

<?php

$value   = 'foobar';
$methods =  array('md5', 'base64_encode', 'sha1');

for ($k = 0; $k < 5; $k++) {
    $nb_recursions = rand(0, 5);
    $result = recurse_on_methods($methods, $nb_recursions, $value);
    echo ' = ' . $result . "\n";
}

function recurse_on_methods($methods, $recursions, $value)
{
    $method_no = rand(0, count($methods) - 1);
    $method = $methods[$method_no];

    if ($recursions > 0) {
        echo $method . ' > ';
        return $method(recurse_on_methods($methods, $recursions - 1, $value));
    } else {
        echo $method . '(' . $value . ')';
        return $method($value);
    }
}

示例输出

sha1 > base64_encode > sha1(foobar) = b1322e636ae45c163be50b28f8cb6e51debf341e
base64_encode > sha1 > md5 > sha1 > md5 > md5(foobar) = ZDBkMzY4YWI4NjRjY2FlNGRmNTAzMGM0NTg5ZmFjZjQ5MmI0MTc2YQ==
md5(foobar) = 3858f62230ac3c915f300c664312c63f
md5 > md5 > md5 > base64_encode > sha1(foobar) = 694a8dd41c13868abb9c6300ec87413a
sha1 > sha1(foobar) = 72833f1c7d3b80aadc836d5d035745ffa3a65894

这假设$methods中的函数是内同胚,可以这么说意味着它们可以按任意顺序组成。但是,在您的示例中,hex2bin(hex2bin($value))可能会失败,因为hex2bin的输出不一定是十六进制值。

关于评论的修改:如果您正在寻找返回f_1(f_2(...(f_N($value))...))的作文$hash,那么您可以执行以下操作。首先定义一个函数,该函数生成固定长度N的所有此类组合:

function recurse_on_methods($methods, $N, $value)
{
    if ($N <= 0) {
        yield [$value, 'id'];
    } else {
        foreach ($methods as $method) {
            $recurse = recurse_on_methods($methods, $N - 1, $value);

            foreach ($recurse as $r) {
                yield [$method($r[0]), $method . ' > ' . $r[1]];
            }
        }
    }
}

然后迭代N(组合的长度)所需的值范围,并在结果中查找特定的哈希值:

$hash = sha1(md5(sha1(sha1($value))));
echo 'Looking for a composition that yields ' . $hash . "\n";

for ($N = 1; $N <= 5; $N++) {
    $results = recurse_on_methods(['md5', 'sha1'], $N, $value);
    foreach ($results as $r) {
        if ($r[0] == $hash) {
            echo $r[1] . '(' . $value . '): ' . $r[0] . "\n";
        }
    }
}

输出:

Looking for a composition that yields 93fe1beeef1c02a4162d47f387728a8c9e8633fd
sha1 > md5 > sha1 > sha1 > id(foobar): 93fe1beeef1c02a4162d47f387728a8c9e8633fd