summaryrefslogtreecommitdiff
path: root/auth/auth.go
blob: ac908ab988e0afb8b2235af6c54a3c726db7b22a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}