[crypto] bcrypt: reject passwords longer than 72 bytes

40 views
Skip to first unread message

Roland Shoemaker (Gerrit)

unread,
Nov 14, 2022, 3:19:21 PM11/14/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Roland Shoemaker has uploaded this change for review.

View Change

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
---
M bcrypt/bcrypt.go
M bcrypt/bcrypt_test.go
2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/bcrypt/bcrypt.go b/bcrypt/bcrypt.go
index addf56b..8602315 100644
--- a/bcrypt/bcrypt.go
+++ b/bcrypt/bcrypt.go
@@ -82,11 +82,18 @@
minor byte
}

+var errPasswordTooLong = errors.New("crypto/bcrypt: password too long")
+
// GenerateFromPassword returns the bcrypt hash of the password at the given
// cost. If the cost given is less than MinCost, the cost will be set to
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
// to compare the returned hashed password with its cleartext version.
+// GenerateFromPassword does not accept passwords longer than 72 bytes, which
+// is the longest password bcrypt will operate on.
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
+ if len(password) > 72 {
+ return nil, errPasswordTooLong
+ }
p, err := newFromPassword(password, cost)
if err != nil {
return nil, err
diff --git a/bcrypt/bcrypt_test.go b/bcrypt/bcrypt_test.go
index b7162d8..77dd3dc 100644
--- a/bcrypt/bcrypt_test.go
+++ b/bcrypt/bcrypt_test.go
@@ -241,3 +241,10 @@
t.Errorf("got=%q want=%q", got, want)
}
}
+
+func TestPasswordTooLong(t *testing.T) {
+ _, err := GenerateFromPassword(make([]byte, 73), 1)
+ if err != errPasswordTooLong {
+ t.Errorf("unexpected error: got %q, want %q", err, errPasswordTooLong)
+ }
+}

To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: crypto
Gerrit-Branch: master
Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
Gerrit-Change-Number: 450415
Gerrit-PatchSet: 1
Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
Gerrit-MessageType: newchange

Roland Shoemaker (Gerrit)

unread,
Nov 14, 2022, 3:19:45 PM11/14/22
to goph...@pubsubhelper.golang.org, Filippo Valsorda, golang-co...@googlegroups.com

Attention is currently required from: Filippo Valsorda.

Patch set 1:Run-TryBot +1Auto-Submit +1

View Change

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 1
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-Attention: Filippo Valsorda <fil...@golang.org>
    Gerrit-Comment-Date: Mon, 14 Nov 2022 20:19:39 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Dan Kortschak (Gerrit)

    unread,
    Nov 14, 2022, 3:51:44 PM11/14/22
    to Roland Shoemaker, goph...@pubsubhelper.golang.org, Gopher Robot, Filippo Valsorda, golang-co...@googlegroups.com

    Attention is currently required from: Filippo Valsorda, Roland Shoemaker.

    View Change

    1 comment:

    • File bcrypt/bcrypt.go:

      • Patch Set #1, Line 85: var errPasswordTooLong = errors.New("crypto/bcrypt: password too long")

        Is it worth making this an exported label so that client code can differentiate between this case and errors in `newFromPassword`?

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 1
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Attention: Filippo Valsorda <fil...@golang.org>
    Gerrit-Comment-Date: Mon, 14 Nov 2022 20:51:38 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Filippo Valsorda (Gerrit)

    unread,
    Nov 14, 2022, 6:13:48 PM11/14/22
    to Roland Shoemaker, goph...@pubsubhelper.golang.org, Filippo Valsorda, Dan Kortschak, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Roland Shoemaker.

    Patch set 1:Code-Review +2

    View Change

    2 comments:

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 1
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Comment-Date: Mon, 14 Nov 2022 23:13:42 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Roland Shoemaker (Gerrit)

    unread,
    Nov 21, 2022, 1:41:15 PM11/21/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Roland Shoemaker.

    Roland Shoemaker uploaded patch set #2 to this change.

    View Change

    The following approvals got outdated and were removed: Auto-Submit+1 by Roland Shoemaker, Run-TryBot+1 by Roland Shoemaker, TryBot-Result+1 by Gopher Robot

    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
    ---
    M bcrypt/bcrypt.go
    M bcrypt/bcrypt_test.go
    2 files changed, 37 insertions(+), 0 deletions(-)

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-MessageType: newpatchset

    Roland Shoemaker (Gerrit)

    unread,
    Nov 21, 2022, 1:41:24 PM11/21/22
    to goph...@pubsubhelper.golang.org, Filippo Valsorda, Dan Kortschak, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak.

    Patch set 2:Run-TryBot +1Auto-Submit +1

    View Change

    3 comments:

    • Patchset:

      • Patch Set #1:

        Let's give the discussion on the issue a week or so to tell us why this would break people https://g […]

        Looks like there haven't been any objections.

    • File bcrypt/bcrypt.go:

      • Patch Set #1, Line 85: var errPasswordTooLong = errors.New("crypto/bcrypt: password too long")

        Is it worth making this an exported label so that client code can differentiate between this case an […]

        Done

      • Done

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Comment-Date: Mon, 21 Nov 2022 18:41:20 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Dan Kortschak <d...@kortschak.io>
    Comment-In-Reply-To: Filippo Valsorda <fil...@golang.org>
    Gerrit-MessageType: comment

    Jason McNeil (Gerrit)

    unread,
    Nov 26, 2022, 11:34:06 AM11/26/22
    to Roland Shoemaker, goph...@pubsubhelper.golang.org, Gopher Robot, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak, Roland Shoemaker.

    Patch set 2:Code-Review +1

    View Change

    1 comment:

    • Patchset:

      • Patch Set #2:

        this is a good solution that doesn’t break compatibility with hashes but helps ensure longer passwords aren’t used with silent truncation.

        this should be accepted.

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Comment-Date: Sat, 26 Nov 2022 03:00:59 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Sam Mortimer (Gerrit)

    unread,
    Nov 26, 2022, 1:02:32 PM11/26/22
    to Roland Shoemaker, goph...@pubsubhelper.golang.org, Jason McNeil, Gopher Robot, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak, Roland Shoemaker.

    View Change

    1 comment:

    • File bcrypt/bcrypt.go:

      • Patch Set #2, Line 87: var ErrPasswordTooLong = errors.New("bcrypt: password too long")

        how about: "bcrypt: password length exceeds 72 bytes"

        so the user knows doesn't have to research what "too long" means

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Comment-Date: Sat, 26 Nov 2022 18:02:28 +0000

    Roland Shoemaker (Gerrit)

    unread,
    Dec 21, 2022, 12:01:15 PM12/21/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak, Roland Shoemaker.

    Roland Shoemaker uploaded patch set #4 to this change.

    View Change

    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
    ---
    M bcrypt/bcrypt.go
    M bcrypt/bcrypt_test.go
    2 files changed, 37 insertions(+), 0 deletions(-)

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 4
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-MessageType: newpatchset

    Roland Shoemaker (Gerrit)

    unread,
    Dec 21, 2022, 12:01:35 PM12/21/22
    to goph...@pubsubhelper.golang.org, Sam Mortimer, Jason McNeil, Gopher Robot, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak, Jason McNeil, Sam Mortimer.

    Patch set 4:Run-TryBot +1Auto-Submit +1

    View Change

    2 comments:

    • Patchset:

      • Patch Set #2:

        this is a good solution that doesn’t break compatibility with hashes but helps ensure longer passwor […]

        Ack

    • File bcrypt/bcrypt.go:

      • how about: "bcrypt: password length exceeds 72 bytes" […]

        Done

    To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: crypto
    Gerrit-Branch: master
    Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
    Gerrit-Change-Number: 450415
    Gerrit-PatchSet: 4
    Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
    Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
    Gerrit-Attention: Sam Mortimer <sam.mo...@gmail.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Attention: Jason McNeil <jmc...@x2studios.com>
    Gerrit-Comment-Date: Wed, 21 Dec 2022 17:01:31 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Sam Mortimer <sam.mo...@gmail.com>
    Comment-In-Reply-To: Jason McNeil <jmc...@x2studios.com>
    Gerrit-MessageType: comment

    Damien Neil (Gerrit)

    unread,
    Dec 21, 2022, 12:19:22 PM12/21/22
    to Roland Shoemaker, goph...@pubsubhelper.golang.org, Gopher Robot, Sam Mortimer, Jason McNeil, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak, Jason McNeil, Roland Shoemaker, Sam Mortimer.

    Patch set 4:Code-Review +1

    View Change

      To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: crypto
      Gerrit-Branch: master
      Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
      Gerrit-Change-Number: 450415
      Gerrit-PatchSet: 4
      Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
      Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
      Gerrit-CC: Dan Kortschak <d...@kortschak.io>
      Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
      Gerrit-Attention: Sam Mortimer <sam.mo...@gmail.com>
      Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
      Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
      Gerrit-Attention: Jason McNeil <jmc...@x2studios.com>
      Gerrit-Comment-Date: Wed, 21 Dec 2022 17:19:16 +0000

      Gopher Robot (Gerrit)

      unread,
      Dec 21, 2022, 12:19:34 PM12/21/22
      to Roland Shoemaker, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Damien Neil, Sam Mortimer, Jason McNeil, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

      Gopher Robot submitted this change.

      View Change



      1 is the latest approved patch-set.
      The change was submitted with unreviewed changes in the following files:

      ```
      The name of the file: bcrypt/bcrypt_test.go
      Insertions: 2, Deletions: 2.

      @@ -244,7 +244,7 @@

      func TestPasswordTooLong(t *testing.T) {

      _, err := GenerateFromPassword(make([]byte, 73), 1)
      -	if err != errPasswordTooLong {
      - t.Errorf("unexpected error: got %q, want %q", err, errPasswordTooLong)
      + if err != ErrPasswordTooLong {
      + t.Errorf("unexpected error: got %q, want %q", err, ErrPasswordTooLong)
      }
      }
      ```
      ```
      The name of the file: bcrypt/bcrypt.go
      Insertions: 4, Deletions: 2.

      @@ -82,7 +82,9 @@
      minor byte
      }

      -var errPasswordTooLong = errors.New("crypto/bcrypt: password too long")
      +// ErrPasswordTooLong is returned when the password passed to
      +// GenerateFromPassword is too long (i.e. > 72 bytes).
      +var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")


      // GenerateFromPassword returns the bcrypt hash of the password at the given
      // cost. If the cost given is less than MinCost, the cost will be set to
      @@ -92,7 +94,7 @@

      // is the longest password bcrypt will operate on.
      func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
       	if len(password) > 72 {
      - return nil, errPasswordTooLong
      + return nil, ErrPasswordTooLong

      }
      p, err := newFromPassword(password, cost)
      if err != nil {
      ```

      Approvals: Damien Neil: Looks good to me, but someone else must approve Roland Shoemaker: Run TryBots; Automatically submit change Jason McNeil: Looks good to me, but someone else must approve Gopher Robot: TryBots succeeded Filippo Valsorda: Looks good to me, approved
      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 <dn...@google.com>
      Reviewed-by: Jason McNeil <jmc...@x2studios.com>
      TryBot-Result: Gopher Robot <go...@golang.org>
      Reviewed-by: Filippo Valsorda <fil...@golang.org>
      Auto-Submit: Roland Shoemaker <rol...@golang.org>
      Run-TryBot: Roland Shoemaker <rol...@golang.org>

      ---
      M bcrypt/bcrypt.go
      M bcrypt/bcrypt_test.go
      2 files changed, 44 insertions(+), 0 deletions(-)

      diff --git a/bcrypt/bcrypt.go b/bcrypt/bcrypt.go
      index addf56b..5577c0f 100644
      --- a/bcrypt/bcrypt.go
      +++ b/bcrypt/bcrypt.go
      @@ -82,11 +82,20 @@
      minor byte
      }

      +// ErrPasswordTooLong is returned when the password passed to
      +// GenerateFromPassword is too long (i.e. > 72 bytes).
      +var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")

      +
      // GenerateFromPassword returns the bcrypt hash of the password at the given
      // cost. If the cost given is less than MinCost, the cost will be set to
      // DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
      // to compare the returned hashed password with its cleartext version.
      +// GenerateFromPassword does not accept passwords longer than 72 bytes, which
      +// is the longest password bcrypt will operate on.
      func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
      + if len(password) > 72 {
      +		return nil, ErrPasswordTooLong

      + }
      p, err := newFromPassword(password, cost)
      if err != nil {
      return nil, err
      diff --git a/bcrypt/bcrypt_test.go b/bcrypt/bcrypt_test.go
      index b7162d8..8b589e3 100644

      --- a/bcrypt/bcrypt_test.go
      +++ b/bcrypt/bcrypt_test.go
      @@ -241,3 +241,10 @@
      t.Errorf("got=%q want=%q", got, want)
      }
      }
      +
      +func TestPasswordTooLong(t *testing.T) {
      + _, err := GenerateFromPassword(make([]byte, 73), 1)
      +	if err != ErrPasswordTooLong {
      + t.Errorf("unexpected error: got %q, want %q", err, ErrPasswordTooLong)
      + }
      +}

      To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: crypto
      Gerrit-Branch: master
      Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
      Gerrit-Change-Number: 450415
      Gerrit-PatchSet: 5
      Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
      Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
      Gerrit-CC: Dan Kortschak <d...@kortschak.io>
      Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
      Gerrit-MessageType: merged

      Jason McNeil (Gerrit)

      unread,
      Dec 21, 2022, 12:43:46 PM12/21/22
      to Gopher Robot, Roland Shoemaker, goph...@pubsubhelper.golang.org, Damien Neil, Sam Mortimer, Filippo Valsorda, Dan Kortschak, golang-co...@googlegroups.com

      View Change

      1 comment:

      To view, visit change 450415. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: crypto
      Gerrit-Branch: master
      Gerrit-Change-Id: I039addd2a2961a7fa9d1e4a3e892a9e3c8bf4c9a
      Gerrit-Change-Number: 450415
      Gerrit-PatchSet: 5
      Gerrit-Owner: Roland Shoemaker <rol...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Filippo Valsorda <fil...@golang.org>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Jason McNeil <jmc...@x2studios.com>
      Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
      Gerrit-CC: Dan Kortschak <d...@kortschak.io>
      Gerrit-CC: Sam Mortimer <sam.mo...@gmail.com>
      Gerrit-Comment-Date: Wed, 21 Dec 2022 17:43:42 +0000
      Reply all
      Reply to author
      Forward
      0 new messages