Class: Nuntius::Messenger

Inherits:
Object
  • Object
show all
Defined in:
lib/nuntius/messenger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Messenger

Returns a new instance of Messenger.



5
6
7
# File 'lib/nuntius/messenger.rb', line 5

def initialize(attributes)
  self.key = attributes[:key]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/nuntius/messenger.rb', line 3

def key
  @key
end

Instance Method Details

#unwrap(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nuntius/messenger.rb', line 23

def unwrap(options)
  data = options[:envelope].raw_data
  signature = options[:envelope].raw_signature

  options[:from].validate(data, signature)

  key = @key.decrypt options[:envelope].raw_key

  cipher = OpenSSL::Cipher.new('AES-256-CBC').decrypt
  cipher.key = key

  cipher.update(data) + cipher.final
end

#wrap(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nuntius/messenger.rb', line 9

def wrap(options)
  cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt

  key = options[:to].encrypt( cipher.random_key )
  data = cipher.update( options[:message] ) + cipher.final
  signature = @key.sign(data)

  Envelope.new({
    :raw_data => data,
    :raw_key => key,
    :raw_signature => signature 
  })
end