-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshax.go
67 lines (56 loc) · 1.16 KB
/
shax.go
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
package main
import (
"bufio"
"fmt"
"os"
"sync"
"github.com/minio/sha256-simd"
)
func main() {
words, err := os.Open(os.Args[1])
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer words.Close()
hashes, err := os.Open(os.Args[2])
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer hashes.Close()
hashList := make(map[string]bool)
scanner := bufio.NewScanner(hashes)
for scanner.Scan() {
hashList[scanner.Text()] = true
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
var wg sync.WaitGroup
scanner = bufio.NewScanner(words)
var found bool
var lock sync.Mutex
for scanner.Scan() && !found {
wg.Add(1)
go func(word string) {
defer wg.Done()
hash := sha256.Sum256([]byte(word))
hashString := fmt.Sprintf("%x", hash)
lock.Lock()
if hashList[hashString] {
fmt.Println("Match found:", word, "Hash:", hashString)
delete(hashList, hashString)
if len(hashList) == 0 {
found = true
}
}
lock.Unlock()
}(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
wg.Wait()
}