-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
49 lines (44 loc) · 889 Bytes
/
util.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
package bluzelle
import (
"io/ioutil"
"log"
"math/rand"
"os"
"time"
"unicode/utf8"
)
func encodeToUTF8(str string) string {
if utf8.ValidString(str) {
return str
}
buffer := make([]rune, 0, len(str))
for i, r := range str {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(str[i:])
if size == 1 {
continue
}
}
buffer = append(buffer, r)
}
return string(buffer)
}
func randUint64() uint64 {
now := time.Now().UTC().Unix()
r := rand.New(rand.NewSource(now))
return r.Uint64()
}
// ReadPemFile reads a private pem file.
func ReadPemFile(path string) ([]byte, error) {
privKeyFile, err := os.Open(path)
if err != nil {
return []byte{}, err
}
defer privKeyFile.Close()
pemStats, err := privKeyFile.Stat()
if err != nil {
return []byte{}, err
}
log.Println("Loaded pem file", pemStats.Name())
return ioutil.ReadAll(privKeyFile)
}