-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto-utils.go
38 lines (32 loc) · 990 Bytes
/
crypto-utils.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
package simpleutils
import (
"crypto/rand"
"crypto/sha1"
"encoding/base64"
)
//GenerateRandomBytes returns a byte array of cryptographic (true)
//random numbers. The byte array length is specified by count.
func GenerateRandomBytes(count int) ([]byte, error) {
bytes := make([]byte, count)
_, err := rand.Read(bytes)
if err != nil {
return nil, err
}
return bytes, nil
}
//GenerateRandomString returns a base-64 encoding of a cryptographic
//(true) random number byte array. The byte array length is specified
//by count.
func GenerateRandomString(count int) (string, error) {
bytes, err := GenerateRandomBytes(count)
return base64.URLEncoding.EncodeToString(bytes), err
}
// HashedText is a typed alias for string
type HashedText string
//HashText returns a sha1 hash of text in base64 encoding.
func HashText(text string) HashedText {
hasher := sha1.New()
hasher.Write([]byte(text))
sha := HashedText(base64.URLEncoding.EncodeToString(hasher.Sum(nil)))
return sha
}