Class: MTProto::EncryptedMessage
- Inherits:
-
Object
- Object
- MTProto::EncryptedMessage
- Defined in:
- lib/mtproto/encrypted_message.rb
Instance Attribute Summary collapse
-
#auth_key_id ⇒ Object
readonly
Returns the value of attribute auth_key_id.
-
#encrypted_data ⇒ Object
readonly
Returns the value of attribute encrypted_data.
-
#msg_key ⇒ Object
readonly
Returns the value of attribute msg_key.
Class Method Summary collapse
- .decrypt(auth_key:, encrypted_message_data:, sender: :server) ⇒ Object
- .encrypt(auth_key:, server_salt:, session_id:, msg_id:, seq_no:, body:) ⇒ Object
Instance Method Summary collapse
-
#initialize(auth_key_id:, msg_key:, encrypted_data:) ⇒ EncryptedMessage
constructor
A new instance of EncryptedMessage.
- #serialize ⇒ Object
Constructor Details
#initialize(auth_key_id:, msg_key:, encrypted_data:) ⇒ EncryptedMessage
Returns a new instance of EncryptedMessage.
10 11 12 13 14 |
# File 'lib/mtproto/encrypted_message.rb', line 10 def initialize(auth_key_id:, msg_key:, encrypted_data:) @auth_key_id = auth_key_id @msg_key = msg_key @encrypted_data = encrypted_data end |
Instance Attribute Details
#auth_key_id ⇒ Object (readonly)
Returns the value of attribute auth_key_id.
8 9 10 |
# File 'lib/mtproto/encrypted_message.rb', line 8 def auth_key_id @auth_key_id end |
#encrypted_data ⇒ Object (readonly)
Returns the value of attribute encrypted_data.
8 9 10 |
# File 'lib/mtproto/encrypted_message.rb', line 8 def encrypted_data @encrypted_data end |
#msg_key ⇒ Object (readonly)
Returns the value of attribute msg_key.
8 9 10 |
# File 'lib/mtproto/encrypted_message.rb', line 8 def msg_key @msg_key end |
Class Method Details
.decrypt(auth_key:, encrypted_message_data:, sender: :server) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mtproto/encrypted_message.rb', line 40 def self.decrypt(auth_key:, encrypted_message_data:, sender: :server) auth_key_id = [0, 8].unpack1('Q<') msg_key = [8, 16] encrypted_data = [24..] keys = Crypto::MessageKey.derive_aes_key_iv(auth_key, msg_key, sender: sender) plaintext = Crypto::AES_IGE.decrypt_ige(encrypted_data, keys[:aes_key], keys[:aes_iv]) expected_msg_key = Crypto::MessageKey.generate_msg_key(auth_key, plaintext, sender: sender) raise 'msg_key mismatch!' unless msg_key == expected_msg_key offset = 0 server_salt = plaintext[offset, 8].unpack1('Q<') offset += 8 session_id = plaintext[offset, 8].unpack1('Q<') offset += 8 msg_id = plaintext[offset, 8].unpack1('Q<') offset += 8 seq_no = plaintext[offset, 4].unpack1('L<') offset += 4 body_length = plaintext[offset, 4].unpack1('L<') offset += 4 body = plaintext[offset, body_length] { auth_key_id: auth_key_id, msg_key: msg_key, server_salt: server_salt, session_id: session_id, msg_id: msg_id, seq_no: seq_no, body: body } end |
.encrypt(auth_key:, server_salt:, session_id:, msg_id:, seq_no:, body:) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mtproto/encrypted_message.rb', line 16 def self.encrypt(auth_key:, server_salt:, session_id:, msg_id:, seq_no:, body:) salt_bytes = [server_salt].pack('Q<') session_id_bytes = [session_id].pack('Q<') msg_id_bytes = [msg_id].pack('Q<') seq_no_bytes = [seq_no].pack('L<') body_length_bytes = [body.bytesize].pack('L<') plaintext = salt_bytes + session_id_bytes + msg_id_bytes + seq_no_bytes + body_length_bytes + body padding_length = (16 - (plaintext.bytesize % 16)) % 16 padding_length += 16 if padding_length < 12 plaintext += SecureRandom.random_bytes(padding_length) msg_key = Crypto::MessageKey.generate_msg_key(auth_key, plaintext, sender: :client) keys = Crypto::MessageKey.derive_aes_key_iv(auth_key, msg_key, sender: :client) encrypted_data = Crypto::AES_IGE.encrypt_ige(plaintext, keys[:aes_key], keys[:aes_iv]) auth_key_id = Digest::SHA1.digest(auth_key)[-8..].unpack1('Q<') new(auth_key_id: auth_key_id, msg_key: msg_key, encrypted_data: encrypted_data) end |
Instance Method Details
#serialize ⇒ Object
81 82 83 84 |
# File 'lib/mtproto/encrypted_message.rb', line 81 def serialize auth_key_id_bytes = [@auth_key_id].pack('Q<') auth_key_id_bytes + @msg_key + @encrypted_data end |