forked from denisbrodbeck/machineid
-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper.go
69 lines (61 loc) · 1.56 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
61
62
63
64
65
66
67
68
69
package machineid
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
)
// run wraps `exec.Command` with easy access to stdout and stderr.
func run(stdout, stderr io.Writer, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
c.Stdin = os.Stdin
c.Stdout = stdout
c.Stderr = stderr
return c.Run()
}
// protect calculates HMAC-SHA256 of the application ID, keyed by the machine ID and returns a hex-encoded string.
func protect(appID, id string) string {
mac := hmac.New(sha256.New, []byte(id))
mac.Write([]byte(appID))
return hex.EncodeToString(mac.Sum(nil))
}
func readFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func writeFile(filename string, data []byte) error {
return ioutil.WriteFile(filename, data, 0644)
}
// readFirstFile tries all the pathnames listed and returns the contents of the first readable file.
func readFirstFile(pathnames []string) ([]byte, error) {
contents := []byte{}
var err error
for _, pathname := range pathnames {
if pathname != "" {
contents, err = readFile(pathname)
if err == nil {
return contents, nil
}
}
}
return contents, err
}
// writeFirstFile writes to the first file that "works" between all pathnames listed.
func writeFirstFile(pathnames []string, data []byte) error {
var err error
for _, pathname := range pathnames {
if pathname != "" {
err = writeFile(pathname, data)
if err == nil {
return nil
}
}
}
return err
}
func trim(s string) string {
return strings.TrimSpace(strings.Trim(s, "\n"))
}