-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.go
63 lines (50 loc) · 1.15 KB
/
main2.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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
)
var input = "ugkcyxxp"
func main() {
inputBuf := []byte(input)
password := make(map[int]byte)
var buf = make([]byte, 0, len(input)+9)
for i := 0; ; i++ {
buf = strconv.AppendInt(inputBuf, int64(i), 10)
md5Sum := md5.Sum(buf)
// n hex string chars means n/2 bytes
// checking like this helps to lazy call EncodeToString
if md5Sum[0]|md5Sum[1]|md5Sum[2]>>4 == 0 {
hexString := hex.EncodeToString(md5Sum[:])
position := int(hexString[5] - 48) // 48 ASCII code of '0'
// ignore wrong position
if position < 0 || position > 7 {
continue
}
// ignore already found
if _, ok := password[position]; ok {
continue
}
password[position] = hexString[6]
fmt.Fprint(os.Stderr, "\033[H\033[2J")
printPassword(os.Stderr, password)
if len(password) == 8 {
break
}
}
}
printPassword(os.Stdout, password)
}
func printPassword(w io.Writer, password map[int]byte) {
for i := 0; i < 8; i++ {
if password[i] == 0 {
_, _ = fmt.Fprint(w, "_")
} else {
_, _ = fmt.Fprint(w, string(password[i]))
}
}
_, _ = fmt.Fprintln(w)
}