Skip to content

Commit b35fabd

Browse files
committed
Add AES CTR operation mode
1 parent 14632c9 commit b35fabd

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

pyrogram/crypto/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from .ctr import CTR
1920
from .ige import IGE
2021
from .kdf import KDF
2122
from .prime import Prime

pyrogram/crypto/ctr.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

Comments
 (0)