Skip to content

Commit bc7d1d1

Browse files
rolandshoemakergopherbot
authored andcommitted
bcrypt: reject passwords longer than 72 bytes
By design, bcrypt only uses the first 72 bytes of a password when generating a hash. Most implementations, including the reference one, simply silently ignore any trailing input when provided passwords longer than 72 bytes. This can cause confusion for users who expect the entire password to be used to generate the hash. In GenerateFromPassword, reject passwords longer than 72 bytes. CompareHashAndPassword will still accept these passwords, since we cannot break hashes that have already been stored. Fixes golang/go#36546 Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a Reviewed-on: https://go-review.googlesource.com/c/crypto/+/450415 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Jason McNeil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]>
1 parent 7e3ac20 commit bc7d1d1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

bcrypt/bcrypt.go

+9
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,20 @@ type hashed struct {
8282
minor byte
8383
}
8484

85+
// ErrPasswordTooLong is returned when the password passed to
86+
// GenerateFromPassword is too long (i.e. > 72 bytes).
87+
var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")
88+
8589
// GenerateFromPassword returns the bcrypt hash of the password at the given
8690
// cost. If the cost given is less than MinCost, the cost will be set to
8791
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
8892
// to compare the returned hashed password with its cleartext version.
93+
// GenerateFromPassword does not accept passwords longer than 72 bytes, which
94+
// is the longest password bcrypt will operate on.
8995
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
96+
if len(password) > 72 {
97+
return nil, ErrPasswordTooLong
98+
}
9099
p, err := newFromPassword(password, cost)
91100
if err != nil {
92101
return nil, err

bcrypt/bcrypt_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,10 @@ func TestNoSideEffectsFromCompare(t *testing.T) {
241241
t.Errorf("got=%q want=%q", got, want)
242242
}
243243
}
244+
245+
func TestPasswordTooLong(t *testing.T) {
246+
_, err := GenerateFromPassword(make([]byte, 73), 1)
247+
if err != ErrPasswordTooLong {
248+
t.Errorf("unexpected error: got %q, want %q", err, ErrPasswordTooLong)
249+
}
250+
}

0 commit comments

Comments
 (0)