-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
66 lines (55 loc) · 1.54 KB
/
helpers.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
56
57
58
59
60
61
62
63
64
65
66
package utils
import (
"log"
"math/rand"
"os"
"runtime"
"golang.org/x/crypto/bcrypt"
)
// LucidHomeDir is the deafult configuration directory on the syste
func LucidHomeDir() string {
configDir, err := os.UserConfigDir()
if err != nil {
CheckError(err)
}
return configDir + "/lucid"
}
// AppPath returns a tring of the current application path
var AppPath string = getAppPath()
func getAppPath() string {
path, err := os.Getwd()
if err != nil {
CheckError(err)
}
return path
}
// CheckError takes in an error value and handles it by logging its output
func CheckError(err error) {
if err != nil {
_, fn, line, _ := runtime.Caller(1)
log.Printf("[error] %s:%d %v", fn, line, err)
}
}
// CreatePasswordHash creates a bcrypt has with a default cost added
func CreatePasswordHash(password string) ([]byte, bool) {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
CheckError(err)
return []byte{}, false
}
return passwordHash, true
}
// PasswordHashCompare checks whether the password and hash in the database cryptographically match
func PasswordHashCompare(password string, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// RandomString generates a random string of the specififed length
func randomString(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}