Skip to content

miniproto.crypto.native.mtproto_encode_message

miniproto.crypto.native.mtproto_encode_message

Section titled “miniproto.crypto.native.mtproto_encode_message”
mtproto_encode_message(auth_key: bytes, server_salt: int, session_id: int, msg_id: int, seq_no: int, body: BytesLike, *, client_to_server: bool = True, padding: bytes | None = None) -> bytes

Build, pad, and encrypt a complete MTProto encrypted message.

Parameters:

  • auth_key (bytes) – Exactly 256 bytes of authorization-key material.
  • server_salt (int) – Unsigned 64-bit salt; bits outside that width are masked.
  • session_id (int) – Unsigned 64-bit session identifier; bits outside that width are masked.
  • msg_id (int) – Signed 64-bit MTProto message identifier.
  • seq_no (int) – Signed 32-bit message sequence number.
  • body (BytesLike) – TL body bytes no larger than the signed 32-bit protocol limit.
  • client_to_server (bool) – Uses the client-to-server direction by default.
  • padding (bytes | None) – Optional compliant padding; when omitted the backend generates random padding.

Returns:

  • bytes – The encrypted wire packet: auth-key ID, message key, and ciphertext.

Raises:

  • ValueError – If key, body, padding, or encrypted-layout constraints fail.
  • OverflowError – If signed msg_id or seq_no cannot be encoded.

The default random-padding source belongs to the selected backend. The function returns protocol bytes only; it does not send them.