-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcmd.go
More file actions
62 lines (53 loc) · 1.39 KB
/
cmd.go
File metadata and controls
62 lines (53 loc) · 1.39 KB
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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"golang.org/x/crypto/bcrypt"
)
const (
// DefaultCost is the default cost for the bcrypt hash
DefaultCost = 10
)
// BcryptGenerateCmd allows encrypting a password provided through stdin
type BcryptGenerateCmd struct {
Cost int `short:"c" long:"cost" default:"10" description:"The cost weight, range of 4-31" value-name:"COST"`
OutWriter io.Writer
InReader io.Reader
}
// Execute performs the Brcrypt operation
func (c *BcryptGenerateCmd) Execute(args []string) error {
if c.Cost < 4 || c.Cost > 31 {
return fmt.Errorf("cost %d is outside allowed range (4,31)", c.Cost)
}
var in io.Reader
if c.InReader != nil {
in = c.InReader
} else if hasPipedStdin() {
in = os.Stdin
} else {
return fmt.Errorf("you must provide the password through stdin")
}
data, err := ioutil.ReadAll(in)
if err != nil {
return err
}
cb, err := bcrypt.GenerateFromPassword(data, c.Cost)
if err != nil {
return fmt.Errorf("error producing bcrypt hash: %v", err)
}
_, err = fmt.Fprintf(c.OutWriter, "%s\n", cb)
return err
}
// NewBcryptGenerateCmd returns a BcryptGenerateCmd with configured defaults
func NewBcryptGenerateCmd() *BcryptGenerateCmd {
return &BcryptGenerateCmd{Cost: DefaultCost, OutWriter: os.Stdout}
}
func hasPipedStdin() bool {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
return true
}
return false
}