-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.split.multithreaded
93 lines (82 loc) · 2.22 KB
/
main.split.multithreaded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"strconv"
"sync"
"time"
)
/*
Requires SHA2 with 256 bytes 64 rounds
https://gchq.github.io/CyberChef/#recipe=SHA2('256',64,160)&input=SlVTVElOMTk5Mw
JUSTIN1993 : 96306567b5683a463820ce53dd484eccc57c53aaa3c6d5f74018da1b8ef99815
*/
var checkHash string = "96306567b5683a463820ce53dd484eccc57c53aaa3c6d5f74018da1b8ef99815"
func hashString(inputString string) string {
hashObject := sha256.New()
hashObject.Write([]byte(inputString))
calculatedHash := hashObject.Sum(nil)
return hex.EncodeToString(calculatedHash)
}
// count lines in file to split between workers
func countLines(filename string) int {
fileHandle, err := os.Open(filename)
if err != nil {
panic(err)
}
defer fileHandle.Close()
counterInt := 0
scanner := bufio.NewScanner(fileHandle)
for scanner.Scan() {
counterInt++
}
return counterInt
}
func crackRange(minline int, maxline int, filename string, wg *sync.WaitGroup) {
defer wg.Done()
fileHandle, err := os.Open(filename)
if err != nil {
panic(err)
}
defer fileHandle.Close()
counterInt := 0
scanner := bufio.NewScanner(fileHandle)
for scanner.Scan() {
if counterInt >= minline {
line := scanner.Text()
hashedEntry := hashString(line)
if hashedEntry == checkHash {
foundLineNum := strconv.Itoa(counterInt + 1)
fmt.Println("Found Password on line " + "(" + foundLineNum + "): " + line)
return
}
if counterInt >= maxline {
return
}
}
counterInt++
}
}
func main() {
startTime := time.Now()
fmt.Println("Trying Hash: " + checkHash)
//count total lines and split between worker jobs
totalCount := countLines("rockyou-odd.txt")
halfCount := totalCount / 2
fmt.Println("File line count: " + strconv.Itoa(halfCount))
fmt.Println("File half line count: " + strconv.Itoa(halfCount))
//use a WaitGroup to ensure all goroutines complete before ending the program
var wg sync.WaitGroup
wg.Add(2)
go crackRange(0, halfCount, "rockyou-odd.txt", &wg)
go crackRange(halfCount, totalCount, "rockyou-odd.txt", &wg)
//wait for all goroutines to complete
wg.Wait()
//print out the nanoseconds spent
duration := time.Since(startTime)
fmt.Println("Runtime Nanoseconds: ")
fmt.Println(duration.Nanoseconds())
}