mirror of
https://github.com/pushbits/server.git
synced 2025-05-16 02:17:03 +02:00
Make hashing parameters configurable
This commit is contained in:
parent
c0ac5c3d16
commit
ba0306f384
8 changed files with 86 additions and 37 deletions
|
@ -3,28 +3,27 @@ package credentials
|
|||
import (
|
||||
"log"
|
||||
|
||||
"github.com/eikendev/pushbits/configuration"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
// CreatePasswordHash returns a hashed version of the given password.
|
||||
func CreatePasswordHash(password string) []byte {
|
||||
hash, err := argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return []byte(hash)
|
||||
// Manager holds information for managing credentials.
|
||||
type Manager struct {
|
||||
argon2Params *argon2id.Params
|
||||
}
|
||||
|
||||
// ComparePassword compares a hashed password with its possible plaintext equivalent.
|
||||
func ComparePassword(hash, password []byte) bool {
|
||||
match, err := argon2id.ComparePasswordAndHash(string(password), string(hash))
|
||||
// CreateManager instanciates a credential manager.
|
||||
func CreateManager(c configuration.CryptoConfig) *Manager {
|
||||
log.Println("Setting up credential manager.")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return false
|
||||
argon2Params := &argon2id.Params{
|
||||
Memory: c.Argon2.Memory,
|
||||
Iterations: c.Argon2.Iterations,
|
||||
Parallelism: c.Argon2.Parallelism,
|
||||
SaltLength: c.Argon2.SaltLength,
|
||||
KeyLength: c.Argon2.KeyLength,
|
||||
}
|
||||
|
||||
return match
|
||||
return &Manager{argon2Params: argon2Params}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue