-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (68 loc) · 1.31 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
package main
import (
"flag"
"fmt"
"io"
"os"
passgen "github.com/ilius/repassgen/lib"
"github.com/ilius/repassgen/xflag"
)
func printError(err error, pattern string) {
myErr, ok := err.(*passgen.Error)
if !ok {
fmt.Println(err)
return
}
fmt.Println(pattern)
fmt.Println(myErr.SpacedError())
}
func main() {
Main(os.Stdout, os.Args)
}
func Main(stdout io.Writer, args []string) {
flagSet := &flag.FlagSet{}
entropyFlag := flagSet.Bool(
"entropy",
false,
"repassgen [-entropy] PATTERN",
)
err := xflag.ParseToEnd(flagSet, args[1:])
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
calcEnropy := entropyFlag != nil && *entropyFlag
pattern := flagSet.Arg(0)
out, _, err := passgen.Generate(passgen.GenerateInput{
Pattern: []rune(pattern),
})
if err != nil {
printError(err, pattern)
os.Exit(1)
}
_, err = fmt.Fprintln(stdout, string(out.Password))
if err != nil {
panic(err)
}
if calcEnropy {
if os.Getenv("REPASSGEN_FLOAT_ENTROPY") == "true" {
_, err := fmt.Fprintf(
stdout,
"Entropy of pattern: %.2f bits\n",
out.PatternEntropy,
)
if err != nil {
panic(err)
}
} else {
_, err := fmt.Fprintf(
stdout,
"Entropy of pattern: %d bits\n",
int(out.PatternEntropy),
)
if err != nil {
panic(err)
}
}
}
}