32
func init() {
33
caddycmd.RegisterCommand(caddycmd.Command{
34
Name: "hash-password",
35
- Usage: "[--plaintext <password>] [--algorithm <name>]",
35
+ Usage: "[--plaintext <password>] [--algorithm <name>] [--cost <difficulty>]",
36
Short: "Hashes a password and writes base64",
37
Long: `
38
Convenient way to hash a plaintext password. The resulting
43
not be echoed.
44
45
--algorithm currently only supports 'bcrypt', and is the default.
46
+
47
+--cost sets the bcrypt hashing difficulty.
48
+Higher values increase security by making the hash computation slower and more CPU-intensive.
49
+If the provided cost is not within the valid range [bcrypt.MinCost, bcrypt.MaxCost],
50
+the default value (defaultBcryptCost) will be used instead.
51
+Note: Higher cost values can significantly degrade performance on slower systems.
52
`,
53
CobraFunc: func(cmd *cobra.Command) {
54
cmd.Flags().StringP("plaintext", "p", "", "The plaintext password")
55
cmd.Flags().StringP("algorithm", "a", "bcrypt", "Name of the hash algorithm")
56
+ cmd.Flags().Int("bcrypt-cost", defaultBcryptCost, "Bcrypt hashing cost (only used with 'bcrypt' algorithm)")
57
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdHashPassword)
58
},
59
})
64
65
algorithm := fs.String("algorithm")
66
plaintext := []byte(fs.String("plaintext"))
67
+ bcryptCost := fs.Int("bcrypt-cost")
68
69
if len(plaintext) == 0 {
70
fd := int(os.Stdin.Fd())
116
var hashString string
117
switch algorithm {
118
case "bcrypt":
111
- hash, err = BcryptHash{}.Hash(plaintext)
119
+ hash, err = BcryptHash{cost: bcryptCost}.Hash(plaintext)
120
hashString = string(hash)
121
default:
122
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)