-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (138 loc) · 4.25 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// cpass - A minimalist CLI random password generator focusing on convenience and security.
// Copyright (c) 2023 The cpass Authors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"crypto/rand"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"github.com/AlexSSD7/cpass/generator"
"github.com/pkg/errors"
"golang.org/x/exp/constraints"
)
const Version = "v0.1.0"
func askUint32(stdinReader *bufio.Reader, prompt string) (uint32, error) {
fmt.Printf("%s > ", prompt)
b, err := stdinReader.ReadBytes('\n')
if err != nil {
return 0, errors.Wrap(err, "read bytes")
}
v, err := strconv.ParseUint(strings.ReplaceAll(string(b), "\n", ""), 10, 32)
if err != nil {
return 0, errors.Wrap(err, "parse uint")
}
return uint32(v), nil
}
func askYesNo(stdinReader *bufio.Reader, prompt string) (bool, error) {
fmt.Printf("%s [y/n] > ", prompt)
b, err := stdinReader.ReadBytes('\n')
if err != nil {
return false, errors.Wrap(err, "read bytes")
}
return strings.EqualFold(string(b[0]), "y"), nil
}
func isPowerOfTwo[T constraints.Unsigned](v T) bool {
return v > 0 && (v&(v-1)) == 0
}
func main() {
fmt.Printf("cpass %v %v/%v %v. Copyright (c) 2023 The cpass Authors. Distributed under GNU GPL v3, this program comes with ABSOLUTELY NO WARRANTY.\n", Version, runtime.GOOS, runtime.GOARCH, runtime.Version())
stdinReader := bufio.NewReader(os.Stdin)
var pwLen uint32
var err error
for {
pwLen, err = askUint32(stdinReader, "Password length")
if err != nil {
fmt.Printf("Error: ask for password length: %s\n", err)
os.Exit(1)
}
if pwLen%10 == 0 || isPowerOfTwo(pwLen) {
fmt.Print("WARN: Detected a common base-ten (10, 20, etc) or power-of-two (16, 32, etc) password length. It's recommended to use something more random.\n")
yes, err := askYesNo(stdinReader, "Change password length?")
if err != nil {
fmt.Printf("Error: ask for yes/no: %s\n", err)
os.Exit(1)
}
if !yes {
fmt.Print("WARN: Going with unsafe password length.\n")
break
}
} else {
break
}
}
uppercaseCount, err := askUint32(stdinReader, "Number of uppercase characters to include (ABCDE)")
if err != nil {
fmt.Printf("Error: ask for uppercase character count: %s\n", err)
os.Exit(1)
}
digitCount, err := askUint32(stdinReader, "Number of digit characters to include (01234)")
if err != nil {
fmt.Printf("Error: ask for digit character count: %s\n", err)
os.Exit(1)
}
specialCount, err := askUint32(stdinReader, "Number of special characters to include (~!@#$)")
if err != nil {
fmt.Printf("Error: ask for special character count: %s\n", err)
os.Exit(1)
}
g, err := generator.NewGenerator(pwLen, uppercaseCount, digitCount, specialCount)
if err != nil {
fmt.Printf("Error: create password generator instance: %s\n", err)
os.Exit(1)
}
b, err := g.Generate()
if err != nil {
fmt.Printf("Error: generate password: %s\n", err)
os.Exit(1)
}
entropyMax := g.EntropyMax()
entropyMin, err := g.EntropyMin()
if err != nil {
fmt.Printf("Error: get min entropy: %s\n", err)
os.Exit(1)
}
entropyAvg := (float64(g.EntropyMax()) + float64(entropyMin)) / 2
fmt.Printf(`
Generated Password: %v
Entropy (min/realistic/max bits): %v/%v/%v (%v)
`, string(b), entropyMin, entropyAvg, entropyMax, getRatingString(entropyAvg))
// Clean up memory.
{
for i := 0; i < len(b); i++ {
b[i] = 0
}
_, _ = rand.Read(b)
}
}
func getRatingString(entropyBits float64) string {
switch {
case entropyBits <= 32:
return "Very Poor"
case entropyBits <= 48:
return "Poor"
case entropyBits <= 72:
return "Weak"
case entropyBits <= 96:
return "Good"
case entropyBits <= 120:
return "Excellent"
default:
return "Overkill"
}
}