Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility for hashing SCRAM-SHA-256 password #4031

Merged
merged 20 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
henvic committed Feb 8, 2024
commit 30433e75bd527654aa2f058b2eec462769c00f74
34 changes: 17 additions & 17 deletions internal/util/password/scramsha256.go
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,28 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// scramSHA256Params represent password parameters for SCRAM-SHA-256 authentication.
// SCRAMSHA256Hash computes SCRAM-SHA-256 credentials and returns the document that should be stored.
func SCRAMSHA256Hash(password string) (*types.Document, error) {
salt := make([]byte, fixedScramSHA256Params.saltLen)
if _, err := rand.Read(salt); err != nil {
return nil, lazyerrors.Error(err)
}

doc, err := scramSHA256HashParams(password, salt, fixedScramSHA256Params)
if err != nil {
return nil, lazyerrors.Error(err)
}

return doc, nil
}

// scramSHA256Params represent password parameters for SCRAM-SHA-256 authentication
type scramSHA256Params struct {
iterationCount int
saltLen int
}

// fixedScramSHA256Params represent fixed password parameters for SCRAM-SHA-256 authentication.
// fixedScramSHA256Params represent fixed password parameters for SCRAM-SHA-256 authentication
var fixedScramSHA256Params = &scramSHA256Params{
iterationCount: 15_000,
saltLen: 45,
Expand Down Expand Up @@ -68,18 +83,3 @@ func scramSHA256HashParams(password string, salt []byte, params *scramSHA256Para

return doc, nil
}

// SCRAMSHA256Hash computes SCRAM-SHA-256 credentials and returns the document that should be stored.
func SCRAMSHA256Hash(password string) (*types.Document, error) {
salt := make([]byte, fixedScramSHA256Params.saltLen)
if _, err := rand.Read(salt); err != nil {
return nil, lazyerrors.Error(err)
}

doc, err := scramSHA256HashParams(password, salt, fixedScramSHA256Params)
if err != nil {
return nil, lazyerrors.Error(err)
}

return doc, nil
}
2 changes: 1 addition & 1 deletion internal/util/password/scramsha256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type scramSHA256TestCase struct {
err string
}

// scramSHA256TestCases for the SCRAM-SHA-256 authentication.
// Test cases for the SCRAM-SHA-256 authentication.
var scramSHA256TestCases = map[string]scramSHA256TestCase{
henvic marked this conversation as resolved.
Show resolved Hide resolved
// Test vector generated with db.runCommand({createUser: "user", pwd: "pencil", roles: []})
"FromMongoDB": {
Expand Down