算法排列数组构建php


algorithm permutations array building 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>';
    }
?>

<?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的输出不一定是十六进制值。


编辑关于您的评论:如果您正在寻找返回$hash的组合f_1(f_2(...(f_N($value))...)),那么您可以执行以下操作。首先定义一个函数,生成所有固定长度的组合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

如何:

平原:

php > echo base64_encode(md5("a value"));
YTIxM2RmNDA5YzcwNGY2ZWZkOTY4MTEyMDZmODk0ZTI=

幻想:

php > echo array_reduce(['md5','base64_encode'] // add as much as you like
                        ,function($val,$fn){ return $fn($val); }
                        ,"a value");
YTIxM2RmNDA5YzcwNGY2ZWZkOTY4MTEyMDZmODk0ZTI=

编辑:这只是部分解决方案,剩下的是创建一个所需的fn排列数组