clowwindy 10 years ago
parent
commit
d6a5c567ae
  1. 45
      shadowsocks/crypto/ctypes_openssl.py
  2. 10
      shadowsocks/crypto/salsa20_ctr.py

45
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()

10
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()

Loading…
Cancel
Save