lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-1.0.0 vs lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-1.1.1

- old
+ new

@@ -20,21 +20,22 @@ # :key => '1234567890ABCDEF1234567890ABCDEF', # :iv => '1234567890ABCDEF', # :cipher => 'aes-128-cbc' # ) def self.cipher=(cipher) - raise "Cipher must be similar to SymmetricEncryption::Ciphers" unless cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt) + raise "Cipher must be similar to SymmetricEncryption::Ciphers" unless cipher.nil? || (cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt)) @@cipher = cipher end # Returns the Primary Symmetric Cipher being used - # If a version is supplied, then the cipher matching that version will be - # returned or nil if no match was found - def self.cipher(version = 0) + # If a version is supplied + # Returns the primary cipher if no match was found and version == 0 + # Returns nil if no match was found and version != 0 + def self.cipher(version = nil) raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher - return @@cipher if version.nil? || (version == 0) || (@@cipher.version == version) - secondary_ciphers.find {|c| c.version == version} + return @@cipher if version.nil? || (@@cipher.version == version) + secondary_ciphers.find {|c| c.version == version} || (@@cipher if version == 0) end # Set the Secondary Symmetric Ciphers Array to be used def self.secondary_ciphers=(secondary_ciphers) raise "secondary_ciphers must be a collection" unless secondary_ciphers.respond_to? :each @@ -225,11 +226,12 @@ iv_filename = cipher_cfg['iv_filename'] || cipher_cfg['symmetric_iv_filename'] { :cipher => cipher_cfg['cipher'] || default_cipher, :key_filename => key_filename, :iv_filename => iv_filename, - :encoding => cipher_cfg['encoding'] + :encoding => cipher_cfg['encoding'], + :version => cipher_cfg['version'] } end else # Migrate old format config @@ -285,10 +287,11 @@ iv = rsa.private_decrypt(encrypted_iv) if iv_filename Cipher.new( :key => rsa.private_decrypt(encrypted_key), :iv => iv, :cipher => cipher_conf[:cipher], - :encoding => cipher_conf[:encoding] + :encoding => cipher_conf[:encoding], + :version => cipher_conf[:version] ) end # With Ruby 1.9 strings have encodings if defined?(Encoding) \ No newline at end of file