-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
83 lines (72 loc) · 1.76 KB
/
rand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package rand
import (
"crypto/rand"
"fmt"
"io"
"net"
"strings"
)
// Common sets for random strings
const (
Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Lowercase = "abcdefghijklmnopqrstuvwxyz"
Alpha = Uppercase + Lowercase
Digit = "0123456789"
AlphaNumeric = Alpha + Digit
Punctuation = "!@#$%^&*~?=-+_./|"
Password = AlphaNumeric + Punctuation
)
// Cached MAC address
var macaddr net.HardwareAddr
func init() {
// search our network interfaces for a hardware MAC address
if interfaces, err := net.Interfaces(); err == nil {
for _, i := range interfaces {
if (i.Flags&net.FlagLoopback) == 0 && len(i.HardwareAddr) > 0 {
macaddr = i.HardwareAddr
break
}
}
}
// if we failed to obtain the MAC address of the current computer, we will
// use a randomly generated 6 byte sequence instead and set the multicast
// bit as recommended in RFC 4122.
if macaddr == nil {
macaddr = make(net.HardwareAddr, 6)
randomBytes(macaddr)
macaddr[0] = macaddr[0] | 0x01
}
}
// Obtain the current host's MAC address
func HardwareAddr() net.HardwareAddr {
return macaddr
}
// Obtain the current host's MAC address as a hex string
func HardwareKey() string {
return fmt.Sprintf("%x", macaddr)
}
func RandomBytes(n int) []byte {
b := make([]byte, n)
randomBytes(b)
return b
}
func ReadRandom(b []byte) {
randomBytes(b)
}
func randomBytes(b []byte) {
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err.Error()) // rand should never fail
}
}
func RandomString(n int) string {
return RandomStringFromSet(n, AlphaNumeric)
}
func RandomStringFromSet(n int, set string) string {
b := RandomBytes(n)
l := len(set)
s := &strings.Builder{}
for i := 0; i < len(b); i++ {
s.WriteRune(rune(set[int(b[i])%l]))
}
return s.String()
}