| | |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "io/ioutil" |
| | | "os" |
| | | "time" |
| | |
| | | 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) |
| | |
| | | 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 |
| | |
| | | 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 |
| | | } |
| | |
| | | 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 { |