| | |
| | | |
| | | // 验证Licence, 参数: LicenceFile, public.pem 的路径 返回: 0, 成功 1, 无效 2, 设备未授权 3, 过期 |
| | | fmt.Println(licence.VerifyLicence("./LicenceFile", "./public.pem")) |
| | | |
| | | // 获取授权通道数 |
| | | fmt.Println(licence.ReadGrantChannels("./LicenceFile", "./dev-public.pem")) |
| | | } |
| | | |
| | | ``` |
| | |
| | | "io/ioutil" |
| | | "os" |
| | | "time" |
| | | "errors" |
| | | ) |
| | | |
| | | /* |
| | | 1.0.0 采集机器码, 授权日期 然后 AES 加密, 生成Licence, 秘钥会暴露给客户端 |
| | | 1.0.1 修改为RSA 非对称加密, 公钥开放. 加密内容为{注册码{机器码+公司+邮箱+手机}+过期时间+授权时间} |
| | | 1.0.2 修改Licence文件内容最终为AES加密后的内容, 然后再由公钥解密. 上个版本的bug, 仅使用非对称加密, 用户可以替换公钥 |
| | | 1.0.3 授权文件添加通道数量, 增加读取通道数量接口 |
| | | */ |
| | | type RegisterCode struct { |
| | | MachineCode string |
| | |
| | | |
| | | type Licence struct { |
| | | RegCode RegisterCode |
| | | Channel int64 |
| | | Expires int64 |
| | | Timestamp int64 |
| | | } |
| | |
| | | return AESDecodeStr(regCode, aesKey) |
| | | } |
| | | |
| | | func GenerateLicence(regCode, timeOut, privateKeyPath string) (string, error) { |
| | | func GenerateLicence(regCode, timeOut, privateKeyPath string, channel int64) (string, error) { |
| | | timeLayout := "2006-01-02 15:04:05" //转化所需模板 |
| | | loc, _ := time.LoadLocation("Local") //获取时区 |
| | | tmp, _ := time.ParseInLocation(timeLayout, timeOut, loc) |
| | |
| | | return "", err |
| | | } |
| | | |
| | | licence := Licence{registerCode, exp, now} |
| | | licence := Licence{registerCode, channel,exp, now} |
| | | json, err := json.Marshal(licence) |
| | | if err != nil { |
| | | return "", err |
| | |
| | | return _verifyLicence(licenceText) |
| | | } |
| | | |
| | | func ReadGrantChannels(licencePath, publicKeyPath string) (int64, error) { |
| | | licenceText, err := DecryptLicenceFile(licencePath, publicKeyPath) |
| | | if err != nil { |
| | | return 0 ,err |
| | | } |
| | | |
| | | var licence Licence |
| | | if err := json.Unmarshal(licenceText, &licence); err != nil { |
| | | return 0, err |
| | | } |
| | | |
| | | if _verifyLicence(licenceText) != 0 { |
| | | return 0, errors.New("Invalid licence.") |
| | | } |
| | | |
| | | return licence.Channel, nil |
| | | } |
| | | |
| | | func _verifyLicence(licenceText []byte) uint32 { |
| | | var licence Licence |
| | | var now = time.Now().Unix() |