Skip to content

Instantly share code, notes, and snippets.

@Justasic
Created March 24, 2022 22:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justasic/86601a40680e452eb35450104a29d8f2 to your computer and use it in GitHub Desktop.
Save Justasic/86601a40680e452eb35450104a29d8f2 to your computer and use it in GitHub Desktop.
Calculate the CRC32 values of Telegram's Type Language constructor IDs
#!/usr/bin/env python3
import sys, re
from zlib import crc32
from typing import Tuple
from PyQt5.QtWidgets import QWidget, QLineEdit, QApplication, QLabel, QVBoxLayout, QHBoxLayout
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.InitUI()
def GenerateID(self, text) -> Tuple[str, int]:
# Don't jack up the modifiers
line = re.sub(r"([%!$])[{}()<>]", r"\1", text)
# Telegram dictates that all parens and brackets and other special stuff removed
line = re.sub(r"[{}()<>;]|#[a-zA-Z0-9]+", " ", line)
# spaces must be inserted between square brackets
line = re.sub(r"\[([^\]]+)\]", r"[ \1 ]", line)
# Clean spaces up
line = re.sub(r"[\s]{2,}", " ", line.strip())
# telegram only has a "string" type even though sometimes they use "bytes"
# they are serialized the same.
line = line.replace(":bytes", ":string").replace("?bytes", "?string")
# tokenize the string and remove flag-only booleans like `is_bot:flags.2?true`
owo = line.split(" ")
for i,s in enumerate(owo):
if s.endswith("?true"):
del owo[i]
line = " ".join(owo)
return line, crc32(line.encode())
def InitUI(self):
# Add Layouts
self.layout = QVBoxLayout(self)
self.lineonelayout = QHBoxLayout(self)
self.linetwolayout = QHBoxLayout(self)
self.linethreelayout = QHBoxLayout(self)
# Add Label
self.le1 = QLabel(self)
self.le1.setText("Input: ")
self.lineonelayout.addWidget(self.le1)
# Add Input field
self.textin = QLineEdit(self)
self.textin.textChanged.connect(self.TextChanged)
self.lineonelayout.addWidget(self.textin)
# Add Set the layout for the first line
self.layout.addLayout(self.lineonelayout)
# Add Label
self.le3 = QLabel(self)
self.le3.setText("Clean: ")
self.linethreelayout.addWidget(self.le3)
# Add Input field
self.texttest = QLineEdit(self)
self.texttest.setReadOnly(True)
self.texttest.textChanged.connect(self.TextChanged)
self.linethreelayout.addWidget(self.texttest)
# Add Set the layout for the first line
self.layout.addLayout(self.linethreelayout)
# Add label
self.le2 = QLabel(self)
self.le2.setText("CRC32:")
self.linetwolayout.addWidget(self.le2)
self.textout = QLineEdit(self)
self.textout.setReadOnly(True)
self.linetwolayout.addWidget(self.textout)
self.layout.addLayout(self.linetwolayout)
self.setLayout(self.layout)
# self.setGeometry(300, 300, 290, 150)
self.setGeometry(0, 0, 600, 0)
self.setWindowTitle('Telegram CRC Calculator')
self.show()
def TextChanged(self, text):
line, crc = self.GenerateID(text)
self.texttest.setText(line)
self.textout.setText(f"#{crc:x}")
def main() -> int:
app = QApplication(sys.argv)
ex = Calculator()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment