From 80e1f059585c4446ebbbd340b1255101c7b875f3 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期五, 22 十一月 2019 14:30:39 +0800
Subject: [PATCH] fix: add decrypt Licence code & file api
---
licence.go | 182 +++++++++++++++++++++++++++++++++-----------
1 files changed, 135 insertions(+), 47 deletions(-)
diff --git a/licence.go b/licence.go
index ab955be..f59f359 100644
--- a/licence.go
+++ b/licence.go
@@ -2,86 +2,174 @@
import (
"encoding/json"
- "strings"
+ "fmt"
+ "io/ioutil"
+ "os"
"time"
-
- "github.com/shirou/gopsutil/cpu"
- // "github.com/shirou/gopsutil/disk"
- "github.com/shirou/gopsutil/host"
)
-const key = "flzxsqc,ysyhljt."
+/*
+ 1.0.0 閲囬泦鏈哄櫒鐮�, 鎺堟潈鏃ユ湡 鐒跺悗 AES 鍔犲瘑, 鐢熸垚Licence, 绉橀挜浼氭毚闇茬粰瀹㈡埛绔�
+ 1.0.1 淇敼涓篟SA 闈炲绉板姞瀵�, 鍏挜寮�鏀�. 鍔犲瘑鍐呭涓簕娉ㄥ唽鐮亄鏈哄櫒鐮�+鍏徃+閭+鎵嬫満}+杩囨湡鏃堕棿+鎺堟潈鏃堕棿}
+ 1.0.2 淇敼Licence鏂囦欢鍐呭鏈�缁堜负AES鍔犲瘑鍚庣殑鍐呭, 鐒跺悗鍐嶇敱鍏挜瑙e瘑. 涓婁釜鐗堟湰鐨刡ug, 浠呬娇鐢ㄩ潪瀵圭О鍔犲瘑, 鐢ㄦ埛鍙互鏇挎崲鍏挜
+*/
+type RegisterCode struct {
+ MachineCode string
+ Company string
+ Email string
+ Phone string
+ Version string
+}
type Licence struct {
- MachineCode string
- Expires int64
+ RegCode RegisterCode
+ Expires int64
+ Timestamp int64
}
const (
- ValidationErrorMalformed uint32 = iota + 1 // Licence is malformed
- ValidationErrorUnverifiableHost // Licence could not be verified because of signing problems
- ValidationErrorExpired // Signature validation failed
+ ValidationErrorDecrypt uint32 = iota + 1 // Licence is malformed
+ ValidationErrorUnverifiableHost // Host unauthorized
+ ValidationErrorExpired // Signature expired
+ ValidationErrorMalformed
+
+ Version = "1.0.2"
+ aesKey = "www.aiotlink.com"
)
-func GetMachineCode() string {
- var machineCode string
+func GetRegisterCode(company, email, phone string) string {
+ mCode := GetMachineCode()
- // CPU
- if cpu, err := cpu.Info(); err == nil {
- for _, c := range cpu {
- strings.Join([]string{machineCode, c.String()}, "-")
- }
- }
+ regCode := RegisterCode{mCode, company, email, phone, Version}
- // // Disk
- // if diskInfo, err := disk.Partitions(false); err == nil {
- // for _, d := range diskInfo {
- // diskSerialNumber := disk.GetDiskSerialNumber(d.Device)
- // strings.Join([]string{machineCode, diskSerialNumber}, "-")
- // }
- // }
+ json, _ := json.Marshal(regCode)
- // Host
- if host, err := host.Info(); err == nil {
- strings.Join([]string{machineCode, host.HostID}, "-")
- }
-
- return GetMd5String(machineCode, true, false)
+ return AESEncodeStr(json, aesKey)
}
-func GenerateLicence(machineCode, timeOut, key string) string {
+func DecryptRegisterCode(regCode string) []byte {
+ return AESDecodeStr(regCode, aesKey)
+}
+
+func GenerateLicence(regCode, timeOut, privateKeyPath string) (string, error) {
timeLayout := "2006-01-02 15:04:05" //杞寲鎵�闇�妯℃澘
loc, _ := time.LoadLocation("Local") //鑾峰彇鏃跺尯
tmp, _ := time.ParseInLocation(timeLayout, timeOut, loc)
- timestamp := tmp.Unix()
+ exp := tmp.Unix()
+ now := time.Now().Unix()
- licence := Licence{machineCode, timestamp}
+ regCodeText := AESDecodeStr(regCode, aesKey)
+ var registerCode RegisterCode
+ if err := json.Unmarshal(regCodeText, ®isterCode); err != nil {
+ return "", err
+ }
- json, _ := json.Marshal(licence)
- return AESEncodeStr(json, key)
+ licence := Licence{registerCode, exp, now}
+ json, err := json.Marshal(licence)
+ if err != nil {
+ return "", err
+ }
+
+ fd, err := os.Open(privateKeyPath)
+ if err != nil {
+ fmt.Println(err)
+ return "", err
+ }
+
+ defer fd.Close()
+ privateKey, err := ioutil.ReadAll(fd)
+ if err != nil {
+ fmt.Println(err)
+ return "", err
+ }
+
+ RSA := &RSASecurity{}
+ if err := RSA.SetPrivateKey(privateKey); err != nil {
+ return "", err
+ }
+
+ licenceHex, err := RSA.PriKeyENCTYPT(json)
+ if err != nil {
+ return "", err
+ }
+
+ return AESEncodeStr(licenceHex, aesKey), nil
}
-func VerifyLicence(licenceCode string) uint32 {
- decodeData := AESDecodeStr(licenceCode, key)
- if decodeData == nil {
- return ValidationErrorMalformed
+func DecryptLicenceFile(licencePath, publicKeyPath string) ([]byte, error) {
+ // 璇诲彇Licence File
+ fdLic, err := os.Open(licencePath)
+ if err != nil {
+ return nil, err
+ }
+ defer fdLic.Close()
+
+ licenceCode, err := ioutil.ReadAll(fdLic)
+ if err != nil {
+ return nil, err
}
+ return DecryptLicence(string(licenceCode), publicKeyPath)
+}
+
+func DecryptLicence(licenceCode, publicKeyPath string) ([]byte, error) {
+ // 璇诲彇鍏挜
+ fdPub, err := os.Open(publicKeyPath)
+ if err != nil {
+ return nil, err
+ }
+ defer fdPub.Close()
+
+ publicKey, err := ioutil.ReadAll(fdPub)
+ if err != nil {
+ return nil, err
+ }
+
+ RSA := &RSASecurity{}
+ if err := RSA.SetPublicKey(publicKey); err != nil {
+ return nil, err
+ }
+
+ licenceHex := AESDecodeStr(licenceCode, aesKey)
+
+ return RSA.PubKeyDECRYPT(licenceHex)
+}
+
+func VerifyLicenceFile(licencePath, publicKeyPath string) uint32 {
+ licenceText, err := DecryptLicenceFile(licencePath, publicKeyPath)
+ if err != nil {
+ return ValidationErrorDecrypt
+ }
+ return _verifyLicence(licenceText)
+}
+
+func VerifyLicence(licenceCode, publicKeyPath string) uint32 {
+ licenceText, err := DecryptLicence(licenceCode, publicKeyPath)
+ if err != nil {
+ return ValidationErrorDecrypt
+ }
+
+ return _verifyLicence(licenceText)
+}
+
+func _verifyLicence(licenceText []byte) uint32 {
var licence Licence
- if err := json.Unmarshal(decodeData, &licence); err != nil {
+ if err := json.Unmarshal(licenceText, &licence); err != nil {
return ValidationErrorMalformed
}
- code := GetMachineCode()
- if licence.MachineCode != code {
- return ValidationErrorUnverifiableHost
- }
-
+ // 鍒ゆ柇杩囨湡
now := time.Now().Unix()
if now > licence.Expires {
return ValidationErrorExpired
}
+ // 鍒ゆ柇鏈哄櫒鐮�
+ mCode := GetMachineCode()
+ if licence.RegCode.MachineCode != mCode {
+ return ValidationErrorUnverifiableHost
+ }
+
return 0
}
--
Gitblit v1.8.0