-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildDict.go
86 lines (74 loc) · 1.58 KB
/
buildDict.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
)
var Profile = flag.String("c", "conf.ini", "build password detection Profile.")
var max = flag.Int("f", 3, "The maximum number of stitches.")
const Dictfile = "pass.txt"
var selfwriter *bufio.Writer
func main(){
flag.Parse()
Profileinfo := loadProfile(*Profile)
file, err := os.OpenFile(Dictfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("文件打开失败", err)
}
//及时关闭file句柄
defer file.Close()
selfwriter = bufio.NewWriter(file)
permutation(Profileinfo,"",min(*max,len(Profileinfo)))
//Flush将缓存的文件真正写入到文件中
selfwriter.Flush()
}
func permutation(S []string,tmp string,flag int){
if flag == 0 {
return
}
for _,v := range S {
fmt.Println(v+tmp)
selfwriter.WriteString(v+tmp+"\n")
permutation(S,v+tmp,flag-1)
}
}
func loadProfile(profile string) []string {
file, err := os.OpenFile(profile, os.O_RDWR, 0666)
if err != nil {
fmt.Println("Open ",profile," file error!", err)
return nil
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
panic(err)
}
var size = stat.Size()
fmt.Println("Profile size=", size)
var linelist []string
buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
linelist = append(linelist, line)
if err != nil {
if err == io.EOF {
fmt.Println("Profile read ok!")
break
} else {
fmt.Println("Read Profile error!", err)
return nil
}
}
}
return linelist
}
func min(a,b int)int{
if a < b{
return a
}
return b
}