私はMD5ハッシュ値をとり、可能なすべての文字列の組み合わせを並べ替えて入力/元の値を見つける関数を書いています。文字列の順列を生成するランタイムを減らすにはどうすればよいですか?
...非常にリソース集約的で実用的で非倫理的な無差別なハッキングのような試みをせずにMD5を解読することはできません。
私はこれをよく知ってる、しかし、私は、文字列の置換関数を実装するために、このシナリオを使用しています。私はまた、他のものとは対照的に再帰的方法論に固執したいと思う。これを行うための最良の要約はおそらく、マーク・バイヤースポストによって要約される。
- Try each of the letters in turn as the first letter and then find all
the permutations of the remaining letters using a recursive call.
- The base case is when the input is an empty string the only permutation is the empty string.
(Generating all permutations of a given string)は
とにかく、私は次のようにこれを実装しました:
function matchMD5($possibleChars, $md5, $concat, $length) {
for($i = 0; $i < strlen($possibleChars); $i++) {
$ch = $possibleChars[$i];
$concatSubstr = $concat.$ch;
if(strlen($concatSubstr) != $length) {
matchMD5($possibleChars, $md5, $concatSubstr, $length);
}
else if(strlen($concatSubstr) == $length) {
$tryHash = hash('md5', $concatSubstr);
if ($tryHash == $md5) {
echo "Match! $concatSubstr ";
return $concatSubstr;
}
}
}
}
作品100%、しかし、私が4文字の配列を渡すと、サーバは10.7秒で実行され、一致がすべての可能な順列の約1/10になる一致を生成します。私の有効な文字での機能の並べ替える、$ possibleChars、と呼ばれる、すべての英数字に加えて、いくつかの選択punctionationsが含まれています
.,;:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
質問:上記のコードは速く何とか実行に書き込むことができますか?
この質問はhttps://codereview.stackexchange.comに属します – naomik