-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkOrderUtil.py
193 lines (156 loc) · 4.66 KB
/
WorkOrderUtil.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年1月3日
@author: Irony
@site: http://github.com/892768447
@email: 892768447@qq.com
@file: WorkOrderUtil
@description:
"""
import base64
import json
import os
from random import randint
import traceback
from tornado.concurrent import run_on_executor
from tornado.ioloop import IOLoop
from tornado.options import options
import xxtea # @UnresolvedImport
__Author__ = 'By: Irony\nQQ: 892768447\nEmail: 892768447@qq.com'
__Copyright__ = 'Copyright (c) ${year} Irony'
__Version__ = 1.0
class ResultCode(int):
def __new__(self, code, value):
obj = super(ResultCode, self).__new__(self, code)
obj._value = value
return obj
@property
def value(self):
return self._value
class AsyncUtil:
_threadpool = None # type: ignore
_threadpool_pid = None # type: int
def __init__(self, io_loop=None):
self.executor = AsyncUtil._create_threadpool(options.pool_size)
self.io_loop = io_loop or IOLoop.current()
@run_on_executor
def run(self, func, *args, **kwargs):
try:
ret = func(*args, **kwargs)
return ret
except Exception as e:
return str(e), None
@classmethod
def shutdown(cls):
if cls._threadpool != None:
cls._threadpool.shutdown()
@classmethod
def _create_threadpool(cls, num_threads):
pid = os.getpid()
if cls._threadpool_pid != pid:
cls._threadpool = None
if cls._threadpool is None:
from concurrent.futures import ThreadPoolExecutor
cls._threadpool = ThreadPoolExecutor(num_threads)
cls._threadpool_pid = pid
return cls._threadpool
def encrypt(data):
"""
:param data: str or bytes
"""
try:
key = os.urandom(16)
plen = randint(1, 9)
padding = os.urandom(plen)
# key + padding len + padding + data
return key + hex(plen).encode() + padding + xxtea.encrypt(data, key)
except:
traceback.print_exc()
return b''
def decrypt(data):
"""
:param data: str or bytes
"""
try:
data = data.encode() if isinstance(data, str) else data
key = data[:16]
offset = 16 + 3 + int(data[16:19].decode(), 16)
return xxtea.decrypt(data[offset:], key)
except:
traceback.print_exc()
return b''
def encrypt_to_base64(data):
"""
:param data: str or bytes
"""
try:
return base64.a85encode(encrypt(data))
except:
traceback.print_exc()
return b''
def encrypt_to_base64_to_str(data):
"""
:param data: str or bytes
"""
if not data:
return ''
try:
return encrypt_to_base64(data).decode()
except:
traceback.print_exc()
return ''
def decrypt_to_str(data):
"""
:param data: str or bytes
"""
return decrypt(data).decode()
def decrypt_from_base64(data):
"""
:param data: str or bytes
"""
try:
return decrypt(base64.a85decode(data.encode() if isinstance(data, str) else data))
except:
traceback.print_exc()
return b''
def decrypt_from_base64_to_str(data):
"""
:param data: str or bytes
"""
if not data:
return ''
return decrypt_from_base64(data).decode()
class WorkOrderResult(dict):
def __init__(self, code=200, msg='', data='', version=1.0):
super(WorkOrderResult, self).__init__(
code=code, msg=msg,
data=encrypt_to_base64_to_str(json.dumps(data, separators=(
',', ':')) if isinstance(data, dict) or isinstance(data, list) else data),
version=version
)
if __name__ == '__main__':
code = ResultCode(0, '错误')
print(code, code.value)
##
result = WorkOrderResult(data=b'def')
print(result, decrypt_from_base64(result.get('data')))
print(json.dumps(result))
result = WorkOrderResult(data='abc')
print(result, decrypt_from_base64_to_str(result.get('data')))
print(json.dumps(result))
data = 'test'
print('**encrypt test**')
a = encrypt(data)
b = encrypt_to_base64(data)
c = encrypt_to_base64_to_str(data)
print('encrypt:', a)
print('encrypt_to_base64:', b)
print('encrypt_to_base64_to_str:', c)
print('**decrypt test**')
a = decrypt(a)
b = decrypt_from_base64(b)
c = decrypt_from_base64_to_str(c)
print('decrypt:', a)
print('decrypt_from_base64:', b)
print('decrypt_from_base64_to_str:', c)