mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 09:26:37 +03:00
Use more straightforward calls of hashlib.sha1/sha256
This commit is contained in:
parent
6b4f2abb96
commit
153cbfd350
|
@ -1,3 +1,5 @@
|
||||||
|
from hashlib import sha1
|
||||||
|
|
||||||
from .. import helpers as utils
|
from .. import helpers as utils
|
||||||
from ..utils import BinaryReader, BinaryWriter
|
from ..utils import BinaryReader, BinaryWriter
|
||||||
|
|
||||||
|
@ -6,7 +8,7 @@ class AuthKey:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.key = data
|
self.key = data
|
||||||
|
|
||||||
with BinaryReader(utils.sha1(self.key)) as reader:
|
with BinaryReader(sha1(self.key).digest()) as reader:
|
||||||
self.aux_hash = reader.read_long(signed=False)
|
self.aux_hash = reader.read_long(signed=False)
|
||||||
reader.read(4)
|
reader.read(4)
|
||||||
self.key_id = reader.read_long(signed=False)
|
self.key_id = reader.read_long(signed=False)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
|
from hashlib import sha1
|
||||||
|
|
||||||
from .. import helpers as utils
|
|
||||||
from ..utils import BinaryWriter
|
from ..utils import BinaryWriter
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ class RSAServerKey:
|
||||||
|
|
||||||
with BinaryWriter() as writer:
|
with BinaryWriter() as writer:
|
||||||
# Write SHA
|
# Write SHA
|
||||||
writer.write(utils.sha1(data[offset:offset + length]))
|
writer.write(sha1(data[offset:offset + length]).digest())
|
||||||
# Write data
|
# Write data
|
||||||
writer.write(data[offset:offset + length])
|
writer.write(data[offset:offset + length])
|
||||||
# Add padding if required
|
# Add padding if required
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import hashlib
|
from hashlib import sha1, sha256
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# region Multiple utilities
|
# region Multiple utilities
|
||||||
|
@ -24,11 +24,12 @@ def calc_key(shared_key, msg_key, client):
|
||||||
"""Calculate the key based on Telegram guidelines, specifying whether it's the client or not"""
|
"""Calculate the key based on Telegram guidelines, specifying whether it's the client or not"""
|
||||||
x = 0 if client else 8
|
x = 0 if client else 8
|
||||||
|
|
||||||
sha1a = sha1(msg_key + shared_key[x:x + 32])
|
sha1a = sha1(msg_key + shared_key[x:x + 32]).digest()
|
||||||
sha1b = sha1(shared_key[x + 32:x + 48] + msg_key + shared_key[x + 48:x +
|
sha1b = sha1(shared_key[x + 32:x + 48] + msg_key +
|
||||||
64])
|
shared_key[x + 48:x + 64]).digest()
|
||||||
sha1c = sha1(shared_key[x + 64:x + 96] + msg_key)
|
|
||||||
sha1d = sha1(msg_key + shared_key[x + 96:x + 128])
|
sha1c = sha1(shared_key[x + 64:x + 96] + msg_key).digest()
|
||||||
|
sha1d = sha1(msg_key + shared_key[x + 96:x + 128]).digest()
|
||||||
|
|
||||||
key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16]
|
key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16]
|
||||||
iv = sha1a[8:20] + sha1b[0:8] + sha1c[16:20] + sha1d[0:8]
|
iv = sha1a[8:20] + sha1b[0:8] + sha1c[16:20] + sha1d[0:8]
|
||||||
|
@ -38,34 +39,20 @@ def calc_key(shared_key, msg_key, client):
|
||||||
|
|
||||||
def calc_msg_key(data):
|
def calc_msg_key(data):
|
||||||
"""Calculates the message key from the given data"""
|
"""Calculates the message key from the given data"""
|
||||||
return sha1(data)[4:20]
|
return sha1(data).digest()[4:20]
|
||||||
|
|
||||||
|
|
||||||
def generate_key_data_from_nonce(server_nonce, new_nonce):
|
def generate_key_data_from_nonce(server_nonce, new_nonce):
|
||||||
"""Generates the key data corresponding to the given nonce"""
|
"""Generates the key data corresponding to the given nonce"""
|
||||||
hash1 = sha1(bytes(new_nonce + server_nonce))
|
hash1 = sha1(bytes(new_nonce + server_nonce)).digest()
|
||||||
hash2 = sha1(bytes(server_nonce + new_nonce))
|
hash2 = sha1(bytes(server_nonce + new_nonce)).digest()
|
||||||
hash3 = sha1(bytes(new_nonce + new_nonce))
|
hash3 = sha1(bytes(new_nonce + new_nonce)).digest()
|
||||||
|
|
||||||
key = hash1 + hash2[:12]
|
key = hash1 + hash2[:12]
|
||||||
iv = hash2[12:20] + hash3 + new_nonce[:4]
|
iv = hash2[12:20] + hash3 + new_nonce[:4]
|
||||||
return key, iv
|
return key, iv
|
||||||
|
|
||||||
|
|
||||||
def sha1(data):
|
|
||||||
"""Calculates the SHA1 digest for the given data"""
|
|
||||||
sha = hashlib.sha1()
|
|
||||||
sha.update(data)
|
|
||||||
return sha.digest()
|
|
||||||
|
|
||||||
|
|
||||||
def sha256(data):
|
|
||||||
"""Calculates the SHA256 digest for the given data"""
|
|
||||||
sha = hashlib.sha256()
|
|
||||||
sha.update(data)
|
|
||||||
return sha.digest()
|
|
||||||
|
|
||||||
|
|
||||||
def get_password_hash(pw, current_salt):
|
def get_password_hash(pw, current_salt):
|
||||||
"""Gets the password hash for the two-step verification.
|
"""Gets the password hash for the two-step verification.
|
||||||
current_salt should be the byte array provided by invoking GetPasswordRequest()"""
|
current_salt should be the byte array provided by invoking GetPasswordRequest()"""
|
||||||
|
@ -76,6 +63,6 @@ def get_password_hash(pw, current_salt):
|
||||||
data = pw.encode('utf-8')
|
data = pw.encode('utf-8')
|
||||||
|
|
||||||
pw_hash = current_salt + data + current_salt
|
pw_hash = current_salt + data + current_salt
|
||||||
return sha256(pw_hash)
|
return sha256(pw_hash).digest()
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
from hashlib import sha1
|
||||||
|
|
||||||
from .. import helpers as utils
|
from .. import helpers as utils
|
||||||
from ..crypto import AES, RSA, AuthKey, Factorization
|
from ..crypto import AES, RSA, AuthKey, Factorization
|
||||||
|
@ -162,11 +163,13 @@ def do_authentication(transport):
|
||||||
|
|
||||||
with BinaryWriter() as client_dh_inner_data_with_hash_writer:
|
with BinaryWriter() as client_dh_inner_data_with_hash_writer:
|
||||||
client_dh_inner_data_with_hash_writer.write(
|
client_dh_inner_data_with_hash_writer.write(
|
||||||
utils.sha1(client_dh_inner_data_writer.get_bytes()))
|
sha1(client_dh_inner_data_writer.get_bytes()).digest())
|
||||||
|
|
||||||
client_dh_inner_data_with_hash_writer.write(
|
client_dh_inner_data_with_hash_writer.write(
|
||||||
client_dh_inner_data_writer.get_bytes())
|
client_dh_inner_data_writer.get_bytes())
|
||||||
client_dh_inner_data_bytes = client_dh_inner_data_with_hash_writer.get_bytes(
|
|
||||||
)
|
client_dh_inner_data_bytes = \
|
||||||
|
client_dh_inner_data_with_hash_writer.get_bytes()
|
||||||
|
|
||||||
# Encryption
|
# Encryption
|
||||||
client_dh_inner_data_encrypted_bytes = AES.encrypt_ige(
|
client_dh_inner_data_encrypted_bytes = AES.encrypt_ige(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
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
|
||||||
|
@ -23,7 +24,7 @@ class CryptoTests(unittest.TestCase):
|
||||||
def test_sha1():
|
def test_sha1():
|
||||||
string = 'Example string'
|
string = 'Example string'
|
||||||
|
|
||||||
hash_sum = utils.sha1(string.encode('utf-8'))
|
hash_sum = 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 {})'\
|
||||||
|
|
Loading…
Reference in New Issue
Block a user