Add password hashing with argon2

This commit is contained in:
bronku 2025-03-28 21:53:04 +01:00
parent 01da46b81e
commit 1dd0332bc1
11 changed files with 107 additions and 15 deletions

21
internal/crypto/crypto.go Normal file
View file

@ -0,0 +1,21 @@
package crypto
import (
"crypto/rand"
"encoding/base64"
"log"
"golang.org/x/crypto/argon2"
)
func PasswordHash(password, salt string) string {
return string(argon2.Key([]byte(password), []byte(salt), 3, 32*1024, 4, 32))
}
func GenerateKey() string {
key := [32]byte{}
if _, err := rand.Read(key[:]); err != nil {
log.Fatal("can't generate a vaild key", err)
}
return base64.StdEncoding.EncodeToString(key[:])
}