From 5b4251fd39c43e4cfed27e032a4efb2bbba28e38 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 14 May 2023 20:12:06 -0500 Subject: add auth & pages --- auth/auth.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 auth/auth.go (limited to 'auth/auth.go') diff --git a/auth/auth.go b/auth/auth.go new file mode 100644 index 0000000..ac908ab --- /dev/null +++ b/auth/auth.go @@ -0,0 +1,33 @@ +package auth + +import ( + "golang.org/x/crypto/argon2" + "crypto/rand" + "sanine.net/git/phlox/config" +) + + +func GenerateSalt() ([]byte, error) { + salt := make([]byte, 10) + _, err := rand.Read(salt) + return salt, err +} + + +func HashPassword(password string, salt []byte) []byte { + return argon2.IDKey( + []byte(password), salt, + 1, 64*1024, 4, 32, + ) +} + + +func AuthenticateUser(user config.User, password string) bool { + hash := HashPassword(password, user.Salt) + for i, v := range user.PasswordHash { + if v != hash[i] { + return false; + } + } + return true +} -- cgit v1.2.1