Class: NetworkClipboard::AESConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/network_clipboard/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, socket) ⇒ AESConnection

Returns a new instance of AESConnection.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/network_clipboard/connection.rb', line 19

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_idObject (readonly)

Returns the value of attribute remote_client_id.



17
18
19
# File 'lib/network_clipboard/connection.rb', line 17

def remote_client_id
  @remote_client_id
end

#socketObject (readonly)

Returns the value of attribute socket.



17
18
19
# File 'lib/network_clipboard/connection.rb', line 17

def socket
  @socket
end

Instance Method Details

#close_readObject



68
69
70
# File 'lib/network_clipboard/connection.rb', line 68

def close_read
  @socket.close_read
end

#close_writeObject



72
73
74
# File 'lib/network_clipboard/connection.rb', line 72

def close_write
  @socket.close_write
end

#handshake(client_id) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/network_clipboard/connection.rb', line 33

def handshake(client_id)
  iv = @encryptor.random_iv
  @socket.send([iv.size].pack('N'),0)
  @socket.send(iv,0)
  iv_size = @socket.read(4).unpack('N')[0]
  @decryptor.iv = @socket.read(iv_size)

  # Verify it all worked.
  send(HANDSHAKE_STRING)
  raise HandshakeException unless receive == HANDSHAKE_STRING

  send(client_id)
  @remote_client_id = receive
end

#receiveObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/network_clipboard/connection.rb', line 55

def receive()
  size_bits = @socket.read(4)
  if size_bits.nil? and @socket.eof?
    raise DisconnectedError
  end
  ciphertext_size = size_bits.unpack('N')[0]
  ciphertext = @socket.read(ciphertext_size)
  plaintext = @decryptor.update(ciphertext) + @decryptor.final
  @decryptor.reset

  return plaintext
end

#send(new_content) ⇒ Object



48
49
50
51
52
53
# File 'lib/network_clipboard/connection.rb', line 48

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