mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-09 22:03:39 -05:00
The production password hasher uses PBKDF2 with 600,000 iterations, which is slow especially when combined with race detection. This adds a fast test hasher (work factor 1) that can be used during tests to speed up user creation. The fast hasher is only available in non-production builds via build tags, ensuring it cannot be used in production.
56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
//go:build !production
|
|
|
|
package hashers
|
|
|
|
import "testing"
|
|
|
|
// testHasher is used during tests to override the latestHasher with a faster
|
|
// alternative. This should only be set via SetTestHasher and only in test code.
|
|
var testHasher PasswordHasher
|
|
|
|
// getLatestHasher returns the hasher to use for password operations.
|
|
// In non-production builds, if a test hasher has been set via SetTestHasher,
|
|
// it will be returned instead of the production latestHasher.
|
|
func getLatestHasher() PasswordHasher {
|
|
if testHasher != nil {
|
|
return testHasher
|
|
}
|
|
return latestHasher
|
|
}
|
|
|
|
// SetTestHasher sets a hasher to be used instead of the latestHasher during tests.
|
|
// This is useful for speeding up tests that create many users, as password hashing
|
|
// is computationally expensive. Pass nil to restore normal behavior.
|
|
//
|
|
// This function is only available in non-production builds and should only be
|
|
// called from test code, typically in TestMain. It will panic if called outside
|
|
// of a test context.
|
|
//
|
|
// Example usage:
|
|
//
|
|
// func TestMain(m *testing.M) {
|
|
// hashers.SetTestHasher(hashers.FastTestHasher())
|
|
// os.Exit(m.Run())
|
|
// }
|
|
func SetTestHasher(h PasswordHasher) {
|
|
if !testing.Testing() {
|
|
panic("SetTestHasher called outside of test context")
|
|
}
|
|
testHasher = h
|
|
}
|
|
|
|
// FastTestHasher returns a PBKDF2 hasher configured with minimal work factor
|
|
// for use in tests while still producing valid password hashes that can be
|
|
// verified.
|
|
//
|
|
// This function is only available in non-production builds.
|
|
func FastTestHasher() PasswordHasher {
|
|
h, err := NewPBKDF2(1, defaultKeyLength)
|
|
if err != nil {
|
|
panic("failed to create fast test hasher: " + err.Error())
|
|
}
|
|
return h
|
|
}
|