Golang program to check a string list of patterns in another string list – password cracking

huangapple go评论113阅读模式
英文:

Golang program to check a string list of patterns in another string list - password cracking

问题

社区,我需要改进以下代码的建议:

为了解决问题,该程序执行以下操作:

  • 它读取一个包含sha2哈希的文件到一个字符串数组中。
  • 它接受一个起始数字并连续计算哈希值。
  • 它将计算的哈希与文件中的哈希(列表)进行比较,并打印匹配项。

以下是代码:

//FILENAME: passcracker.go
package main

import (
    "fmt"
    "crypto/sha256"
    "os"
    "io/ioutil"
    "strings"
    "math/big"    
)

func isValueInList(value1 string, list []string) bool {
    for _, v := range list {
        if v == value1 {
            return true
        }
    }
    return false
}

func main() {
    if len(os.Args) <= 2 {
        fmt.Printf("USAGE : %s <PATTERNFILE> <STARTING_NUMBER>\n", os.Args[0])
        os.Exit(0)
    }

    fileName := os.Args[1]
    fileBytes, err := ioutil.ReadFile(fileName)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    
    strHashArr := strings.Split(string(fileBytes), "\r\n")

    startNum := new(big.Int)
    startNum.SetString(os.Args[2], 10)
    one := big.NewInt(1)

    var i int64
    for i = 0; i < 262144; i++ {
        h := sha256.New()
        h.Write([]byte(startNum.Bytes()))

        s := fmt.Sprintf("%x", h.Sum(nil))

        if isValueInList(s, strHashArr) {
            fmt.Printf("Matched Hash %s for number %s\n",s,startNum)
        }
        startNum = startNum.Add(startNum,one)

    }
}

密码哈希文件位于:https://pastebin.com/TWPxrb4R

要运行程序,请使用以下命令:

passcracker hashes.txt 1000

该程序将打印匹配的哈希值以及识别出的数字。

由于该程序只计算有限的262144个哈希值,因此打印速度较快。

现在,为了改进程序以更快地输出匹配项,是否可能将哈希计算到一个字符串数组中,并调用一个函数来匹配文件中的哈希值,并以单个调用返回匹配的索引?

由于该问题与密码破解方法非常相似,但这里的区别在于打印连续计算和匹配的哈希值。这有点像连续运行的用户ID。

由于输入的哈希文件可能非常大(大约有几千个哈希值),连续的数字也可能很大,即使对于包含10,000个哈希值的哈希文件,该程序也会遇到困难。

目前为了简洁起见,上述文件中的哈希数量为50,循环检查256K个数字,执行速度更快。

非常感谢您的帮助。谢谢。

英文:

Community, I need suggestions on improving the following code:

To give the problem, the program does the following:

  • It reads a file containing sha2 hashes into a string array.
  • It accepts a starting number and computes the hash values consecutively.
  • It checks for the computed hash with the hashes from the file (list) and prints the matches.
//FILENAME: passcracker.go
package main
import (
&quot;fmt&quot;
&quot;crypto/sha256&quot;
&quot;os&quot;
&quot;io/ioutil&quot;
&quot;strings&quot;
&quot;math/big&quot;	
)
func isValueInList(value1 string, list []string) bool {
for _, v := range list {
if v == value1 {
return true
}
}
return false
}
func main() {
if len(os.Args) &lt;= 2 {
fmt.Printf(&quot;USAGE : %s &lt;PATTERNFILE&gt; &lt;STARTING_NUMBER&gt;\n&quot;, os.Args[0])
os.Exit(0)
}
fileName := os.Args[1]
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
strHashArr := strings.Split(string(fileBytes), &quot;\r\n&quot;)
startNum := new(big.Int)
startNum.SetString(os.Args[2], 10)
one := big.NewInt(1)
//list := []string{ }
var i int64
for i = 0; i &lt; 262144; i++ {
h := sha256.New()
h.Write([]byte(startNum.Bytes()))
s := fmt.Sprintf(&quot;%x&quot;, h.Sum(nil))
// Hash values are computed and added to a string list - A probable approach
//list.append(s)
if isValueInList(s, strHashArr) {
fmt.Printf(&quot;Matched Hash %s for number %s\n&quot;,s,startNum)
}
startNum = startNum.Add(startNum,one)
}
// Probable approach to reduce the time
// Computed hash string list is checked with the file hashes list
// Function takes to string arrays 
// Can it also use map or any other method for list in list comparison?
//if isValueInList(list, strHashArr) {
// get all the matched items and print the index value using the startNum value in a loop
//}
}

The password hash file is at: https://pastebin.com/TWPxrb4R

To run the program, use

passcracker hashes.txt 1000

The program prints the matching hashes along with the identified number.

Since the program only computes a limited 262144 hashes, it will print faster.

Now to improve the program to output the matches faster, is it possible to compute the hashes into a string array and call a function to match the hashes from the file and return the matched indexes in a single call?

Since the problem is very similar to password cracking approach, but here the difference is to print the sequentially computed and matched hash value. It is something like a running user id which is consecutive.

Since the input hash file could get really big (in the tune of a couple of thousand hashes) and consecutive numbers could also be a large one, the program struggles even if the computation is just for 32K loop for a hash file containing a 10K hashes.

Currently for the sake of brevity, the number of hashes in the above file is 50 and loop checks for 256K numbers which executes faster.

Some help would be really appreciated. Thank you.

答案1

得分: 2

我能够将速度提高三倍:

package main
import (
"bufio"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"math/bits"
"os"
"testing"
)
func BenchmarkFast(b *testing.B) {
f, err := os.Open("hashes.txt")
if err != nil {
b.Fatal(err)
}
defer f.Close()
s := bufio.NewScanner(f)
hash := make(map[[32]byte]struct{})
for s.Scan() {
var dst [32]byte
hex.Decode(dst[:], s.Bytes())
hash[dst] = struct{}{}
}
var num uint64
for n := 0; n < b.N; n++ {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, num)
buf = buf[bits.LeadingZeros64(num)>>3:]
sum := sha256.Sum256(buf)
if _, ok := hash[sum]; ok {
}
num++
}
}

结果:

BenchmarkFast-12         5983708               198.2 ns/op
BenchmarkSlow-12         1946358               614.7 ns/op
英文:

I was able to triple the speed:

package main
import (
&quot;bufio&quot;
&quot;crypto/sha256&quot;
&quot;encoding/binary&quot;
&quot;encoding/hex&quot;
&quot;math/bits&quot;
&quot;os&quot;
&quot;testing&quot;
)
func BenchmarkFast(b *testing.B) {
f, err := os.Open(&quot;hashes.txt&quot;)
if err != nil {
b.Fatal(err)
}
defer f.Close()
s := bufio.NewScanner(f)
hash := make(map[[32]byte]struct{})
for s.Scan() {
var dst [32]byte
hex.Decode(dst[:], s.Bytes())
hash[dst] = struct{}{}
}
var num uint64
for n := 0; n &lt; b.N; n++ {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, num)
buf = buf[bits.LeadingZeros64(num)&gt;&gt;3:]
sum := sha256.Sum256(buf)
if _, ok := hash[sum]; ok {
}
num++
}
}

Result:

BenchmarkFast-12         5983708               198.2 ns/op
BenchmarkSlow-12         1946358               614.7 ns/op

huangapple
  • 本文由 发表于 2021年7月3日 01:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/68229520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定