Skip to content

Commit d822828

Browse files
sbertrangtg123
authored andcommitted
Add support for two more BCrypt variants. (#2)
* Add support for two more BCrypt variants. Summary with historical details: https://stackoverflow.com/questions/15733196/where-2x-prefix-are-used-in-bcrypt/36225192#36225192 * Better example to match the code * Add test case for $2b$ variant * Add test case for $2x$ variant as well
1 parent 0849cea commit d822828

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

bcrypt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type bcryptPassword struct {
1313

1414
//AcceptBcrypt accepts any valid password encoded using bcrypt.
1515
func AcceptBcrypt(src string) (EncodedPasswd, error) {
16-
if !strings.HasPrefix(src, "$2y$") && !strings.HasPrefix(src, "$2a$") {
16+
if !strings.HasPrefix(src, "$2y$") && !strings.HasPrefix(src, "$2a$") && !strings.HasPrefix(src, "$2b$") && !strings.HasPrefix(src, "$2x$") {
1717
return nil, nil
1818
}
1919

@@ -22,7 +22,7 @@ func AcceptBcrypt(src string) (EncodedPasswd, error) {
2222

2323
//RejectBcrypt rejects any password encoded using bcrypt.
2424
func RejectBcrypt(src string) (EncodedPasswd, error) {
25-
if strings.HasPrefix(src, "$2y$") || strings.HasPrefix(src, "$2a$") {
25+
if strings.HasPrefix(src, "$2y$") || strings.HasPrefix(src, "$2a$") || strings.HasPrefix(src, "$2b$") || strings.HasPrefix(src, "$2x$") {
2626
return nil, fmt.Errorf("bcrypt passwords are not accepted: %s", src)
2727
}
2828

bcrypt_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
func Test_Bcrypt(t *testing.T) {
88
testParserGood(t, "bcrypt", AcceptBcrypt, nil, "$2y$05$bWBMg3oUStnhfy5rFvoyreviPySU6hvEmBub5wIlM/D.c5FeYJQ6O", "bar")
9+
testParserGood(t, "bcrypt", AcceptBcrypt, nil, "$2b$08$hQbZuw.cHsECArUAP9mOjehaJxTG9NMJfioQIHcbC0YyXpVybhoQa", "bar")
10+
testParserGood(t, "bcrypt", AcceptBcrypt, nil, "$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e", "\xff\xff\xa3")
911
testParserBad(t, "bcrypt", nil, RejectBcrypt, "$2y$0")
1012
testParserNot(t, "bcrypt", nil, RejectBcrypt, "plaintext")
1113
}

htpasswd.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// by the programmer to support others. (See the sha.go source file as a guide.)
66
//
77
// You will want to use something like...
8-
// myauth := htpasswd.New("My Realm", "./my-htpasswd-file", htpasswd.DefaultSystems, nil)
9-
// m.Use(myauth.Handler)
10-
// ...to configure your authentication and then use the myauth.Handler as a middleware handler in your Martini stack.
8+
// myauth := htpasswd.New("./my-htpasswd-file", htpasswd.DefaultSystems, nil)
9+
// ok := myauth.Match(user, password)
10+
// ...to use in your handler code.
1111
// You should read about that nil, as well as Reread() too.
1212
package htpasswd
1313

0 commit comments

Comments
 (0)