mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
feat: add bcrypt command
This commit is contained in:
@@ -139,6 +139,8 @@ users:
|
|||||||
- username: admin
|
- username: admin
|
||||||
password: admin
|
password: admin
|
||||||
# Example 'john' user with bcrypt encrypted password, with custom directory.
|
# Example 'john' user with bcrypt encrypted password, with custom directory.
|
||||||
|
# You can generate a bcrypt-encrypted password by using the 'webdav bcrypt'
|
||||||
|
# command lint utility.
|
||||||
- username: john
|
- username: john
|
||||||
password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
|
password: "{bcrypt}$2y$10$zEP6oofmXFeHaeMfBNLnP.DO8m.H.Mwhd24/TOX2MWLxAExXi4qgi"
|
||||||
directory: /another/path
|
directory: /another/path
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flags := bcryptCmd.Flags()
|
||||||
|
flags.IntP("cost", "c", bcrypt.DefaultCost, "cost used to generate password, higher cost leads to slower verification times")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(bcryptCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var bcryptCmd = &cobra.Command{
|
||||||
|
Use: "bcrypt",
|
||||||
|
Short: "Generate a bcrypt encrypted password",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
cost, err := cmd.Flags().GetInt("cost")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cost < bcrypt.MinCost {
|
||||||
|
return fmt.Errorf("given cost cannot be under minimum cost of %d", bcrypt.MinCost)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cost > bcrypt.MaxCost {
|
||||||
|
return fmt.Errorf("given cost cannot be over maximum cost of %d", bcrypt.MaxCost)
|
||||||
|
}
|
||||||
|
|
||||||
|
pwd := args[0]
|
||||||
|
if pwd == "" {
|
||||||
|
return errors.New("password argument must not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), cost)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(hash))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user