Class: MTProto::UnencryptedMessage

Inherits:
Object
  • Object
show all
Extended by:
Binary
Includes:
Binary
Defined in:
lib/mtproto/unencrypted_message.rb

Overview

The unencrypted MTProto transport envelope (auth_key_id=0 | msg_id | len | body), used for the plaintext request/response of the DH key exchange. Its encrypted sibling is EncryptedMessage; the runtime never decrypts through this class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Binary

b_u32, b_u64, u32_b, u64_b

Constructor Details

#initialize(body, msg_id: self.class.generate_msg_id, auth_key_id: 0) ⇒ UnencryptedMessage

Returns a new instance of UnencryptedMessage.



13
14
15
16
17
# File 'lib/mtproto/unencrypted_message.rb', line 13

def initialize(body, msg_id: self.class.generate_msg_id, auth_key_id: 0)
  @auth_key_id = auth_key_id
  @msg_id = msg_id
  @body = body
end

Instance Attribute Details

#auth_key_idObject (readonly)

Returns the value of attribute auth_key_id.



11
12
13
# File 'lib/mtproto/unencrypted_message.rb', line 11

def auth_key_id
  @auth_key_id
end

#bodyObject (readonly)

Returns the value of attribute body.



11
12
13
# File 'lib/mtproto/unencrypted_message.rb', line 11

def body
  @body
end

#msg_idObject (readonly)

Returns the value of attribute msg_id.



11
12
13
# File 'lib/mtproto/unencrypted_message.rb', line 11

def msg_id
  @msg_id
end

Class Method Details

.generate_msg_idObject



35
36
37
# File 'lib/mtproto/unencrypted_message.rb', line 35

def self.generate_msg_id
  (Time.now.to_f * (2**32)).to_i & ~3
end

.parse(packet) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mtproto/unencrypted_message.rb', line 19

def self.parse(packet)
  data = packet.data
  msg_id = b_u64(data[8, 8])
  body_length = b_u32(data[16, 4])
  body = data[20, body_length]

  new(body, msg_id: msg_id, auth_key_id: 0)
end

Instance Method Details

#bytesObject



28
29
30
31
32
33
# File 'lib/mtproto/unencrypted_message.rb', line 28

def bytes
  u64_b(@auth_key_id) +
    u64_b(@msg_id) +
    u32_b(@body.length) +
    @body
end