gpt4 book ai didi

loops - 在嵌套 for 循环中使用并发? (蛮力)

转载 作者:IT王子 更新时间:2023-10-29 02:21:19 25 4
gpt4 key购买 nike

<分区>

我正在改编我在类里面编写的 C 程序的代码,并且我正在尝试将我用 C 编写的所有程序转换为 Go 以学习这门语言。不过,我还没有完全“获得”并发性。我如何将并发应用于嵌套的 for 循环?我程序的当前迭代很慢,比我用 C 编写的要慢得多。

这是我的代码:

package main

import (
"fmt"
"os"
"unsafe"
)

// #cgo LDFLAGS: -lcrypt
// #define _GNU_SOURCE
// #include <crypt.h>
// #include <stdlib.h>
import "C"

// credit for this solution goes to https://stackoverflow.com/questions/14109915/what-is-gos-equivalent-to-pythons-crypt-crypt
// for showing that you can wrap go in C via CGO
// crypt wraps C library crypt_r
func crypt(key, salt string) string {
data := C.struct_crypt_data{}
ckey := C.CString(key)
csalt := C.CString(salt)
out := C.GoString(C.crypt_r(ckey, csalt, &data))
C.free(unsafe.Pointer(ckey))
C.free(unsafe.Pointer(csalt))
return out
}

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ./cracker k")
return
}
cipher := os.Args[1]
var guess [5]byte
for i := 65; i < 123; i++ {
if i >= 91 && i <= 96 {

} else {
guess[0] = byte(i)
if cipher == crypt(string(guess[:1]), "50") {
fmt.Println(string(guess[:1]))
return
}
fmt.Println(string(guess[:1]))
for j := 65; j < 123; j++ {
if j >= 91 && j <= 96 {
} else {
guess[1] = byte(j)
if cipher == crypt(string(guess[:2]), "50") {
fmt.Println(string(guess[:2]))
return
}
fmt.Println(string(guess[:2]))
for k := 65; k < 123; k++ {
if k >= 91 && k <= 96 {
} else {
guess[2] = byte(k)
if cipher == crypt(string(guess[:3]), "50") {
fmt.Println(string(guess[:3]))
return
}
fmt.Println(string(guess[:3]))
for l := 65; l < 123; l++ {
if l >= 91 && l <= 96 {
} else {
guess[3] = byte(l)
if cipher == crypt(string(guess[:4]), "50") {
fmt.Println(string(guess[:4]))
return
}
fmt.Println(string(guess[:4]))
for m := 65; m < 123; m++ {
if m >= 91 && m <= 96 {
} else {
guess[4] = byte(m)
if cipher == crypt(string(guess[:5]), "50") {
fmt.Println(string(guess[:5]))
return
}
fmt.Println(string(guess[:5]))
}
}
}
}
}
}
}
}
}
}
}

该程序的目的是暴力破解 DES 哈希,它可以处理任何长度不超过 5 个字符的密码(因此,5 个嵌套 for 循环)。

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