Commits

Kaspar Schiess committed faf511c

Refactors user hash into a user lookup for Decrypt

Comments (0)

Files changed (4)

lib/event_shipper/benchmark.rb

       @s.wrap Filter::Encrypt.new('user', 'password')
 
       @r = UDP.new('localhost', 5050)
-      @r.wrap Filter::Decrypt.new('user' => 'password')
+      @r.wrap Filter::Decrypt.new { 'password' }
     end
 
     def execute

lib/event_shipper/filter/encrypt.rb

   class Decrypt
     include EventShipper::Protocol
     
-    def initialize user_db
-      @user_db = user_db.
-        inject({}) { |h, (user, pwd)| 
-          h[user] = AES256.new(pwd); h }
+    def initialize &user_lookup
+      @user_cache = Hash.new { |h, u|
+        password = user_lookup.call(u)
+        h[u] = AES256.new(password) }
     end
 
     def de encrypted_string, attributes={}
       message = parse_encrypted(encrypted_string)
 
-      if algo = @user_db[message.user]
+      if algo = @user_cache[message.user]
         transmission = algo.dec(
           message.salt, 
           message.iv, 

lib/event_shipper/proxy.rb

         users = configuration['users']
         @userdb = Hash[users.map { |str| str.partition('/').values_at(0,2) }]
 
-        transport.wrap Filter::Decrypt.new(userdb)
+        transport.wrap Filter::Decrypt.new { |user| @userdb[user] }
       end
 
       redis_config = configuration['redis']

spec/lib/event_shipper/filter/encrypt_spec.rb

   include EventShipper::Protocol
 
   let(:encrypt) { EventShipper::Filter::Encrypt.new('test', 'password') }
-  let(:decrypt) { EventShipper::Filter::Decrypt.new('test' => 'password') }
+  let(:decrypt) { EventShipper::Filter::Decrypt.new { |user| 'password'} }
   let(:attrs) { Hash.new }
 
   it 'encrypt and decrypt messages' do