|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017 Dan Tès <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from collections import namedtuple |
| 20 | + |
| 21 | +PublicKey = namedtuple("PublicKey", ["m", "e"]) |
| 22 | + |
| 23 | + |
| 24 | +class RSA: |
| 25 | + # To get modulus and exponent: |
| 26 | + # grep -v -- - public.key | tr -d \\n | base64 -d | openssl asn1parse -inform DER -i |
| 27 | + |
| 28 | + # TODO Add CDNs keys |
| 29 | + server_public_keys = { |
| 30 | + 0xc3b42b026ce86b21 - (1 << 64): PublicKey( |
| 31 | + # -----BEGIN RSA PUBLIC KEY----- |
| 32 | + # MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6 |
| 33 | + # lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS |
| 34 | + # an9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw |
| 35 | + # Efzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+ |
| 36 | + # 8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n |
| 37 | + # Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB |
| 38 | + # -----END RSA PUBLIC KEY----- |
| 39 | + int( |
| 40 | + "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F9" |
| 41 | + "1F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E" |
| 42 | + "580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F" |
| 43 | + "9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934" |
| 44 | + "EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F" |
| 45 | + "81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F" |
| 46 | + "6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD1" |
| 47 | + "5A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", |
| 48 | + 16 |
| 49 | + ), # Modulus |
| 50 | + int("010001", 16) # Exponent |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def encrypt(cls, data: bytes, fingerprint: int) -> bytes: |
| 56 | + return int.to_bytes( |
| 57 | + pow( |
| 58 | + int.from_bytes(data, "big"), |
| 59 | + cls.server_public_keys[fingerprint].e, |
| 60 | + cls.server_public_keys[fingerprint].m |
| 61 | + ), |
| 62 | + 256, |
| 63 | + "big" |
| 64 | + ) |
0 commit comments