/
helper.go
60 lines (49 loc) · 1.13 KB
/
helper.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
package thirdparty
import (
"crypto/md5"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/google/uuid"
"github.com/shirou/gopsutil/v3/mem"
)
func GenerateUuid() string {
id, _ := uuid.NewRandom()
return id.String()
}
func GetVirtualMemory() uint64 {
v, _ := mem.VirtualMemory()
return v.Total
}
func ReadFile(fileName string) ([]byte, error) {
f, _ := os.Open(fileName)
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return data, nil
}
func ReadKeyFile(filePath string) string {
f, _ := os.Open(filePath)
keyData, err := ioutil.ReadAll(f)
if err != nil {
log.Printf("ERROR: fail get idrsa, %s", err.Error())
os.Exit(1)
}
keyBlock, _ := pem.Decode(keyData)
if keyBlock == nil {
log.Printf("ERROR: fail get idrsa, invalid key")
os.Exit(1)
}
// encode base64 key data
return base64.StdEncoding.EncodeToString(keyBlock.Bytes)
}
func DigestString(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}
func GenerateFileName(timestamp int64, operationType string, extenstion string) string {
return fmt.Sprint(timestamp) + "_" + operationType + extenstion
}