-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
55 lines (43 loc) · 1.24 KB
/
hash.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package utils
import (
"github.com/joho/godotenv"
"golang.org/x/crypto/argon2"
)
type params struct {
memory uint32
iterations uint32
parallelism uint8
saltLength uint32
keyLength uint32
}
// HashPassword will hash a given string into a unreadable byte array
func HashPassword(password string) []byte {
p := ¶ms{
memory: 64 * 1024,
iterations: 3,
parallelism: 2,
keyLength: 32,
}
env, _ := godotenv.Read(".env")
saltString := env["PW_SALT"]
salt := []byte(saltString)
// Pass the plaintext password, salt and parameters to the argon2.IDKey
// function. This will generate a hash of the password using the Argon2id
// variant.
hashedPass := argon2.IDKey([]byte(password), salt, p.iterations, p.memory, p.parallelism, p.keyLength)
return hashedPass
}
// CheckPass will check if a user's saved password and a given hashed password are the same
func CheckPass(enteredPassword string, savedPassword []byte) bool {
hashedEnteredPassword := HashPassword(enteredPassword)
var isTheSame = true
if len(hashedEnteredPassword) != len(savedPassword) {
isTheSame = false
}
for i := 0; i < len(savedPassword); i++ {
if hashedEnteredPassword[i] != savedPassword[i] {
isTheSame = false
}
}
return isTheSame
}