Skip to content

Commit

Permalink
Deprecate scrypt, allow using decoded bcrypt hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Jul 12, 2022
1 parent 95b3e4a commit da828d2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
15 changes: 12 additions & 3 deletions modules/caddyhttp/caddyauth/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
weakrand "math/rand"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -117,10 +118,18 @@ func (hba *HTTPBasicAuth) Provision(ctx caddy.Context) error {
return fmt.Errorf("account %d: username and password are required", i)
}

acct.password, err = base64.StdEncoding.DecodeString(acct.Password)
if err != nil {
return fmt.Errorf("base64-decoding password: %v", err)
// Passwords starting with '$' are likely in Modular Crypt Format,
// so we don't need to base64 decode them. But historically, we
// required redundant base64, so we try to decode it otherwise.
if strings.HasPrefix(acct.Password, "$") {
acct.password = []byte(acct.Password)
} else {
acct.password, err = base64.StdEncoding.DecodeString(acct.Password)
if err != nil {
return fmt.Errorf("base64-decoding password: %v", err)
}
}

if acct.Salt != "" {
acct.salt, err = base64.StdEncoding.DecodeString(acct.Salt)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions modules/caddyhttp/caddyauth/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ hash is written to stdout as a base64 string.
Caddy is attached to a controlling tty, the plaintext will
not be echoed.
--algorithm may be bcrypt or scrypt. If script, the default
--algorithm may be bcrypt or scrypt. If scrypt, the default
parameters are used.
Use the --salt flag for algorithms which require a salt to
be provided (scrypt).
Note that scrypt is deprecated. Please use 'bcrypt' instead.
`,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("hash-password", flag.ExitOnError)
Expand Down Expand Up @@ -112,23 +114,24 @@ func cmdHashPassword(fs caddycmd.Flags) (int, error) {
}

var hash []byte
var hashString string
switch algorithm {
case "bcrypt":
hash, err = BcryptHash{}.Hash(plaintext, nil)
hashString = string(hash)
case "scrypt":
def := ScryptHash{}
def.SetDefaults()
hash, err = def.Hash(plaintext, salt)
hashString = base64.StdEncoding.EncodeToString(hash)
default:
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)
}
if err != nil {
return caddy.ExitCodeFailedStartup, err
}

hashBase64 := base64.StdEncoding.EncodeToString(hash)

fmt.Println(hashBase64)
fmt.Println(hashString)

return 0, nil
}
5 changes: 4 additions & 1 deletion modules/caddyhttp/caddyauth/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (BcryptHash) FakeHash() []byte {
}

// ScryptHash implements the scrypt KDF as a hash.
//
// DEPRECATED, please use 'bcrypt' instead.
type ScryptHash struct {
// scrypt's N parameter. If unset or 0, a safe default is used.
N int `json:"N,omitempty"`
Expand All @@ -88,8 +90,9 @@ func (ScryptHash) CaddyModule() caddy.ModuleInfo {
}

// Provision sets up s.
func (s *ScryptHash) Provision(_ caddy.Context) error {
func (s *ScryptHash) Provision(ctx caddy.Context) error {
s.SetDefaults()
ctx.Logger(s).Warn("use of 'scrypt' is deprecated, please use 'bcrypt' instead")
return nil
}

Expand Down

0 comments on commit da828d2

Please sign in to comment.