mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-02 23:20:42 +00:00
Added authenticator
This commit is contained in:
30
utils/auth_key.py
Normal file
30
utils/auth_key.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# This file is based on TLSharp
|
||||
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/MTProto/Crypto/AuthKey.cs
|
||||
from hashlib import sha1
|
||||
from utils.binary_writer import BinaryWriter
|
||||
from utils.binary_reader import BinaryReader
|
||||
|
||||
|
||||
class AuthKey:
|
||||
def __init__(self, gab=None, data=None):
|
||||
if gab:
|
||||
self.key = gab.to_byte_array_unsigned()
|
||||
elif data:
|
||||
self.key = data
|
||||
else:
|
||||
raise AssertionError('Either a gab integer or data bytes array must be provided')
|
||||
|
||||
with BinaryReader(sha1(self.key)) as reader:
|
||||
self.aux_hash = reader.read_long(signed=False)
|
||||
reader.read(4)
|
||||
self.key_id = reader.read_long(signed=False)
|
||||
|
||||
|
||||
def calc_new_nonce_hash(self, new_nonce, number):
|
||||
with BinaryWriter() as writer:
|
||||
writer.write(new_nonce)
|
||||
writer.write_byte(number)
|
||||
writer.write_long(self.aux_hash, signed=False)
|
||||
|
||||
new_nonce_hash = sha1(writer.get_bytes())[4:20]
|
||||
return new_nonce_hash
|
||||
46
utils/factorizator.py
Normal file
46
utils/factorizator.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# This file is based on TLSharp
|
||||
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/MTProto/Crypto/Factorizator.cs
|
||||
from random import randint
|
||||
from math import gcd
|
||||
|
||||
|
||||
class Factorizator:
|
||||
@staticmethod
|
||||
def find_small_multiplier_lopatin(what):
|
||||
g = 0
|
||||
for i in range(3):
|
||||
q = (randint(0, 127) & 15) + 17
|
||||
x = randint(1000000000) + 1
|
||||
y = x
|
||||
lim = 1 << (i + 18)
|
||||
for j in range(1, lim):
|
||||
a, b, c = x, x, q
|
||||
while b != 0:
|
||||
if (b & 1) != 0:
|
||||
c += a
|
||||
if c >= what:
|
||||
c -= what
|
||||
a += a
|
||||
if a >= what:
|
||||
a -= what
|
||||
b >>= 1
|
||||
|
||||
x = c
|
||||
z = y - x if x < y else x - y
|
||||
g = gcd(z, what)
|
||||
if g != 1:
|
||||
break
|
||||
|
||||
if (j & (j - 1)) == 0:
|
||||
y = x
|
||||
|
||||
if g > 1:
|
||||
break
|
||||
|
||||
p = what // g
|
||||
return min(p, g)
|
||||
|
||||
@staticmethod
|
||||
def factorize(pq):
|
||||
divisor = Factorizator.find_small_multiplier_lopatin(pq)
|
||||
return divisor, pq // divisor
|
||||
@@ -59,3 +59,40 @@ def calc_msg_key_offset(data, offset, limit):
|
||||
# TODO untested, may not be offset like this
|
||||
# In the original code it was as parameters for the sha function, not slicing the array
|
||||
return sha1(data[offset:offset + limit])[4:20]
|
||||
|
||||
|
||||
def generate_key_data_from_nonces(serverNonce, newNonce):
|
||||
# TODO unsure that this works
|
||||
nonces = [0] * 48
|
||||
|
||||
nonces[00:32] = newNonce
|
||||
nonces[32:48] = serverNonce
|
||||
hash1 = hash(bytes(nonces))
|
||||
|
||||
nonces[00:16] = serverNonce
|
||||
nonces[16:32] = newNonce
|
||||
hash2 = hash(bytes(nonces))
|
||||
|
||||
nonces = [0] * 64
|
||||
nonces[00:32] = newNonce
|
||||
nonces[32:64] = newNonce
|
||||
hash2 = hash(bytes(nonces))
|
||||
|
||||
with BinaryWriter() as keyBuffer:
|
||||
with BinaryWriter() as ivBuffer:
|
||||
"""
|
||||
using (var keyBuffer = new MemoryStream(32))
|
||||
using (var ivBuffer = new MemoryStream(32))
|
||||
{
|
||||
keyBuffer.Write(hash1, 0, hash1.Length);
|
||||
keyBuffer.Write(hash2, 0, 12);
|
||||
|
||||
ivBuffer.Write(hash2, 12, 8);
|
||||
ivBuffer.Write(hash3, 0, hash3.Length);
|
||||
ivBuffer.Write(newNonce, 0, 4);
|
||||
|
||||
return new AESKeyData(keyBuffer.ToArray(), ivBuffer.ToArray());
|
||||
}
|
||||
"""
|
||||
# TODO implement
|
||||
raise NotImplementedError()
|
||||
|
||||
Reference in New Issue
Block a user