zhangzengfei
2020-03-18 66c18a0d59c71a672318b86f0a57e4ea44ca4fe0
licence.go
@@ -1,13 +1,18 @@
package licence
import (
   "encoding/base64"
   "encoding/json"
   "fmt"
   "io/ioutil"
   "os"
   "time"
)
/*
   1.0.0 采集机器码, 授权日期 然后 AES 加密, 生成Licence, 秘钥会暴露给客户端
   1.0.1 修改为RSA 非对称加密, 公钥开放. 加密内容为{注册码{机器码+公司+邮箱+手机}+过期时间+授权时间}
   1.0.2 修改Licence文件内容最终为AES加密后的内容, 然后再由公钥解密. 上个版本的bug, 仅使用非对称加密, 用户可以替换公钥
*/
type RegisterCode struct {
   MachineCode string
   Company     string
@@ -28,7 +33,7 @@
   ValidationErrorExpired                            // Signature expired
   ValidationErrorMalformed
   Version = "1.0.1"
   Version = "1.0.2"
   aesKey  = "www.aiotlink.com"
)
@@ -46,7 +51,7 @@
   return AESDecodeStr(regCode, aesKey)
}
func GenerateLicence(regCode, timeOut string, privateKey []byte) (string, error) {
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)
@@ -65,6 +70,19 @@
      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
@@ -75,36 +93,34 @@
      return "", err
   }
   licenceText := base64.StdEncoding.EncodeToString(licenceHex)
   return licenceText, nil
   return AESEncodeStr(licenceHex, aesKey), nil
}
func DecryptLicence(licencePath, publicKeyPath string) ([]byte, error) {
   var publicKey, licenceCode []byte
   var fdLic, fdPub *os.File
   var err error
func DecryptLicenceFile(licencePath, publicKeyPath string) ([]byte, error) {
   // 读取Licence File
   fdLic, err = os.Open(licencePath)
   fdLic, err := os.Open(licencePath)
   if err != nil {
      return nil, err
   }
   defer fdLic.Close()
   licenceCode, err = ioutil.ReadAll(fdLic)
   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)
   fdPub, err := os.Open(publicKeyPath)
   if err != nil {
      return nil, err
   }
   defer fdPub.Close()
   publicKey, err = ioutil.ReadAll(fdPub)
   publicKey, err := ioutil.ReadAll(fdPub)
   if err != nil {
      return nil, err
   }
@@ -114,17 +130,29 @@
      return nil, err
   }
   licenceHex, _ := base64.StdEncoding.DecodeString(string(licenceCode))
   licenceHex := AESDecodeStr(licenceCode, aesKey)
   return RSA.PubKeyDECRYPT(licenceHex)
}
func VerifyLicence(licencePath, publicKeyPath string) uint32 {
   licenceText, err := DecryptLicence(licencePath, publicKeyPath)
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(licenceText, &licence); err != nil {