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 |   52 +++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/licence.go b/licence.go
index 2bdb2d8..f59f359 100644
--- a/licence.go
+++ b/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 {

--
Gitblit v1.8.0