-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (90 loc) · 2.08 KB
/
main.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bufio"
"fmt"
"os"
"time"
)
const PW_LENGTH = 5
// Strings are defined as a slice of runes.
// Type rune is defined as an alias for int32 (holds 4 bytes).
type password []rune
func (p password) matches(guess []rune) bool {
if len(p) != len(guess) {
return false
}
for index := 0; index < len(p); index++ {
if p[index] != guess[index] {
return false
}
}
return true
}
// Returns a slice of all usable symbols (runes).
func symbols() []rune {
symbols := make([]rune, 0)
// Lowercase letters a-z.
for index := 97; index <= 122; index++ {
symbols = append(symbols, rune(index))
}
// Uppercase letters A-Z.
for index := 65; index <= 90; index++ {
symbols = append(symbols, rune(index))
}
// Digits 0-9.
for index := 48; index <= 57; index++ {
symbols = append(symbols, rune(index))
}
return symbols
}
func guess(symbols []rune, pw password) {
guess := make([]rune, PW_LENGTH)
if !guessRecursive(symbols, pw, guess, 0) {
fmt.Println("Not found")
}
}
// Returns true if recursive logic shall abort (i.e. found match).
func guessRecursive(symbols []rune, pw password, guess []rune, index int) bool {
// Check if guess has sufficient length for testing.
if index >= PW_LENGTH {
if pw.matches(guess) {
// Found it, exit recursion.
fmt.Printf("Found it: %c\n", guess)
return true
}
} else {
// Try next symbols.
for _, symbol := range symbols {
guess[index] = symbol
if guessRecursive(symbols, pw, guess, index+1) {
return true
}
}
}
// Could not find match yet. Continue with next symbol at previous position.
return false
}
func main() {
symbols := symbols()
fmt.Printf("%c\n", symbols)
file, err := os.Open("pw.txt")
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
pw := password(line)
before := time.Now()
guess(symbols, pw)
after := time.Now()
fmt.Printf(" (took %v)\n", after.Sub(before))
}
err = scanner.Err()
check(err)
}
func check(err error) {
if err != nil {
fmt.Println("Unexpected error: ", err)
os.Exit(1)
}
}