-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoPassword.go
105 lines (93 loc) · 2.02 KB
/
noPassword.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
package main
import (
"crypto/md5"
"encoding/hex"
"math/big"
"strconv"
"os"
"fmt"
"flag"
"encoding/json"
)
var keyMap = map[int]string{
0: "0123456789",
1: "abcdefghijklmnopqrstuvwxyz",
2: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
}
type Item struct {
Uid string `json:"uid"`
Type string `json:"type"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Arg string `json:"arg"`
Autocomplete string `json:"autocomplete"`
}
type Result struct {
Items []*Item `json:"items"`
}
func main() {
argsWithoutProg := os.Args[1:]
text := ""
for _, arg := range argsWithoutProg {
text += string(arg) + " "
}
options := ParseOptions()
ln := getPassword(text)
Output(ln[0:options.Length])
}
func Output(output string) {
item := new(Item)
item.Uid = "np"
item.Type = "type"
item.Title = output
item.Subtitle = "按回车复制密码"
item.Arg = output
item.Autocomplete = "1"
result := new(Result)
result.Items = make([]*Item, 1)
result.Items[0] = item
res, _ := json.Marshal(result)
fmt.Println(string(res))
}
type Options struct {
Length int
}
func ParseOptions() *Options {
opts := new(Options)
flag.IntVar(&opts.Length, "l", 32, "Password Length")
flag.Parse()
return opts
}
func getPassword(text string) string {
nums := getMD5Hash(text)
numList := hexToIntArray(nums)
password := ""
lastIdx2 := 0
for _key, num := range numList {
if (_key < 3) {
//确保数字 小写 大写英文存在
idx1 := _key
idx2 := num % len(keyMap[idx1])
password += keyMap[idx1][idx2:idx2+1]
} else {
idx1 := _key % len(keyMap)
idx2 := (num + lastIdx2) % len(keyMap[idx1])
lastIdx2 = idx2
password += keyMap[idx1][idx2:idx2+1]
}
}
return password
}
func hexToIntArray(hexstr string) (res [32]int) {
bi := big.NewInt(0)
for key, num := range hexstr {
bi.SetString(string(num), 16)
res[key], _ = strconv.Atoi(bi.String())
}
return
}
func getMD5Hash(text string) (string) {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}