diff --git a/shadowsocks/crypto/ctypes_openssl.py b/shadowsocks/crypto/ctypes_openssl.py index 88d9392..823dd90 100644 --- a/shadowsocks/crypto/ctypes_openssl.py +++ b/shadowsocks/crypto/ctypes_openssl.py @@ -114,3 +114,48 @@ ciphers = { 'aes-192-cfb1': (24, 16, CtypesCrypto), 'aes-256-cfb1': (32, 16, CtypesCrypto), } + + +def test(): + from os import urandom + import random + import time + + BLOCK_SIZE = 16384 + rounds = 1 * 1024 + plain = urandom(BLOCK_SIZE * rounds) + import M2Crypto.EVP + # cipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 1, + # key_as_bytes=0, d='md5', salt=None, i=1, + # padding=1) + # decipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 0, + # key_as_bytes=0, d='md5', salt=None, i=1, + # padding=1) + cipher = CtypesCrypto('aes-128-cfb', 'k' * 32, 'i' * 16, 1) + decipher = CtypesCrypto('aes-128-cfb', 'k' * 32, 'i' * 16, 0) + + # cipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) + # decipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) + results = [] + pos = 0 + print 'salsa20 test start' + start = time.time() + while pos < len(plain): + l = random.randint(100, 32768) + c = cipher.update(plain[pos:pos + l]) + results.append(c) + pos += l + pos = 0 + c = ''.join(results) + results = [] + while pos < len(plain): + l = random.randint(100, 32768) + results.append(decipher.update(c[pos:pos + l])) + pos += l + end = time.time() + print 'speed: %d bytes/s' % (BLOCK_SIZE * rounds / (end - start)) + assert ''.join(results) == plain + + +if __name__ == '__main__': + test() \ No newline at end of file diff --git a/shadowsocks/crypto/salsa20_ctr.py b/shadowsocks/crypto/salsa20_ctr.py index 22a8f35..3f74d59 100644 --- a/shadowsocks/crypto/salsa20_ctr.py +++ b/shadowsocks/crypto/salsa20_ctr.py @@ -112,6 +112,11 @@ class Salsa20Cipher(object): return ''.join(results) +ciphers = { + 'salsa20-ctr': (32, 8, Salsa20Cipher), +} + + def test(): from os import urandom import random @@ -149,10 +154,5 @@ def test(): assert ''.join(results) == plain -ciphers = { - 'salsa20-ctr': (32, 8, Salsa20Cipher), -} - - if __name__ == '__main__': test()