Fix fingerprint test; add a similar test for rsa lib.

This commit is contained in:
Andrei Fokau 2017-10-28 13:51:14 +02:00
parent 55846c7ac9
commit 35ade37d74
No known key found for this signature in database
GPG Key ID: F2BC8AF1CE23A98E

View File

@ -1,11 +1,24 @@
import hashlib
import struct
import unittest import unittest
from hashlib import sha1
import telethon.helpers as utils import telethon.helpers as utils
from telethon.crypto import AES, Factorization from telethon.crypto import AES, Factorization
from telethon.crypto import rsa from telethon.crypto import rsa
from Crypto.PublicKey import RSA as PyCryptoRSA from Crypto.PublicKey import RSA as PyCryptoRSA
TEST_RSA_KEY = '''
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6
lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS
an9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw
Efzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+
8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n
Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
-----END RSA PUBLIC KEY-----
'''.strip()
TEST_RSA_KEY_FINGERPRINT = b'!k\xe8l\x02+\xb4\xc3'
class CryptoTests(unittest.TestCase): class CryptoTests(unittest.TestCase):
def setUp(self): def setUp(self):
@ -26,7 +39,7 @@ class CryptoTests(unittest.TestCase):
def test_sha1(): def test_sha1():
string = 'Example string' string = 'Example string'
hash_sum = sha1(string.encode('utf-8')).digest() hash_sum = hashlib.sha1(string.encode('utf-8')).digest()
expected = b'\nT\x92|\x8d\x06:)\x99\x04\x8e\xf8j?\xc4\x8e\xd3}m9' expected = b'\nT\x92|\x8d\x06:)\x99\x04\x8e\xf8j?\xc4\x8e\xd3}m9'
assert hash_sum == expected, 'Invalid sha1 hash_sum representation (should be {}, but is {})'\ assert hash_sum == expected, 'Invalid sha1 hash_sum representation (should be {}, but is {})'\
@ -119,18 +132,16 @@ class CryptoTests(unittest.TestCase):
assert iv == expected_iv, 'IV ("{}") does not equal expected ("{}")'.format( assert iv == expected_iv, 'IV ("{}") does not equal expected ("{}")'.format(
iv, expected_iv) iv, expected_iv)
@staticmethod def test_fingerprint_from_key_pycrypto(self):
def test_fingerprint_from_key(): key = PyCryptoRSA.importKey(TEST_RSA_KEY)
assert rsa._compute_fingerprint(PyCryptoRSA.importKey( fp = rsa._compute_fingerprint(key)
'-----BEGIN RSA PUBLIC KEY-----\n' self.assertEqual(fp, struct.unpack('<q', TEST_RSA_KEY_FINGERPRINT)[0])
'MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6\n'
'lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS\n' def test_fingerprint_from_key_rsa(self):
'an9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw\n' import rsa as rsalib
'Efzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+\n' key = rsalib.PublicKey.load_pkcs1(TEST_RSA_KEY)
'8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n\n' fp = rsa._compute_fingerprint(key)
'Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB\n' self.assertEqual(fp, struct.unpack('<q', TEST_RSA_KEY_FINGERPRINT)[0])
'-----END RSA PUBLIC KEY-----'
)) == b'!k\xe8l\x02+\xb4\xc3', 'Wrong fingerprint calculated'
@staticmethod @staticmethod
def test_factorize(): def test_factorize():