secure_carrot 0.1.2 → 0.2.0

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.
Files changed (4) hide show
  1. data/README.markdown +14 -1
  2. data/VERSION.yml +2 -2
  3. data/lib/amqp/queue.rb +25 -5
  4. metadata +4 -4
@@ -13,7 +13,7 @@ There is currently no way to prevent buffering using eventmachine. Support for p
13
13
 
14
14
  ## Example
15
15
 
16
- require 'carrot'
16
+ require 'secure_carrot'
17
17
 
18
18
  q = Carrot.queue('name')
19
19
  10.times do |num|
@@ -28,6 +28,19 @@ There is currently no way to prevent buffering using eventmachine. Support for p
28
28
  q.ack
29
29
  end
30
30
  Carrot.stop
31
+
32
+ ## Encrypting and Decrypting messages
33
+
34
+ Symmetric encryption is used here which means the same password is used for encrypting
35
+ and decrypting the message.
36
+
37
+ puts "Encrypt and send a message"
38
+ q.send_message('Hello Carrot', :password => 'secure')
39
+ #=> "qrbSyJHx6JhBQtXKsWvm/A==\n"
40
+
41
+ puts "Receiving and decrypting message. If you don't specify the password you will read an encrypted message."
42
+ q.receive_message(:password => 'secure')
43
+ #=> "Hello Carrot"
31
44
 
32
45
  # LICENSE
33
46
 
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 2
2
+ :patch: 0
3
3
  :build:
4
4
  :major: 0
5
- :minor: 1
5
+ :minor: 2
@@ -101,9 +101,26 @@ module Carrot::AMQP
101
101
 
102
102
  def send_message(data,opts={})
103
103
  opts.merge!(:persistent => true)
104
+ # Encrypt the message using the password supplied
105
+ data = encrypt_message(data, opts[:password]) if opts[:password]
104
106
  exchange.publish(data,opts)
105
107
  end
106
108
 
109
+
110
+ ##
111
+ # This message will decrypt the message if a password is passed.
112
+ # Else it will pop the message as it is.
113
+
114
+ def receive_message(opts={})
115
+ msg = pop(opts)
116
+ password = opts[:password]
117
+ if msg && password
118
+ decrypted_message = decrypt_message(msg, password)
119
+ decrypted_message
120
+ end
121
+ decrypted_message || msg
122
+ end
123
+
107
124
  def encrypt_message(message, password)
108
125
  encrypted_message = message.encrypt(:symmetric, :password => password)
109
126
  encrypted_message
@@ -117,19 +134,22 @@ module Carrot::AMQP
117
134
  ##
118
135
  # Is a wrapper around publish to send persistent and encrypted messages using symmetric key.
119
136
 
120
- def encrypt_and_send_message(message, password, opts={})
137
+ def encrypt_and_send_message(opts={})
121
138
  opts.merge!(:persistent => true)
122
- encrypted_message = encrypt_message(message, password)
139
+ encrypted_message = encrypt_message(opts[:message], opts[:password])
123
140
  exchange.publish(encrypted_message,opts)
124
141
  end
125
142
 
126
143
  ##
127
144
  # This method will receive and decrypt messages using symmetric key.
128
145
 
129
- def receive_and_decrypt_message(password, opts={})
146
+ def receive_and_decrypt_message(opts={})
130
147
  msg = pop(opts)
131
- decrypted_message = decrypt_message(msg, password)
132
- decrypted_message
148
+ unless msg.nil?
149
+ decrypted_message = decrypt_message(msg, opts[:password])
150
+ decrypted_message
151
+ end
152
+ decrypted_message || msg
133
153
  end
134
154
 
135
155
  private
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secure_carrot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sriram Varahan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 +05:30
18
+ date: 2010-09-09 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency