MCPcopy
hub / github.com/shadowsocks/shadowsocks / Encryptor

Class Encryptor

shadowsocks/encrypt.py:71–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

69
70
71class Encryptor(object):
72 def __init__(self, password, method):
73 self.password = password
74 self.key = None
75 self.method = method
76 self.iv_sent = False
77 self.cipher_iv = b''
78 self.decipher = None
79 self.decipher_iv = None
80 method = method.lower()
81 self._method_info = self.get_method_info(method)
82 if self._method_info:
83 self.cipher = self.get_cipher(password, method, 1,
84 random_string(self._method_info[1]))
85 else:
86 logging.error('method %s not supported' % method)
87 sys.exit(1)
88
89 def get_method_info(self, method):
90 method = method.lower()
91 m = method_supported.get(method)
92 return m
93
94 def iv_len(self):
95 return len(self.cipher_iv)
96
97 def get_cipher(self, password, method, op, iv):
98 password = common.to_bytes(password)
99 m = self._method_info
100 if m[0] > 0:
101 key, iv_ = EVP_BytesToKey(password, m[0], m[1])
102 else:
103 # key_length == 0 indicates we should use the key directly
104 key, iv = password, b''
105 self.key = key
106 iv = iv[:m[1]]
107 if op == 1:
108 # this iv is for cipher not decipher
109 self.cipher_iv = iv[:m[1]]
110 return m[2](method, key, iv, op)
111
112 def encrypt(self, buf):
113 if len(buf) == 0:
114 return buf
115 if self.iv_sent:
116 return self.cipher.update(buf)
117 else:
118 self.iv_sent = True
119 return self.cipher_iv + self.cipher.update(buf)
120
121 def decrypt(self, buf):
122 if len(buf) == 0:
123 return buf
124 if self.decipher is None:
125 decipher_iv_len = self._method_info[1]
126 decipher_iv = buf[:decipher_iv_len]
127 self.decipher_iv = decipher_iv
128 self.decipher = self.get_cipher(self.password, self.method, 0,

Callers 2

try_cipherFunction · 0.85
test_encryptorFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_encryptorFunction · 0.68