Module: Cryptosphere::Handshake

Defined in:
lib/cryptosphere/protocol/handshake.rb

Class Method Summary collapse

Class Method Details

.decode_request(recipient, message) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/cryptosphere/protocol/handshake.rb', line 12

def decode_request(recipient, message)
  bytes = PUBKEY_SIZE / 8
  signature, message = message[0...bytes], message[bytes..-1]

  # FIXME: this should be encrypted :(
  sender_key = message
  Cryptosphere.verify!(sender_key, message, signature)

  sender_key
end

.decode_response(recipient, sender, message) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cryptosphere/protocol/handshake.rb', line 29

def decode_response(recipient, sender, message)
  bytes = PUBKEY_SIZE / 8
  signature, message = message[0...bytes], message[bytes..-1]
  Cryptosphere.verify!(sender.public_key, message, signature)
  cipher = AsymmetricCipher.new(recipient.private_key)
  cipher.private_decrypt(message)
end

.encode_request(sender, recipient) ⇒ Object



5
6
7
8
9
10
# File 'lib/cryptosphere/protocol/handshake.rb', line 5

def encode_request(sender, recipient)
  # TODO: encrypt sender's public key
  # Sure would be nice to have some Curve25519 here
  message = sender.public_key
  Cryptosphere.sign(sender.private_key, message) + message
end

.encode_response(sender, recipient, secret = Cryptosphere.random_bytes(32)) ⇒ Object



23
24
25
26
27
# File 'lib/cryptosphere/protocol/handshake.rb', line 23

def encode_response(sender, recipient, secret = Cryptosphere.random_bytes(32))
  cipher = AsymmetricCipher.new(recipient.public_key)
  message = cipher.public_encrypt(secret)
  Cryptosphere.sign(sender.private_key, message) + message
end