Skip to content

Commit

Permalink
Only use fmt.Errorf() if the error string requires formatting, otherw…
Browse files Browse the repository at this point in the history
…ise use errors.New()
  • Loading branch information
dhui committed Mar 28, 2018
1 parent e58973e commit 44aab25
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions credential.go
Expand Up @@ -2,7 +2,7 @@ package passhash

import (
"crypto/subtle"
"fmt"
"errors"
"golang.org/x/crypto/bcrypt"
"net"
)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (c *Credential) ChangePasswordWithConfig(config Config, oldPassword, newPas
// ChangePasswordWithConfigAndIP changes the password for the given Credential and updates the Credential to meet the Config parameters if necessary
func (c *Credential) ChangePasswordWithConfigAndIP(config Config, oldPassword, newPassword string, ip net.IP) error {
if !c.matchPassword(oldPassword, config.AuditLogger, ip) {
return fmt.Errorf("Old password does not match existing password")
return errors.New("Old password does not match existing password")
}
return c.ResetWithConfigAndIP(config, newPassword, ip)
}
Expand Down
7 changes: 4 additions & 3 deletions passhash.go
Expand Up @@ -2,6 +2,7 @@ package passhash

import (
"crypto"
"errors"
"fmt"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/pbkdf2"
Expand Down Expand Up @@ -91,16 +92,16 @@ func getPasswordHash(kdf Kdf, workFactor WorkFactor, salt []byte, keyLength int,
case Pbkdf2Sha3_512:
return pbkdf2.Key([]byte(password), salt, wf.Iter, keyLength, sha3.New512), nil
default:
return []byte{}, fmt.Errorf("Pbkdf2WorkFactor can only be specified with the Pbkdf2 Kdf")
return []byte{}, errors.New("Pbkdf2WorkFactor can only be specified with the Pbkdf2 Kdf")
}
case *BcryptWorkFactor:
if kdf != Bcrypt {
return []byte{}, fmt.Errorf("BcryptWorkFactor can only be specified with the Bcrypt Kdf")
return []byte{}, errors.New("BcryptWorkFactor can only be specified with the Bcrypt Kdf")
}
return bcrypt.GenerateFromPassword([]byte(password), wf.Cost)
case *ScryptWorkFactor:
if kdf != Scrypt {
return []byte{}, fmt.Errorf("ScryptWorkFactor can only be specified with the Scrypt Kdf")
return []byte{}, errors.New("ScryptWorkFactor can only be specified with the Scrypt Kdf")
}
return scrypt.Key([]byte(password), salt, wf.N, wf.R, wf.P, keyLength)
default:
Expand Down
3 changes: 2 additions & 1 deletion password_policies.go
@@ -1,6 +1,7 @@
package passhash

import (
"errors"
"fmt"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -56,7 +57,7 @@ type NotCommonPasswordNaive struct {
// PasswordAcceptable accepts passwords that are not common passwords
func (pp NotCommonPasswordNaive) PasswordAcceptable(password string) error {
if pp.CommonPasswords[password] {
return fmt.Errorf("Password is a common password")
return errors.New("Password is a common password")
}
return nil
}
6 changes: 3 additions & 3 deletions store.go
Expand Up @@ -2,7 +2,7 @@ package passhash

import (
"context"
"fmt"
"errors"
)

// CredentialStore is an interfance for customizing Credential storage
Expand All @@ -24,10 +24,10 @@ func (d DummyCredentialStore) StoreContext(context.Context, *Credential) error {

// Load only returns errors
func (d DummyCredentialStore) Load(UserID) (*Credential, error) {
return nil, fmt.Errorf("DummyCredentialStore does not support loading credentials")
return nil, errors.New("DummyCredentialStore does not support loading credentials")
}

// LoadContext only returns errors
func (d DummyCredentialStore) LoadContext(context.Context, UserID) (*Credential, error) {
return nil, fmt.Errorf("DummyCredentialStore does not support loading credentials")
return nil, errors.New("DummyCredentialStore does not support loading credentials")
}

0 comments on commit 44aab25

Please sign in to comment.