-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
179 lines (165 loc) · 4.59 KB
/
id.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// package hardwareid provides support for reading the unique hardware id of most OSs (without admin privileges).
//
// https://github.com/sandipmavani/hardwareid
//
// https://godoc.org/github.com/sandipmavani/hardwareid/cmd/hardwareid
//
// This package is Cross-Platform (tested on Win7+, Debian 8+, Ubuntu 14.04+, OS X 10.6+, FreeBSD 11+)
// and does not use any internal hardware IDs (no MAC, BIOS, or CPU).
//
// Returned hardware IDs are generally stable for the OS installation
// and usually stay the same after updates or hardware changes.
//
// This package allows sharing of hardware IDs in a secure way by
// calculating HMAC-SHA256 over a user provided app ID, which is keyed by the hardware id.
//
// Caveat: Image-based environments have usually the same hardware-id (perfect clone).
// Linux users can generate a new id with `dbus-uuidgen` and put the id into
// `/var/lib/dbus/hardware-id` and `/etc/hardware-id`.
// Windows users can use the `sysprep` toolchain to create images, which produce valid images ready for distribution.
package hardwareid // import "github.com/sandipmavani/hardwareid"
import (
"crypto/md5"
"errors"
"fmt"
"net"
"os/exec"
"runtime"
"strings"
)
// ID returns the platform specific hardware id of the current host OS.
// Regard the returned id as "confidential" and consider using ProtectedID() instead.
func ID() (string, error) {
id, err := hardwareId()
if err != nil {
return "", fmt.Errorf("hardwareid: %v", err)
}
return id, nil
}
func physicalId() (string, error) {
id, err := GetPhysicalId()
if err != nil {
return "", fmt.Errorf("hardwareid: %v", err)
}
return id, nil
}
// ProtectedID returns a hashed version of the hardware ID in a cryptographically secure way,
// using a fixed, application-specific key.
// Internally, this function calculates HMAC-SHA256 of the application ID, keyed by the hardware ID.
func ProtectedID(appID string) (string, error) {
id, err := ID()
if err != nil {
return "", fmt.Errorf("hardwareid: %v", err)
}
return protect(appID, id), nil
}
func hardwareId() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, i := range ifaces {
if i.HardwareAddr.String() != "" {
return i.HardwareAddr.String(), nil
}
}
return "", nil
}
func Md5(data []byte) string {
has := md5.Sum(data)
md5Str := fmt.Sprintf("%X", has)
return md5Str
}
func GetPhysicalId() (string, error) {
biosSn, err := GetBIOSSerialNumber()
if err != nil {
return "", err
}
diskSn, err := GetDiskDriverSerialNumber()
if err != nil {
return "", err
}
cpuId, err := GetCPUPorcessorID()
if err != nil {
return "", err
}
return strings.ToUpper(Md5([]byte(biosSn + diskSn + cpuId))), nil
}
//获取硬盘SerialNumber
func GetDiskDriverSerialNumber() (string, error) {
switch runtime.GOOS {
case "windows":
return diskDriverSerialNumberOnWindows()
case "linux":
return "", errors.New("unsupported os")
case "darwin":
return "", errors.New("unsupported os")
default:
return "", errors.New("unknown os")
}
}
func diskDriverSerialNumberOnWindows() (string, error) {
cmd := exec.Command("CMD", "/C", "WMIC DISKDRIVE GET SERIALNUMBER")
serialNo, err := cmd.Output()
if err != nil {
return "", err
}
l := strings.Split(string(serialNo), "\n")
if len(l) >= 2 {
return l[1], nil
} else {
return "", errors.New("return split length less 2")
}
}
//获取硬盘SerialNumber
func GetBIOSSerialNumber() (string, error) {
switch runtime.GOOS {
case "windows":
return biosSerialNumberOnWindows()
case "linux":
return "", errors.New("unsupported os")
case "darwin":
return "", errors.New("unsupported os")
default:
return "", errors.New("unknown os")
}
}
func biosSerialNumberOnWindows() (string, error) {
cmd := exec.Command("CMD", "/C", "WMIC BIOS GET SERIALNUMBER")
serialNo, err := cmd.Output()
if err != nil {
return "", err
}
l := strings.Split(string(serialNo), "\n")
if len(l) >= 2 {
return l[1], nil
} else {
return "", errors.New("return split length less 2")
}
}
//获取CPU PorcessorID
func GetCPUPorcessorID() (string, error) {
switch runtime.GOOS {
case "windows":
return cpuPorcessorIDOnWindows()
case "linux":
return "", errors.New("unsupported os")
case "darwin":
return "", errors.New("unsupported os")
default:
return "", errors.New("unknown os")
}
}
func cpuPorcessorIDOnWindows() (string, error) {
cmd := exec.Command("CMD", "/C", "WMIC CPU GET ProcessorID")
serialNo, err := cmd.Output()
if err != nil {
return "", err
}
l := strings.Split(string(serialNo), "\n")
if len(l) >= 2 {
return l[1], nil
} else {
return "", errors.New("return split length less 2")
}
}