12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from hub import methods, Global
- class Key(object):
- @classmethod
- def create_key(cls, **params):
- """
- 生成key
- """
- # --- check ---
- key_path = '/home/server/key'
- if methods.is_file(key_path):
- methods.remove_file(key_path)
- # --- get serial ---
- ssh = Global.SSHClient('172.18.0.1', 22, 'server', 'server')
- serial = ssh.run_command("cat /sys/firmware/devicetree/base/serial-number").strip('\x00')
- # --- generate salt ---
- salt = methods.now_ts()
- salt = methods.reverse(str(salt))
- salt = 0
- # --- generate key ---
- keys = methods.text_to_b64(f"{serial}|{salt}")
- # --- storage env_key ---
- methods.run_command(f"echo \"{keys}\" > {key_path}", callback=True)
- return dict(code=0, data=keys)
- @classmethod
- def verify_key(cls, **params):
- """
- 验证key
- """
- # --- check ---
- if not methods.is_file('/home/server/key'):
- return dict(code=1, is_verified=False)
- # --- get key ---
- keys = methods.read_text('/home/server/key')
- keys = methods.b64_to_text(keys)
- # --- check again ---
- if not keys:
- return dict(code=2, is_verified=False)
- if len(keys.split('|')) != 2:
- return dict(code=3, is_verified=False)
- serial_1, _ = keys.split('|')
- # --- check again ---
- ssh = Global.SSHClient('172.18.0.1', 22, 'server', 'server')
- serial_2 = ssh.run_command("cat /sys/firmware/devicetree/base/serial-number").strip('\x00')
- if serial_1 != serial_2:
- return dict(code=4, is_verified=False)
- return dict(code=0, is_verified=True)
|