|
| 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 | +try: |
| 20 | + from pyaes import AESModeOfOperationCTR |
| 21 | +except ImportError: |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class CTR: |
| 26 | + def __init__(self, key: bytes, iv: bytes): |
| 27 | + self.ctr = AESModeOfOperationCTR(key) |
| 28 | + self.iv = iv |
| 29 | + |
| 30 | + def decrypt(self, data: bytes, offset: int) -> bytes: |
| 31 | + replace = int.to_bytes(offset // 16, byteorder="big", length=4) |
| 32 | + iv = self.iv[:-4] + replace |
| 33 | + self.ctr._counter._counter = list(iv) |
| 34 | + |
| 35 | + return self.ctr.decrypt(data) |
0 commit comments