-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgopwgen.go
161 lines (142 loc) · 3.93 KB
/
gopwgen.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
/*
* Sal's Random Password Generator (GoPwgen)
* --------------------------------
* My very first Go application.
* It does what it says above.
* --------------------------------
* Copyright (c) 2015-2024, Salvatore LaMendola <[email protected]>
* All rights reserved.
*/
package main
import (
"bytes"
"crypto/rand"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"text/tabwriter"
"github.com/sethvargo/go-diceware/diceware"
)
// Assign all the acceptable arguments and their default values
var (
flagSymbols = flag.Bool("s", false, "Alphanumeric + symbols (NOT FOR MYSQL!)")
flagAlpha = flag.Bool("a", false, "Alphanumeric only")
flagDiceware = flag.Bool("d", false, "Diceware passphrase (Choose random words from a list)")
flagHexadecimal = flag.Bool("H", false, "Hexadecimal only (abcdef0123456789)")
flagPhpMyAdmin = flag.Bool("p", false, "Generate phpMyAdmin Blowfish secret (for cookie auth)")
flagWordPress = flag.Bool("w", false, "Generate WordPress encryption salts for use in wp-config.php")
flagVersion = flag.Bool("v", false, "Print the version number and exit")
phpKeys = [...]string{
"AUTH_KEY",
"SECURE_AUTH_KEY",
"LOGGED_IN_KEY",
"NONCE_KEY",
"AUTH_SALT",
"SECURE_AUTH_SALT",
"LOGGED_IN_SALT",
"NONCE_SALT",
}
)
// Alphanumeric values and symbols+alpha / default length and number of passwords
const alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
const symbols = alphanumeric + "-_!@#$%^&*/\\()_+{}|:<>?="
const hexadecimal = "abcdef0123456789"
const defaultlen = 19
const defaultnum = 1
const defaultDwLen = 6
const version = "2.3.0"
func myUsage() {
fmt.Printf("Usage: %s [OPTION] [length | num dice words] [num pwds/phrases]\n\nOptions:\n", os.Args[0])
flag.PrintDefaults()
}
func myVersion() {
fmt.Printf("GoPwgen version %s\n", version)
}
func diceWareGen(dwlen int) {
list, err := diceware.Generate(dwlen)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", strings.Join(list, "-"))
}
func main() {
flag.Usage = myUsage
flag.Parse()
// Either set len/num using provided values or fallback to defaults
allowed := symbols
pwlen, err1 := strconv.Atoi(flag.Arg(0))
dwlen := pwlen
if err1 != nil {
pwlen = defaultlen
dwlen = defaultDwLen
}
numPws, err2 := strconv.Atoi(flag.Arg(1))
if err2 != nil {
numPws = defaultnum
}
pwStringer := func(n int, s string) string { return string(pwgen(n, s)) }
// Option validation
switch {
case *flagVersion:
myVersion()
os.Exit(0)
case *flagSymbols: // This is the default assigned value
case *flagAlpha:
allowed = alphanumeric
case *flagDiceware:
for i := 0; i < numPws; i++ {
diceWareGen(dwlen)
}
os.Exit(0)
case *flagHexadecimal:
allowed = hexadecimal
case *flagPhpMyAdmin:
pwlen = 64
case *flagWordPress:
pwlen = 64
w := new(tabwriter.Writer)
var b bytes.Buffer
w.Init(&b, 26, 1, 0, ' ', 0)
pwStringer = phpKeysPwgen(w, &b)
}
outputs := make([]string, numPws)
for i := 0; i < numPws; i++ {
outputs[i] = pwStringer(pwlen, allowed)
}
fmt.Println(strings.Join(outputs, "\n"))
}
func pwgen(length int, allowedChars string) []byte {
// Create the password string and associated randomness
password := make([]byte, length)
entropy := make([]byte, length+(length/4))
allowedLength := len(allowedChars)
// Generate password of the requested length
_, err := io.ReadFull(rand.Reader, entropy)
if err != nil {
fmt.Println("Error: ", err)
}
for j := 0; j < length; j++ {
password[j] = allowedChars[entropy[j]%byte(allowedLength)]
}
return password
}
type flushableWriter interface {
io.Writer
Flush() error
}
func phpKeysPwgen(w flushableWriter, b *bytes.Buffer) func(int, string) string {
return func(n int, s string) string {
lines := make([]string, len(phpKeys)+1)
for i, key := range phpKeys {
fmt.Fprintf(w, "define('%s',\t'%s');", key, string(pwgen(n, s)))
w.Flush()
lines[i] = b.String()
b.Reset()
}
return strings.Join(lines, "\n")
}
}