use string more

This commit is contained in:
clowwindy 2015-02-03 18:09:07 +08:00
parent ce805f0aea
commit 318d88ec89
6 changed files with 57 additions and 59 deletions

View file

@ -111,31 +111,31 @@ class OpenSSLCrypto(object):
ciphers = { ciphers = {
b'aes-128-cfb': (16, 16, OpenSSLCrypto), 'aes-128-cfb': (16, 16, OpenSSLCrypto),
b'aes-192-cfb': (24, 16, OpenSSLCrypto), 'aes-192-cfb': (24, 16, OpenSSLCrypto),
b'aes-256-cfb': (32, 16, OpenSSLCrypto), 'aes-256-cfb': (32, 16, OpenSSLCrypto),
b'aes-128-ofb': (16, 16, OpenSSLCrypto), 'aes-128-ofb': (16, 16, OpenSSLCrypto),
b'aes-192-ofb': (24, 16, OpenSSLCrypto), 'aes-192-ofb': (24, 16, OpenSSLCrypto),
b'aes-256-ofb': (32, 16, OpenSSLCrypto), 'aes-256-ofb': (32, 16, OpenSSLCrypto),
b'aes-128-ctr': (16, 16, OpenSSLCrypto), 'aes-128-ctr': (16, 16, OpenSSLCrypto),
b'aes-192-ctr': (24, 16, OpenSSLCrypto), 'aes-192-ctr': (24, 16, OpenSSLCrypto),
b'aes-256-ctr': (32, 16, OpenSSLCrypto), 'aes-256-ctr': (32, 16, OpenSSLCrypto),
b'aes-128-cfb8': (16, 16, OpenSSLCrypto), 'aes-128-cfb8': (16, 16, OpenSSLCrypto),
b'aes-192-cfb8': (24, 16, OpenSSLCrypto), 'aes-192-cfb8': (24, 16, OpenSSLCrypto),
b'aes-256-cfb8': (32, 16, OpenSSLCrypto), 'aes-256-cfb8': (32, 16, OpenSSLCrypto),
b'aes-128-cfb1': (16, 16, OpenSSLCrypto), 'aes-128-cfb1': (16, 16, OpenSSLCrypto),
b'aes-192-cfb1': (24, 16, OpenSSLCrypto), 'aes-192-cfb1': (24, 16, OpenSSLCrypto),
b'aes-256-cfb1': (32, 16, OpenSSLCrypto), 'aes-256-cfb1': (32, 16, OpenSSLCrypto),
b'bf-cfb': (16, 8, OpenSSLCrypto), 'bf-cfb': (16, 8, OpenSSLCrypto),
b'camellia-128-cfb': (16, 16, OpenSSLCrypto), 'camellia-128-cfb': (16, 16, OpenSSLCrypto),
b'camellia-192-cfb': (24, 16, OpenSSLCrypto), 'camellia-192-cfb': (24, 16, OpenSSLCrypto),
b'camellia-256-cfb': (32, 16, OpenSSLCrypto), 'camellia-256-cfb': (32, 16, OpenSSLCrypto),
b'cast5-cfb': (16, 8, OpenSSLCrypto), 'cast5-cfb': (16, 8, OpenSSLCrypto),
b'des-cfb': (8, 8, OpenSSLCrypto), 'des-cfb': (8, 8, OpenSSLCrypto),
b'idea-cfb': (16, 8, OpenSSLCrypto), 'idea-cfb': (16, 8, OpenSSLCrypto),
b'rc2-cfb': (16, 8, OpenSSLCrypto), 'rc2-cfb': (16, 8, OpenSSLCrypto),
b'rc4': (16, 0, OpenSSLCrypto), 'rc4': (16, 0, OpenSSLCrypto),
b'seed-cfb': (16, 16, OpenSSLCrypto), 'seed-cfb': (16, 16, OpenSSLCrypto),
} }
@ -148,31 +148,31 @@ def run_method(method):
def test_aes_128_cfb(): def test_aes_128_cfb():
run_method(b'aes-128-cfb') run_method('aes-128-cfb')
def test_aes_256_cfb(): def test_aes_256_cfb():
run_method(b'aes-256-cfb') run_method('aes-256-cfb')
def test_aes_128_cfb8(): def test_aes_128_cfb8():
run_method(b'aes-128-cfb8') run_method('aes-128-cfb8')
def test_aes_256_ofb(): def test_aes_256_ofb():
run_method(b'aes-256-ofb') run_method('aes-256-ofb')
def test_aes_256_ctr(): def test_aes_256_ctr():
run_method(b'aes-256-ctr') run_method('aes-256-ctr')
def test_bf_cfb(): def test_bf_cfb():
run_method(b'bf-cfb') run_method('bf-cfb')
def test_rc4(): def test_rc4():
run_method(b'rc4') run_method('rc4')
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -34,15 +34,15 @@ def create_cipher(alg, key, iv, op, key_as_bytes=0, d=None, salt=None,
ciphers = { ciphers = {
b'rc4-md5': (16, 16, create_cipher), 'rc4-md5': (16, 16, create_cipher),
} }
def test(): def test():
from shadowsocks.crypto import util from shadowsocks.crypto import util
cipher = create_cipher(b'rc4-md5', b'k' * 32, b'i' * 16, 1) cipher = create_cipher('rc4-md5', b'k' * 32, b'i' * 16, 1)
decipher = create_cipher(b'rc4-md5', b'k' * 32, b'i' * 16, 0) decipher = create_cipher('rc4-md5', b'k' * 32, b'i' * 16, 0)
util.run_cipher(cipher, decipher) util.run_cipher(cipher, decipher)

View file

@ -64,9 +64,9 @@ class SodiumCrypto(object):
self.iv = iv self.iv = iv
self.key_ptr = c_char_p(key) self.key_ptr = c_char_p(key)
self.iv_ptr = c_char_p(iv) self.iv_ptr = c_char_p(iv)
if cipher_name == b'salsa20': if cipher_name == 'salsa20':
self.cipher = libsodium.crypto_stream_salsa20_xor_ic self.cipher = libsodium.crypto_stream_salsa20_xor_ic
elif cipher_name == b'chacha20': elif cipher_name == 'chacha20':
self.cipher = libsodium.crypto_stream_chacha20_xor_ic self.cipher = libsodium.crypto_stream_chacha20_xor_ic
else: else:
raise Exception('Unknown cipher') raise Exception('Unknown cipher')
@ -95,22 +95,22 @@ class SodiumCrypto(object):
ciphers = { ciphers = {
b'salsa20': (32, 8, SodiumCrypto), 'salsa20': (32, 8, SodiumCrypto),
b'chacha20': (32, 8, SodiumCrypto), 'chacha20': (32, 8, SodiumCrypto),
} }
def test_salsa20(): def test_salsa20():
cipher = SodiumCrypto(b'salsa20', b'k' * 32, b'i' * 16, 1) cipher = SodiumCrypto('salsa20', b'k' * 32, b'i' * 16, 1)
decipher = SodiumCrypto(b'salsa20', b'k' * 32, b'i' * 16, 0) decipher = SodiumCrypto('salsa20', b'k' * 32, b'i' * 16, 0)
util.run_cipher(cipher, decipher) util.run_cipher(cipher, decipher)
def test_chacha20(): def test_chacha20():
cipher = SodiumCrypto(b'chacha20', b'k' * 32, b'i' * 16, 1) cipher = SodiumCrypto('chacha20', b'k' * 32, b'i' * 16, 1)
decipher = SodiumCrypto(b'chacha20', b'k' * 32, b'i' * 16, 0) decipher = SodiumCrypto('chacha20', b'k' * 32, b'i' * 16, 0)
util.run_cipher(cipher, decipher) util.run_cipher(cipher, decipher)

View file

@ -67,7 +67,7 @@ class TableCipher(object):
ciphers = { ciphers = {
b'table': (0, 0, TableCipher) 'table': (0, 0, TableCipher)
} }
@ -163,8 +163,8 @@ def test_table_result():
def test_encryption(): def test_encryption():
from shadowsocks.crypto import util from shadowsocks.crypto import util
cipher = TableCipher(b'table', b'test', b'', 1) cipher = TableCipher('table', b'test', b'', 1)
decipher = TableCipher(b'table', b'test', b'', 0) decipher = TableCipher('table', b'test', b'', 0)
util.run_cipher(cipher, decipher) util.run_cipher(cipher, decipher)

View file

@ -22,6 +22,7 @@ import sys
import hashlib import hashlib
import logging import logging
from shadowsocks import common
from shadowsocks.crypto import rc4_md5, openssl, sodium, table from shadowsocks.crypto import rc4_md5, openssl, sodium, table
@ -46,8 +47,6 @@ def try_cipher(key, method=None):
def EVP_BytesToKey(password, key_len, iv_len): def EVP_BytesToKey(password, key_len, iv_len):
# equivalent to OpenSSL's EVP_BytesToKey() with count 1 # equivalent to OpenSSL's EVP_BytesToKey() with count 1
# so that we make the same key and iv as nodejs version # so that we make the same key and iv as nodejs version
if hasattr(password, 'encode'):
password = password.encode('utf-8')
cached_key = '%s-%d-%d' % (password, key_len, iv_len) cached_key = '%s-%d-%d' % (password, key_len, iv_len)
r = cached_keys.get(cached_key, None) r = cached_keys.get(cached_key, None)
if r: if r:
@ -95,8 +94,7 @@ class Encryptor(object):
return len(self.cipher_iv) return len(self.cipher_iv)
def get_cipher(self, password, method, op, iv): def get_cipher(self, password, method, op, iv):
if hasattr(password, 'encode'): password = common.to_bytes(password)
password = password.encode('utf-8')
m = self._method_info m = self._method_info
if m[0] > 0: if m[0] > 0:
key, iv_ = EVP_BytesToKey(password, m[0], m[1]) key, iv_ = EVP_BytesToKey(password, m[0], m[1])
@ -153,12 +151,12 @@ def encrypt_all(password, method, op, data):
CIPHERS_TO_TEST = [ CIPHERS_TO_TEST = [
b'aes-128-cfb', 'aes-128-cfb',
b'aes-256-cfb', 'aes-256-cfb',
b'rc4-md5', 'rc4-md5',
b'salsa20', 'salsa20',
b'chacha20', 'chacha20',
b'table', 'table',
] ]

View file

@ -154,11 +154,11 @@ def get_config(is_local):
elif key == '-l': elif key == '-l':
config['local_port'] = int(value) config['local_port'] = int(value)
elif key == '-s': elif key == '-s':
config['server'] = to_bytes(value) config['server'] = to_str(value)
elif key == '-m': elif key == '-m':
config['method'] = to_bytes(value) config['method'] = to_str(value)
elif key == '-b': elif key == '-b':
config['local_address'] = to_bytes(value) config['local_address'] = to_str(value)
elif key == '-v': elif key == '-v':
v_count += 1 v_count += 1
# '-vv' turns on more verbose mode # '-vv' turns on more verbose mode