-
Notifications
You must be signed in to change notification settings - Fork 278
/
util.go
53 lines (48 loc) · 999 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
50
51
52
53
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zmssvctoken
import (
"crypto/rand"
"fmt"
"net"
"os"
)
// getSalt returns a random salt
func getSalt() (string, error) {
buf := make([]byte, 8)
_, err := rand.Read(buf)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", buf), nil
}
// defaultHostname returns a default value for hostname
// or an empty string in case of errors
func defaultHostname() string {
h, err := os.Hostname()
if err != nil {
return ""
}
return h
}
// defaultIP returns a default IP address
// or an empty string in case of errors
func defaultIP() string {
hostname := defaultHostname()
if hostname != "" {
addrs, err := net.LookupHost(hostname)
if err == nil {
for _, addr := range addrs {
ip := net.ParseIP(addr)
if ip.To4() == nil {
continue
}
if ip.IsLoopback() {
continue
}
return ip.String()
}
}
}
return ""
}