This commit is contained in:
Leo Zhu 2014-03-19 02:28:10 +00:00
commit c4168916e2
3 changed files with 8 additions and 10 deletions

View file

@ -42,11 +42,7 @@ def get_table(key):
table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i))) table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i)))
return table return table
encrypt_table = None def try_encryptor(key, method=None):
decrypt_table = None
def init_table(key, method=None):
if method == 'table': if method == 'table':
method = None method = None
if method: if method:
@ -56,7 +52,6 @@ def init_table(key, method=None):
logging.error('M2Crypto is required to use encryption other than default method') logging.error('M2Crypto is required to use encryption other than default method')
sys.exit(1) sys.exit(1)
if not method: if not method:
global encrypt_table, decrypt_table
encrypt_table = ''.join(get_table(key)) encrypt_table = ''.join(get_table(key))
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
else: else:
@ -118,6 +113,9 @@ class Encryptor(object):
self.cipher = self.get_cipher(key, method, 1, iv=random_string(32)) self.cipher = self.get_cipher(key, method, 1, iv=random_string(32))
else: else:
self.cipher = None self.cipher = None
self.encrypt_table = ''.join(get_table(key))
self.decrypt_table = string.maketrans(self.encrypt_table, string.maketrans('', ''))
def get_cipher_len(self, method): def get_cipher_len(self, method):
method = method.lower() method = method.lower()
@ -147,7 +145,7 @@ class Encryptor(object):
if len(buf) == 0: if len(buf) == 0:
return buf return buf
if self.method is None: if self.method is None:
return string.translate(buf, encrypt_table) return string.translate(buf, self.encrypt_table)
else: else:
if self.iv_sent: if self.iv_sent:
return self.cipher.update(buf) return self.cipher.update(buf)
@ -159,7 +157,7 @@ class Encryptor(object):
if len(buf) == 0: if len(buf) == 0:
return buf return buf
if self.method is None: if self.method is None:
return string.translate(buf, decrypt_table) return string.translate(buf, self.decrypt_table)
else: else:
if self.decipher is None: if self.decipher is None:
decipher_iv_len = self.get_cipher_len(self.method)[1] decipher_iv_len = self.get_cipher_len(self.method)[1]

View file

@ -235,7 +235,7 @@ def main():
utils.check_config(config) utils.check_config(config)
encrypt.init_table(KEY, METHOD) encrypt.try_encryptor(KEY, METHOD)
try: try:
if IPv6: if IPv6:

View file

@ -193,7 +193,7 @@ def main():
PORTPASSWORD = {} PORTPASSWORD = {}
PORTPASSWORD[str(PORT)] = KEY PORTPASSWORD[str(PORT)] = KEY
encrypt.init_table(KEY, METHOD) encrypt.try_encryptor(KEY, METHOD)
if IPv6: if IPv6:
ThreadingTCPServer.address_family = socket.AF_INET6 ThreadingTCPServer.address_family = socket.AF_INET6
for port, key in PORTPASSWORD.items(): for port, key in PORTPASSWORD.items():