synacrb 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4964063b1ca35c7063231c1c10f1d86c088493f0
4
- data.tar.gz: a0358c631c43e032dc73ef99785d479bf392d93b
3
+ metadata.gz: 30dbae35dd723e6b899621986b3a0399393cd8c9
4
+ data.tar.gz: ffcffa2fdbd971f00a753b2b0b58ee5a22d23557
5
5
  SHA512:
6
- metadata.gz: f4e7277192867a5e82655c3f586a40a8ad4e9da8cb4bd0e43d4580deb256dc93f7123b7639752712c7364a9681e754744609a895fb2a29cdcfd422f33048dc58
7
- data.tar.gz: 0ddd97c3a7dbbae5f8070fbd55ba33350fd1055b432a305ec1e9e15ef95e728982ead47ab614f26bd92d52a9b7f28988617adae7c25773b5f13c2b27c8e78079
6
+ metadata.gz: 55186b9bd1332c6803c7ab07038a11e5fc74107ef6283895545266d533b87e73ff5587147f6178cb58f99adc94f860bc81fa8599abc053c16f81236b940e4491
7
+ data.tar.gz: ea49876c0be8dbf2d13d1d6c4b080474ab2e14378aa4bebecbf1f23ba0f2fad91057f39839a81731bf08ba5e4a22a3099a7b464b30294b5404eca3970614056b
@@ -1,6 +1,7 @@
1
1
  module Synacrb
2
2
  module Common
3
3
  DEFAULT_PORT = 8439
4
+ RSA_LENGTH = 3072
4
5
  TYPING_TIMEOUT = 10
5
6
 
6
7
  LIMIT_USER_NAME = 128
@@ -76,6 +77,13 @@ module Synacrb
76
77
  PACKET_TYPINGRECEIVE_ID = 29; TypingReceive = Struct.new(:author, :channel)
77
78
  PACKET_USERRECEIVE_ID = 30; UserReceive = Struct.new(:inner)
78
79
 
80
+ def self.encode_u16(input)
81
+ (input >> 8).chr + (input % 256).chr
82
+ end
83
+ def self.decode_u16(input)
84
+ (input[0].ord << 8) + input[1].ord
85
+ end
86
+
79
87
  def self.packet_from_id(id)
80
88
  case id
81
89
  when PACKET_CLOSE_ID
data/lib/encrypter.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Synacrb
2
+ # Encrypt `input` with `rsa` instance.
3
+ # The difference between just encrypting it normally
4
+ # is that this has a larger max-length and is
5
+ # following the standard synac format.
6
+ def self.encrypt(input, rsa)
7
+ cipher = OpenSSL::Cipher.new "AES-256-CBC"
8
+ cipher.encrypt
9
+ key = cipher.random_key
10
+ iv = cipher.random_iv
11
+
12
+ encrypted_aes = cipher.update(input) + cipher.final
13
+ size_aes = encrypted_aes.bytesize
14
+
15
+ keyiv = key + iv
16
+ encrypted_rsa = rsa.public_encrypt keyiv
17
+ size_rsa = encrypted_rsa.bytesize
18
+
19
+ Common.encode_u16(size_rsa) +
20
+ Common.encode_u16(size_aes) +
21
+ encrypted_rsa +
22
+ encrypted_aes
23
+ end
24
+
25
+ # Decrypt `input` with `rsa` instance.
26
+ # The difference between just decrypting it normally
27
+ # is that this has a larger max-length and is
28
+ # following the standard synac format.
29
+ def self.decrypt(input, rsa)
30
+ size_rsa = Common.decode_u16(input.byteslice(0, 2));
31
+ size_aes = Common.decode_u16(input.byteslice(2, 4));
32
+
33
+ input = input.byteslice(4, input.bytesize)
34
+
35
+ keyiv = rsa.private_decrypt input.byteslice(0, size_rsa)
36
+
37
+ cipher = OpenSSL::Cipher.new "AES-256-CBC"
38
+ cipher.decrypt
39
+ cipher.key = keyiv.byteslice(0, 32)
40
+ cipher.iv = keyiv.byteslice(32, 32+16)
41
+ cipher.update(input.byteslice(size_rsa, input.bytesize)) + cipher.final
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Synacrb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/synacrb.rb CHANGED
@@ -1,5 +1,6 @@
1
+ require "common.rb"
2
+ require "encrypter.rb"
1
3
  require "state.rb"
2
- require "packets.rb"
3
4
 
4
5
  require "msgpack"
5
6
  require "openssl"
@@ -55,17 +56,15 @@ module Synacrb
55
56
  def send(packet)
56
57
  id = Common.packet_to_id(packet)
57
58
  data = [id, [packet.to_a]].to_msgpack
58
- size1 = data.length >> 8
59
- size2 = data.length % 256
60
59
 
61
- @stream.write [size1, size2].pack("U*")
60
+ @stream.write Common.encode_u16(data.length)
62
61
  @stream.write data
63
62
  end
64
63
 
65
64
  # Read a packet from the connection
66
65
  def read()
67
66
  size_a = @stream.read 2
68
- size = (size_a[0].ord << 8) + size_a[1].ord
67
+ size = Common.decode_u16(size_a)
69
68
  data = @stream.read size
70
69
 
71
70
  data = MessagePack.unpack data
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synacrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jD91mZM2
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2017-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,8 @@ files:
53
53
  - Rakefile
54
54
  - bin/console
55
55
  - bin/setup
56
- - lib/packets.rb
56
+ - lib/common.rb
57
+ - lib/encrypter.rb
57
58
  - lib/state.rb
58
59
  - lib/synacrb.rb
59
60
  - lib/synacrb/version.rb