1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# !/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Created on 2022/2/18 15:05
@author: syy_ysk
'''
import uuid
import hashlib
import datetime
import subprocess
from license.aes import get_aes
class LicenseHelper(object):
def generate_license(self, end_date, mac_addr):
print("Received end_date: {}, mac_addr: {}".format(end_date, mac_addr))
psw = self.hash_msg('shly_ymcc' + str(mac_addr))
license_str = {}
license_str['mac'] = mac_addr
license_str['time_str'] = end_date
license_str['psw'] = psw
s = str(license_str)
licence_result = get_aes().encrypt(s)
return licence_result
def get_mac_address(self):
# windows 下获取磁盘uuid
file = subprocess.Popen("wmic csproduct list full | findstr UUID", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 使用管道
UUID = str(file.stdout.read()[5:-1], encoding='utf-8')
UUID = UUID.replace('\r', '')# 获取输出结果
# print(UUID)
# file = os.system("wmic csproduct list full | findstr UUID")
# UUID = file[5:-1].replace('\n', '').replace('\r', '')
# linux 下获取磁盘uuid
# file = subprocess.Popen("blkid", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 使用管道
# UUID = str(file.stdout.read(), encoding='utf-8')
# UUID = UUID.split("\"")[1] # 获取输出结果
return UUID
# mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
def hash_msg(self, msg):
sha256 = hashlib.sha256()
sha256.update(msg.encode('utf-8'))
res = sha256.hexdigest()
return res
def read_license(self, license_result):
lic_msg = bytes(license_result, encoding="utf8")
license_str = get_aes().decrypt(lic_msg)
license_dic = eval(license_str)
return license_dic
def check_license_date(self, lic_date):
current_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
current_time_array = datetime.datetime.strptime(current_time, "%Y-%m-%d %H:%M:%S")
lic_date_array = datetime.datetime.strptime(lic_date, "%Y-%m-%d %H:%M:%S")
remain_days = lic_date_array - current_time_array
remain_days = remain_days.days
if remain_days < 0 or remain_days == 0:
return False
else:
return True
def check_license_psw(self, psw):
mac_addr = self.get_mac_address()
hashed_msg = self.hash_msg('shly_ymcc' + str(mac_addr))
if psw == hashed_msg:
return True
else:
return False
oper = LicenseHelper()
def verify(license):
try:
license_dic = oper.read_license(license)
date_bool = oper.check_license_date(license_dic['time_str'])
psw_bool = oper.check_license_psw(license_dic['psw'])
if psw_bool:
if date_bool:
result = "成功"
else:
result = "激活码过期"
else:
result = "MAC不匹配, License无效, 请更换License"
except:
result = "读取失败, 无效的License"
return result
if __name__ == '__main__':
import os
license_lic = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
oper = LicenseHelper()
mac = oper.get_mac_address()
#mac = 'fff8524C-2F81-11B2-A85C-93AC6936373A'
s = oper.generate_license('2023-02-26 16:30:00', mac)
# print(s)
# print(os.path.join(license_lic, "license.lic"))
with open(os.path.join(license_lic, "license.lic"), "w+") as licFile:
licFile.write(oper.generate_license('2093-03-26 15:50:00', mac))
licFile.close()
print("ok")
print(verify(s))
s = oper.check_license_psw('60fea10e710aebfabc2c52f9e6e9ca19994c3d269260e9782406def9c573f76f')
print(s)
|