-
Notifications
You must be signed in to change notification settings - Fork 0
/
license.go
95 lines (78 loc) · 1.92 KB
/
license.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
package license
import (
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"github.com/super-l/machine-code/machine"
"strings"
"time"
)
type License struct {
Product string `json:"product"` //产品
Domain string `json:"domain,omitempty"` //域名,用于前端,支持多个
MachineID string `json:"machine_id,omitempty"` //机器ID,用于后端
ExpireAt time.Time `json:"expire_at,omitempty"` //失效期
Signature string `json:"signature,omitempty"` //签名
}
func (l *License) Stringify() string {
buf, _ := json.Marshal(l)
return base64.StdEncoding.EncodeToString(buf)
}
func (l *License) Parse(lic string) error {
buf, err := base64.StdEncoding.DecodeString(lic)
if err != nil {
return err
}
return json.Unmarshal(buf, l)
}
func (l *License) Serialize() string {
var ss []string
ss = append(ss, "p:", l.Product, "\n")
ss = append(ss, "d:", l.Domain, "\n")
ss = append(ss, "m:", l.MachineID, "\n")
ss = append(ss, "e:", l.ExpireAt.Format(time.DateTime))
return strings.Join(ss, "")
}
func (l *License) Sign(privateKey string) error {
//序列化
msg := l.Serialize()
key, err := hex.DecodeString(privateKey)
if err != nil {
return err
}
sign := ed25519.Sign(key, []byte(msg))
l.Signature = hex.EncodeToString(sign)
return nil
}
func (l *License) Verify(publicKey string) error {
//序列化
msg := l.Serialize()
key, err := hex.DecodeString(publicKey)
if err != nil {
return err
}
sign, err := hex.DecodeString(l.Signature)
if err != nil {
return err
}
ret := ed25519.Verify(key, []byte(msg), sign)
if !ret {
return errors.New("签名错误")
}
return nil
}
func (l *License) Expired() bool {
return time.Now().After(l.ExpireAt)
}
func (l *License) Validate() error {
cpuid, err := machine.GetCpuSerialNumber()
if err != nil {
return err
}
if l.MachineID != cpuid {
return errors.New("机器码错误")
}
return nil
}