Commit 11e561e

mo <mo.khan@gmail.com>
2017-11-26 18:40:11
extract method to convert algorithm to cipher.
1 parent 8b06632
Changed files (1)
lib
saml
lib/saml/kit/crypto/simple_cipher.rb
@@ -10,17 +10,8 @@ module Saml
         }
 
         def initialize(algorithm, key)
+          @algorithm = algorithm
           @key = key
-          @cipher = case algorithm
-                    when 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
-                      OpenSSL::Cipher.new('DES-EDE3-CBC')
-                    when 'http://www.w3.org/2001/04/xmlenc#aes128-cbc'
-                      OpenSSL::Cipher.new('AES-128-CBC')
-                    when 'http://www.w3.org/2001/04/xmlenc#aes192-cbc'
-                      OpenSSL::Cipher.new('AES-192-CBC')
-                    when 'http://www.w3.org/2001/04/xmlenc#aes256-cbc'
-                      OpenSSL::Cipher.new('AES-256-CBC')
-                    end
         end
 
         def self.matches?(algorithm)
@@ -28,17 +19,34 @@ module Saml
         end
 
         def decrypt(cipher_text)
-          @cipher.decrypt
-          iv = cipher_text[0..@cipher.iv_len-1]
-          data = cipher_text[@cipher.iv_len..-1]
-          #@cipher.padding = 0
-          @cipher.key = @key
-          @cipher.iv = iv
+          cipher = cipher_for(@algorithm)
+          cipher.decrypt
+          iv = cipher_text[0..cipher.iv_len-1]
+          data = cipher_text[cipher.iv_len..-1]
+          #cipher.padding = 0
+          cipher.key = @key
+          cipher.iv = iv
 
           Saml::Kit.logger.debug ['-key', @key].inspect
           Saml::Kit.logger.debug ['-iv', iv].inspect
 
-          @cipher.update(data) + @cipher.final
+          cipher.update(data) + cipher.final
+        end
+
+        private
+
+        def cipher_for(algorithm)
+          name = case algorithm
+                 when 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
+                   'DES-EDE3-CBC'
+                 when 'http://www.w3.org/2001/04/xmlenc#aes128-cbc'
+                   'AES-128-CBC'
+                 when 'http://www.w3.org/2001/04/xmlenc#aes192-cbc'
+                   'AES-192-CBC'
+                 when 'http://www.w3.org/2001/04/xmlenc#aes256-cbc'
+                   'AES-256-CBC'
+                 end
+          OpenSSL::Cipher.new(name)
         end
       end
     end