Browse Source

fix salsa20 on python 3

auth
clowwindy 10 years ago
parent
commit
ede2b1b120
  1. 4
      shadowsocks/crypto/ctypes_openssl.py
  2. 25
      shadowsocks/crypto/salsa20_ctr.py

4
shadowsocks/crypto/ctypes_openssl.py

@ -163,8 +163,8 @@ def test():
# decipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 0, # decipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 0,
# key_as_bytes=0, d='md5', salt=None, i=1, # key_as_bytes=0, d='md5', salt=None, i=1,
# padding=1) # padding=1)
cipher = CtypesCrypto('aes-128-cfb', 'k' * 32, 'i' * 16, 1) cipher = CtypesCrypto('aes-128-cfb', b'k' * 32, b'i' * 16, 1)
decipher = CtypesCrypto('aes-128-cfb', 'k' * 32, 'i' * 16, 0) decipher = CtypesCrypto('aes-128-cfb', b'k' * 32, b'i' * 16, 0)
# cipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) # cipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1)
# decipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) # decipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1)

25
shadowsocks/crypto/salsa20_ctr.py

@ -72,9 +72,16 @@ def numpy_xor(a, b):
def py_xor_str(a, b): def py_xor_str(a, b):
c = [] c = []
for i in range(0, len(a)): if bytes == str:
c.append(chr(ord(a[i]) ^ ord(b[i]))) for i in range(0, len(a)):
return ''.join(c) c.append(chr(ord(a[i]) ^ ord(b[i])))
else:
for i in range(0, len(a)):
c.append(a[i] ^ b[i])
if bytes == str:
return ''.join(c)
else:
return bytes(c)
class Salsa20Cipher(object): class Salsa20Cipher(object):
@ -83,7 +90,7 @@ class Salsa20Cipher(object):
def __init__(self, alg, key, iv, op, key_as_bytes=0, d=None, salt=None, def __init__(self, alg, key, iv, op, key_as_bytes=0, d=None, salt=None,
i=1, padding=1): i=1, padding=1):
run_imports() run_imports()
if alg != 'salsa20-ctr': if alg != b'salsa20-ctr':
raise Exception('unknown algorithm') raise Exception('unknown algorithm')
self._key = key self._key = key
self._nonce = struct.unpack('<Q', iv)[0] self._nonce = struct.unpack('<Q', iv)[0]
@ -115,7 +122,7 @@ class Salsa20Cipher(object):
self._pos = 0 self._pos = 0
if not data: if not data:
break break
return ''.join(results) return b''.join(results)
ciphers = { ciphers = {
@ -137,8 +144,8 @@ def test():
# key_as_bytes=0, d='md5', salt=None, i=1, # key_as_bytes=0, d='md5', salt=None, i=1,
# padding=1) # padding=1)
cipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) cipher = Salsa20Cipher(b'salsa20-ctr', b'k' * 32, b'i' * 8, 1)
decipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1) decipher = Salsa20Cipher(b'salsa20-ctr', b'k' * 32, b'i' * 8, 1)
results = [] results = []
pos = 0 pos = 0
print('salsa20 test start') print('salsa20 test start')
@ -149,7 +156,7 @@ def test():
results.append(c) results.append(c)
pos += l pos += l
pos = 0 pos = 0
c = ''.join(results) c = b''.join(results)
results = [] results = []
while pos < len(plain): while pos < len(plain):
l = random.randint(100, 32768) l = random.randint(100, 32768)
@ -157,7 +164,7 @@ def test():
pos += l pos += l
end = time.time() end = time.time()
print('speed: %d bytes/s' % (BLOCK_SIZE * rounds / (end - start))) print('speed: %d bytes/s' % (BLOCK_SIZE * rounds / (end - start)))
assert ''.join(results) == plain assert b''.join(results) == plain
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save