forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.go
46 lines (39 loc) · 1.2 KB
/
encryption.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
package utils
import (
"crypto/sha256"
"fmt"
"golang.org/x/crypto/bcrypt"
"math/rand"
"time"
)
// HashPassword returns the bcrypt hash of a password string
func HashPassword(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes)
}
// CheckHash returns true if the password matches with a hashed bcrypt password
func CheckHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// NewSHA1Hash returns a random SHA1 hash based on a specific length
func NewSHA256Hash() string {
d := make([]byte, 10)
rand.Seed(Now().UnixNano())
rand.Read(d)
return fmt.Sprintf("%x", sha256.Sum256(d))
}
// NewSHA1Hash returns a random SHA1 hash based on a specific length
func Sha256Hash(val string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(val)))
}
var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
// RandomString generates a random string of n length
func RandomString(n int) string {
b := make([]rune, n)
rand.Seed(time.Now().UnixNano())
for i := range b {
b[i] = characterRunes[rand.Intn(len(characterRunes))]
}
return string(b)
}