Commit 74778b9

mo <mo.khan@gmail.com>
2017-11-26 18:43:12
replace case statement with hash lookup.
1 parent 11e561e
Changed files (1)
lib
saml
lib/saml/kit/crypto/simple_cipher.rb
@@ -3,15 +3,15 @@ module Saml
     module Crypto
       class SimpleCipher
         ALGORITHMS = {
-          'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' => true,
-          'http://www.w3.org/2001/04/xmlenc#aes128-cbc' => true,
-          'http://www.w3.org/2001/04/xmlenc#aes192-cbc' => true,
-          'http://www.w3.org/2001/04/xmlenc#aes256-cbc' => true,
+          'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' => 'DES-EDE3-CBC',
+          'http://www.w3.org/2001/04/xmlenc#aes128-cbc' => 'AES-128-CBC',
+          'http://www.w3.org/2001/04/xmlenc#aes192-cbc' => 'AES-192-CBC',
+          'http://www.w3.org/2001/04/xmlenc#aes256-cbc' => 'AES-256-CBC',
         }
 
-        def initialize(algorithm, key)
+        def initialize(algorithm, private_key)
           @algorithm = algorithm
-          @key = key
+          @private_key = private_key
         end
 
         def self.matches?(algorithm)
@@ -19,35 +19,19 @@ module Saml
         end
 
         def decrypt(cipher_text)
-          cipher = cipher_for(@algorithm)
+          cipher = OpenSSL::Cipher.new(ALGORITHMS[@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.key = @private_key
           cipher.iv = iv
 
-          Saml::Kit.logger.debug ['-key', @key].inspect
+          Saml::Kit.logger.debug ['-key', @private_key].inspect
           Saml::Kit.logger.debug ['-iv', iv].inspect
 
           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
   end