Class: NetworkClipboard::AESConnection
- Inherits:
-
Object
- Object
- NetworkClipboard::AESConnection
- Defined in:
- lib/network_clipboard/connection.rb
Instance Attribute Summary collapse
-
#remote_client_id ⇒ Object
readonly
Returns the value of attribute remote_client_id.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #close ⇒ Object
- #handshake(client_id) ⇒ Object
-
#initialize(config, socket) ⇒ AESConnection
constructor
A new instance of AESConnection.
- #receive(blocking = true) ⇒ Object
- #send(new_content) ⇒ Object
Constructor Details
#initialize(config, socket) ⇒ AESConnection
Returns a new instance of AESConnection.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/network_clipboard/connection.rb', line 14 def initialize(config,socket) @socket = socket @encryptor = OpenSSL::Cipher::AES.new(128, :CBC) @encryptor.encrypt @encryptor.key = config.secret @decryptor = OpenSSL::Cipher::AES.new(128, :CBC) @decryptor.decrypt @decryptor.key = config.secret handshake(config.client_id) end |
Instance Attribute Details
#remote_client_id ⇒ Object (readonly)
Returns the value of attribute remote_client_id.
12 13 14 |
# File 'lib/network_clipboard/connection.rb', line 12 def remote_client_id @remote_client_id end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
12 13 14 |
# File 'lib/network_clipboard/connection.rb', line 12 def socket @socket end |
Instance Method Details
#close ⇒ Object
76 77 78 |
# File 'lib/network_clipboard/connection.rb', line 76 def close @socket.close end |
#handshake(client_id) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/network_clipboard/connection.rb', line 28 def handshake(client_id) iv = @encryptor.random_iv @socket.send([iv.size].pack('N'),0) @socket.send(iv,0) iv_size = @socket.recv(4).unpack('N')[0] @decryptor.iv = @socket.recv(iv_size) # Verify it all worked. send(HANDSHAKE_STRING) raise HandshakeException unless receive == HANDSHAKE_STRING send(client_id) @remote_client_id = receive end |
#receive(blocking = true) ⇒ Object
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 |
# File 'lib/network_clipboard/connection.rb', line 50 def receive(blocking=true) begin @partial_read ||= '' while @partial_read.size < 4 @partial_read += @socket.recv_nonblock(4 - @partial_read.size) end while @partial_read.size < (total_size = 4 + @partial_read.unpack('N')[0]) @partial_read += @socket.recv_nonblock(total_size - @partial_read.size) end rescue IO::WaitReadable if blocking IO.select([@socket]) retry else return nil end end ciphertext,@partial_read = @partial_read.slice(4,@partial_read.size-4),nil plaintext = @decryptor.update(ciphertext) + @decryptor.final @decryptor.reset return plaintext end |
#send(new_content) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/network_clipboard/connection.rb', line 43 def send(new_content) ciphertext = @encryptor.update(new_content) + @encryptor.final @socket.send([ciphertext.size].pack('N'),0) @socket.send(ciphertext,0) @encryptor.reset end |