38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
ResLib LIC 授权算法
|
||
规则(见开发技术文档 V1.2 附录B):
|
||
MAC "AA-BB-CC-DD-EE-FF" -> 去横线/冒号/空格 -> "AABBCCDDEEFF"
|
||
-> 每字符映射 A=01 B=02 ... Z=26(数字 0-9 映射为 00-09)
|
||
-> 拼接后倒序 -> LIC 文件内容
|
||
"""
|
||
import re
|
||
|
||
|
||
def normalize_mac(mac: str) -> str:
|
||
mac = mac.strip().upper()
|
||
mac = re.sub(r"[-:.\s]", "", mac)
|
||
if not mac or len(mac) != 12:
|
||
raise ValueError("MAC 地址无效,应为 12 位十六进制(如 AA-BB-CC-DD-EE-FF)")
|
||
if not re.fullmatch(r"[0-9A-F]{12}", mac):
|
||
raise ValueError("MAC 地址包含非法字符,仅允许 0-9、A-F")
|
||
return mac
|
||
|
||
|
||
def mac_to_code(mac: str) -> str:
|
||
"""逐字符映射:A=01 B=02 ... Z=26,数字 0-9 -> 00-09"""
|
||
out = []
|
||
for ch in mac:
|
||
if ch.isdigit():
|
||
out.append(f"{int(ch):02d}")
|
||
else:
|
||
out.append(f"{ord(ch) - ord('A') + 1:02d}")
|
||
return "".join(out)
|
||
|
||
|
||
def generate_lic(mac: str) -> str:
|
||
"""生成 LIC 文件内容(倒序)"""
|
||
norm = normalize_mac(mac)
|
||
code = mac_to_code(norm)
|
||
return code[::-1]
|