zhangzengfei
2019-11-22 80e1f059585c4446ebbbd340b1255101c7b875f3
licence.go
@@ -2,6 +2,7 @@
import (
   "encoding/json"
   "fmt"
   "io/ioutil"
   "os"
   "time"
@@ -50,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)
@@ -69,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
@@ -82,31 +96,31 @@
   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
   }
@@ -116,17 +130,29 @@
      return nil, err
   }
   licenceHex := AESDecodeStr(string(licenceCode), aesKey)
   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 {