mtproto 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +13 -1
  3. data/ext/aes_ige/Makefile +273 -0
  4. data/ext/aes_ige/aes_ige.bundle +0 -0
  5. data/ext/aes_ige/aes_ige.c +103 -0
  6. data/ext/aes_ige/extconf.rb +25 -0
  7. data/ext/factorization/Makefile +273 -0
  8. data/ext/factorization/extconf.rb +3 -0
  9. data/ext/factorization/factorization.bundle +0 -0
  10. data/ext/factorization/factorization.c +62 -0
  11. data/lib/mtproto/auth_key_generator.rb +228 -0
  12. data/lib/mtproto/client.rb +235 -0
  13. data/lib/mtproto/crypto/aes_ige.rb +23 -0
  14. data/lib/mtproto/crypto/auth_key_helper.rb +25 -0
  15. data/lib/mtproto/crypto/dh_key_exchange.rb +44 -0
  16. data/lib/mtproto/crypto/dh_validator.rb +80 -0
  17. data/lib/mtproto/crypto/factorization.rb +39 -0
  18. data/lib/mtproto/crypto/message_key.rb +32 -0
  19. data/lib/mtproto/crypto/rsa_key.rb +66 -0
  20. data/lib/mtproto/crypto/rsa_pad.rb +59 -0
  21. data/lib/mtproto/encrypted_message.rb +86 -0
  22. data/lib/mtproto/session.rb +20 -0
  23. data/lib/mtproto/tl/bad_msg_notification.rb +46 -0
  24. data/lib/mtproto/tl/client_dh_inner_data.rb +29 -0
  25. data/lib/mtproto/tl/config.rb +122 -0
  26. data/lib/mtproto/tl/gzip_packed.rb +41 -0
  27. data/lib/mtproto/tl/message.rb +246 -0
  28. data/lib/mtproto/tl/msg_container.rb +40 -0
  29. data/lib/mtproto/tl/new_session_created.rb +30 -0
  30. data/lib/mtproto/tl/p_q_inner_data.rb +41 -0
  31. data/lib/mtproto/tl/rpc_error.rb +34 -0
  32. data/lib/mtproto/tl/serializer.rb +55 -0
  33. data/lib/mtproto/tl/server_dh_inner_data.rb +85 -0
  34. data/lib/mtproto/version.rb +1 -1
  35. data/lib/mtproto.rb +23 -0
  36. data/tmp/.keep +0 -0
  37. metadata +33 -1
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ module Serializer
6
+ module_function
7
+
8
+ def serialize_int(value)
9
+ [value].pack('L<')
10
+ end
11
+
12
+ def serialize_long(value)
13
+ [value].pack('Q<')
14
+ end
15
+
16
+ def serialize_int128(value)
17
+ value.is_a?(String) ? value : [value].pack('Q<Q<')
18
+ end
19
+
20
+ def serialize_int256(value)
21
+ value.is_a?(String) ? value : [value].pack('Q<Q<Q<Q<')
22
+ end
23
+
24
+ def serialize_bytes(bytes)
25
+ length = bytes.bytesize
26
+
27
+ if length <= 253
28
+ [length].pack('C').b + bytes.b + padding(length + 1)
29
+ else
30
+ [254].pack('C').b + [length].pack('L<')[0, 3].b + bytes.b + padding(length + 4)
31
+ end
32
+ end
33
+
34
+ def serialize_string(str)
35
+ serialize_bytes(str)
36
+ end
37
+
38
+ def padding(current_length)
39
+ pad_length = (4 - (current_length % 4)) % 4
40
+ ("\x00" * pad_length).b
41
+ end
42
+
43
+ def integer_to_bytes(int)
44
+ return "\x00" if int.zero?
45
+
46
+ bytes = []
47
+ while int > 0
48
+ bytes.unshift(int & 0xff)
49
+ int >>= 8
50
+ end
51
+ bytes.pack('C*')
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MTProto
4
+ module TL
5
+ class ServerDHInnerData
6
+ CONSTRUCTOR = 0xb5890dba
7
+
8
+ attr_reader :nonce, :server_nonce, :g, :dh_prime, :g_a, :server_time
9
+
10
+ def self.deserialize(data)
11
+ constructor = data[0, 4].unpack1('L<')
12
+ raise "Unexpected constructor: 0x#{constructor.to_s(16)}" unless constructor == CONSTRUCTOR
13
+
14
+ offset = 4
15
+
16
+ nonce = data[offset, 16]
17
+ offset += 16
18
+
19
+ server_nonce = data[offset, 16]
20
+ offset += 16
21
+
22
+ g = data[offset, 4].unpack1('L<')
23
+ offset += 4
24
+
25
+ dh_prime_length_byte = data[offset].ord
26
+ offset += 1
27
+
28
+ if dh_prime_length_byte == 254
29
+ length_bytes = data[offset, 3].bytes
30
+ dh_prime_length = length_bytes[0] | (length_bytes[1] << 8) | (length_bytes[2] << 16)
31
+ offset += 3
32
+ else
33
+ dh_prime_length = dh_prime_length_byte
34
+ end
35
+
36
+ dh_prime = data[offset, dh_prime_length]
37
+ offset += dh_prime_length
38
+ offset += padding_length(dh_prime_length_byte == 254 ? dh_prime_length + 4 : dh_prime_length + 1)
39
+
40
+ g_a_length_byte = data[offset].ord
41
+ offset += 1
42
+
43
+ if g_a_length_byte == 254
44
+ length_bytes = data[offset, 3].bytes
45
+ g_a_length = length_bytes[0] | (length_bytes[1] << 8) | (length_bytes[2] << 16)
46
+ offset += 3
47
+ else
48
+ g_a_length = g_a_length_byte
49
+ end
50
+
51
+ g_a = data[offset, g_a_length]
52
+ offset += g_a_length
53
+ offset += padding_length(g_a_length_byte == 254 ? g_a_length + 4 : g_a_length + 1)
54
+
55
+ server_time = data[offset, 4].unpack1('L<')
56
+
57
+ new(
58
+ nonce: nonce,
59
+ server_nonce: server_nonce,
60
+ g: g,
61
+ dh_prime: dh_prime,
62
+ g_a: g_a,
63
+ server_time: server_time
64
+ )
65
+ end
66
+
67
+ def initialize(nonce:, server_nonce:, g:, dh_prime:, g_a:, server_time:)
68
+ @nonce = nonce
69
+ @server_nonce = server_nonce
70
+ @g = g
71
+ @dh_prime = dh_prime
72
+ @g_a = g_a
73
+ @server_time = server_time
74
+ end
75
+
76
+ def self.padding_length(length)
77
+ (4 - (length % 4)) % 4
78
+ end
79
+
80
+ def padding_length(length)
81
+ self.class.padding_length(length)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MTProto
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.5'
5
5
  end
data/lib/mtproto.rb CHANGED
@@ -3,6 +3,29 @@
3
3
  require_relative 'mtproto/version'
4
4
  require_relative 'mtproto/transport/abridged_packet_codec'
5
5
  require_relative 'mtproto/transport/tcp_connection'
6
+ require_relative 'mtproto/tl/message'
7
+ require_relative 'mtproto/tl/serializer'
8
+ require_relative 'mtproto/tl/p_q_inner_data'
9
+ require_relative 'mtproto/tl/server_dh_inner_data'
10
+ require_relative 'mtproto/tl/client_dh_inner_data'
11
+ require_relative 'mtproto/tl/bad_msg_notification'
12
+ require_relative 'mtproto/tl/msg_container'
13
+ require_relative 'mtproto/tl/new_session_created'
14
+ require_relative 'mtproto/tl/rpc_error'
15
+ require_relative 'mtproto/tl/gzip_packed'
16
+ require_relative 'mtproto/tl/config'
17
+ require_relative 'mtproto/crypto/rsa_key'
18
+ require_relative 'mtproto/crypto/factorization'
19
+ require_relative 'mtproto/crypto/aes_ige'
20
+ require_relative 'mtproto/crypto/rsa_pad'
21
+ require_relative 'mtproto/crypto/auth_key_helper'
22
+ require_relative 'mtproto/crypto/dh_validator'
23
+ require_relative 'mtproto/crypto/dh_key_exchange'
24
+ require_relative 'mtproto/crypto/message_key'
25
+ require_relative 'mtproto/auth_key_generator'
26
+ require_relative 'mtproto/session'
27
+ require_relative 'mtproto/encrypted_message'
28
+ require_relative 'mtproto/client'
6
29
 
7
30
  module MTProto
8
31
  end
data/tmp/.keep ADDED
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mtproto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Levenkov
@@ -19,10 +19,42 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".ruby-version"
21
21
  - Rakefile
22
+ - ext/aes_ige/Makefile
23
+ - ext/aes_ige/aes_ige.bundle
24
+ - ext/aes_ige/aes_ige.c
25
+ - ext/aes_ige/extconf.rb
26
+ - ext/factorization/Makefile
27
+ - ext/factorization/extconf.rb
28
+ - ext/factorization/factorization.bundle
29
+ - ext/factorization/factorization.c
22
30
  - lib/mtproto.rb
31
+ - lib/mtproto/auth_key_generator.rb
32
+ - lib/mtproto/client.rb
33
+ - lib/mtproto/crypto/aes_ige.rb
34
+ - lib/mtproto/crypto/auth_key_helper.rb
35
+ - lib/mtproto/crypto/dh_key_exchange.rb
36
+ - lib/mtproto/crypto/dh_validator.rb
37
+ - lib/mtproto/crypto/factorization.rb
38
+ - lib/mtproto/crypto/message_key.rb
39
+ - lib/mtproto/crypto/rsa_key.rb
40
+ - lib/mtproto/crypto/rsa_pad.rb
41
+ - lib/mtproto/encrypted_message.rb
42
+ - lib/mtproto/session.rb
43
+ - lib/mtproto/tl/bad_msg_notification.rb
44
+ - lib/mtproto/tl/client_dh_inner_data.rb
45
+ - lib/mtproto/tl/config.rb
46
+ - lib/mtproto/tl/gzip_packed.rb
47
+ - lib/mtproto/tl/message.rb
48
+ - lib/mtproto/tl/msg_container.rb
49
+ - lib/mtproto/tl/new_session_created.rb
50
+ - lib/mtproto/tl/p_q_inner_data.rb
51
+ - lib/mtproto/tl/rpc_error.rb
52
+ - lib/mtproto/tl/serializer.rb
53
+ - lib/mtproto/tl/server_dh_inner_data.rb
23
54
  - lib/mtproto/transport/abridged_packet_codec.rb
24
55
  - lib/mtproto/transport/tcp_connection.rb
25
56
  - lib/mtproto/version.rb
57
+ - tmp/.keep
26
58
  homepage: https://github.com/alev-pro/mtproto-ruby
27
59
  licenses:
28
60
  - MIT