-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
196 lines (185 loc) · 5.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// becrypt - Generate and check bcrypt hashes from a CLI
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"golang.org/x/crypto/bcrypt"
"golang.org/x/term"
)
const (
version = "1.4.3"
pwMaxLen = 72
)
var (
self = ""
quiet = false
)
func showHelp(msg string) { // I:self,version,pwMaxLen
helptxt := self + " v" + version + ` - Generate and check bcrypt hashes from the CLI
Repo: github.com/pepa65/becrypt
Usage: ` + self + ` OPTION
Options:
help|-h|--help Display this HELP text.
cost|-c|--cost <hash> Display the COST of bcrypt <hash>.
<hash> [-q|--quiet] CHECK the password(^) against bcrypt <hash>.
[<cost>] Generate a HASH from the given password(^).
(Optional <cost>: ` +
strconv.Itoa(bcrypt.MinCost) + ".." + strconv.Itoa(bcrypt.MaxCost) +
", default: " + strconv.Itoa(bcrypt.DefaultCost) + `.)
(^) Password: can be piped-in or prompted for, it gets cut off after ` +
strconv.Itoa(pwMaxLen) + ` bytes.
Longer ones are accepted without warning, using only the first ` +
strconv.Itoa(pwMaxLen) + " bytes!";
fmt.Fprintln(os.Stderr, helptxt)
if msg != "" {
fmt.Fprintln(os.Stderr, "Abort: "+msg)
os.Exit(2)
}
os.Exit(0)
}
func main() { // IO:self
hash, cmd, cost := "", "", bcrypt.DefaultCost
for _, arg := range os.Args {
if self == "" {
selves := strings.Split(arg, "/")
self = selves[len(selves)-1]
continue
}
if cmd == "COST" {
if hash != "" {
showHelp("Too many arguments for cost: " + arg)
}
hash = arg
continue
}
if cmd == "CHECK" {
if arg == "-q" || arg == "--quiet" {
quiet = true
continue
}
if hash != "" {
showHelp("Hash already given, too many arguments: " + arg)
} else {
hash = arg
continue
}
}
if cmd == "" && (arg == "cost" || arg == "-c" || arg == "--cost") {
cmd = "COST"
continue
}
if arg == "help" || arg == "-h" || arg == "--help" {
showHelp("")
}
if cmd == "" {
c, e := strconv.Atoi(arg)
if e == nil { // Integer: a hash command
cmd, cost = "HASH", c
continue
}
if arg == "-q" || arg == "--quiet" {
cmd, quiet = "CHECK", true
continue
}
cmd, hash = "CHECK", arg
} else {
showHelp("Too many arguments: " + arg)
}
}
if cmd == "" {
cmd = "HASH"
}
if hash != "" { // Check hash
hashpart := strings.Split(hash, "$")
switch true {
case len(hashpart) != 4:
fmt.Fprintln(os.Stderr, "Abort: Exactly 3 '$' in a bcrypt hash, invalid hash: "+hash)
os.Exit(3)
case len(hashpart[0]) > 0:
fmt.Fprintln(os.Stderr, "Abort: A proper bcrypt hash starts with '$', invalid hash: "+hash)
os.Exit(4)
case len(hashpart[1]) == 0:
fmt.Fprintln(os.Stderr, "Abort: Missing crypt type (between the 1st & 2nd '$'), invalid hash: "+hash)
os.Exit(5)
case hashpart[1][:1] != "2":
fmt.Fprintln(os.Stderr, "Abort: The crypt type (between the 1st & 2nd '$') for bcrypt starts with '2', invalid hash: "+hash)
os.Exit(6)
case len(hashpart[1]) > 2:
fmt.Fprintln(os.Stderr, "Abort: The crypt type (between the 1st & 2nd '$') is 1 or 2 characters long, invalid hash: "+hash)
os.Exit(7)
case len(hashpart[2]) != 2:
fmt.Fprintln(os.Stderr, "Abort: The cost (between the 2nd & 3rd '$') must be 2 characters long, invalid hash: "+hash)
os.Exit(8)
case hashpart[2][0] < '0' || hashpart[2][0] > '3' || hashpart[2][1] < '0' || hashpart[2][1] > '9':
fmt.Fprintln(os.Stderr, "Abort: The cost (between the 2nd & 3rd '$') must be numeric and 04..31, invalid hash: "+hash)
os.Exit(9)
case (hashpart[2][0] == '0' && hashpart[2][1] < '4') || (hashpart[2][0] == '3' && hashpart[2][1] > '1'):
fmt.Fprintln(os.Stderr, "Abort: The cost (between the 2nd & 3rd '$') must be 04..31, invalid hash: "+hash)
os.Exit(10)
case len(hashpart[3]) != 53:
fmt.Fprintln(os.Stderr, "Abort: The salt & password hash (after the 3rd '$') must be 53 characters long, invalid hash: "+hash)
os.Exit(11)
}
}
switch cmd {
case "COST":
if hash == "" {
showHelp("Hash needed to tell its cost")
}
fmt.Printf("%d\n", getCost(hash))
case "CHECK":
doCheck(getPassword(), []byte(hash))
case "HASH":
if cost < bcrypt.MinCost || cost > bcrypt.MaxCost {
showHelp("Argument for cost not 4..31, out of range: " + fmt.Sprint(cost))
}
fmt.Println(getHash(getPassword(), cost))
}
}
func getCost(hash string) int {
cost, e := bcrypt.Cost([]byte(hash))
if e != nil {
fmt.Fprintln(os.Stderr, e)
os.Exit(12)
}
return cost
}
func doCheck(password, hash []byte) {
if bcrypt.CompareHashAndPassword(hash, password) == nil {
if !quiet {
fmt.Println("Y")
}
} else {
if !quiet {
fmt.Println("N")
}
os.Exit(1)
}
}
func getHash(password []byte, cost int) string {
hash, e := bcrypt.GenerateFromPassword(password, cost)
if e != nil {
fmt.Fprintln(os.Stderr, e)
os.Exit(13)
}
return string(hash)
}
func getPassword() []byte {
var password []byte
if !term.IsTerminal(0) {
password, _ = io.ReadAll(os.Stdin)
}
if len(password) == 0 {
fmt.Fprintf(os.Stderr, "Enter password: ")
pw, _ := term.ReadPassword(0)
fmt.Fprintf(os.Stderr, "\r \r")
password = []byte(pw)
}
if len(password) > pwMaxLen {
password = password[:pwMaxLen]
}
return password
}