gpt4 book ai didi

javascript - MD5 Bruteforce,从 Python 转换为 Javascript 的问题

转载 作者:行者123 更新时间:2023-11-28 18:31:56 25 4
gpt4 key购买 nike

我昨天在 python 中制作了这个函数的原型(prototype),用于暴力破解 md5 哈希值,并且它工作得非常好。在这种情况下,它将打印 Match: aa4124bc0a9335c27f086f24ba207a4912。因为这是字符串“aa”的哈希值。

import hashlib

def crack(chars, st, hsh):
if chars == 0:
if hashlib.md5(st).hexdigest() == hsh:
print "Match: " + st
print hashlib.md5(st).hexdigest()
else:
for i in range(32,127):
new = st + str(unichr(i))
crack(chars - 1, new, hsh)

crack(2, "", "4124bc0a9335c27f086f24ba207a4912")

现在我正在尝试用 JavaScript 实现它。我已经在使用 md5 库并且它工作正常。这是我编写的代码,递归未按预期工作。我将展示代码和控制台输出来进行说明。

<!DOCTYPE html>
<html lang="en">
<body>
<script src="js/md5.min.js"></script>
<script>
function crack(chars, st, hsh){
console.log(chars);
console.log(st);
if (chars == 0){
if (md5(st) == hsh){
console.log(st);
}
}
else {
for (i = 32; i <= 126; i++){
newst = st + String.fromCharCode(i);
crack(chars - 1, newst, hsh);
}
}
}

crack(2, "", "4124bc0a9335c27f086f24ba207a4912");
</script>
</body>
</html>

现在控制台输出:

2
(space ascii 32)
1
(space ascii 32)
0
(space ascii 32)
0
!
0
"
0
#
0
$
0
%
0
&
0
etc.
0
~ (ascii 126)

需要什么样的魔法才能解决这个问题?

最佳答案

你的循环迭代器i是一个全局变量。使用 varlet 将其设置为本地:

 function crack(chars, st, hsh) {
if (chars == 0) {
if (md5(st) == hsh) {
console.log(st);
}
} else {
for (var i = 32; i <= 126; i++) { // <--- Declare i with var or let
var newst = st + String.fromCharCode(i);
crack(chars - 1, newst, hsh);
}
}
}

crack(2, "", "4124bc0a9335c27f086f24ba207a4912");
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.3.0/js/md5.min.js"></script>

在递归函数调用中增加全局迭代器变量 i 也会增加调用者的值。

关于javascript - MD5 Bruteforce,从 Python 转换为 Javascript 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com