lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-3.4.0 vs lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-3.6.0

- old
+ new

@@ -31,13 +31,13 @@ # Set the Primary Symmetric Cipher to be used # # Example: For testing purposes the following test cipher can be used: # # SymmetricEncryption.cipher = SymmetricEncryption::Cipher.new( - # :key => '1234567890ABCDEF1234567890ABCDEF', - # :iv => '1234567890ABCDEF', - # :cipher => 'aes-128-cbc' + # key: '1234567890ABCDEF1234567890ABCDEF', + # iv: '1234567890ABCDEF', + # cipher: 'aes-128-cbc' # ) def self.cipher=(cipher) raise "Cipher must be similar to SymmetricEncryption::Ciphers" unless cipher.nil? || (cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt)) @@cipher = cipher end @@ -309,11 +309,11 @@ File.open(iv_filename, 'wb') {|file| file.write( rsa_key.public_encrypt(key_pair[:iv]) ) } puts("Generated new Symmetric Key for encryption. Please copy #{iv_filename} to the other web servers in #{environment}.") elsif !cipher_cfg[:iv] iv = rsa_key.public_encrypt(key_pair[:iv]) puts "Generated new Symmetric Key for encryption. Set the IV environment variable in #{environment} to:" - puts ::Base64.encode64(key) + puts ::Base64.encode64(iv) end end # Generate a 22 character random password def self.random_password @@ -411,11 +411,11 @@ # Load Encrypted Symmetric keys if key_filename = config.delete(:key_filename) raise "Missing mandatory config parameter :private_rsa_key when :key_filename is supplied" unless rsa encrypted_key = begin - File.read(key_filename, :open_args => ['rb']) + File.open(key_filename, 'rb'){|f| f.read} rescue Errno::ENOENT puts "\nSymmetric Encryption key file: '#{key_filename}' not found or readable." puts "To generate the keys for the first time run: rails generate symmetric_encryption:new_keys\n\n" return end @@ -423,11 +423,11 @@ end if iv_filename = config.delete(:iv_filename) raise "Missing mandatory config parameter :private_rsa_key when :iv_filename is supplied" unless rsa encrypted_iv = begin - File.read(iv_filename, :open_args => ['rb']) if iv_filename + File.open(iv_filename, 'rb'){|f| f.read} if iv_filename rescue Errno::ENOENT puts "\nSymmetric Encryption initialization vector file: '#{iv_filename}' not found or readable." puts "To generate the keys for the first time run: rails generate symmetric_encryption:new_keys\n\n" return end @@ -436,17 +436,27 @@ if encrypted_key = config.delete(:encrypted_key) raise "Missing mandatory config parameter :private_rsa_key when :encrypted_key is supplied" unless rsa # Decode value first using encoding specified encrypted_key = ::Base64.decode64(encrypted_key) + if !encrypted_key || encrypted_key.empty? + puts "\nSymmetric Encryption encrypted_key not found." + puts "To generate the keys for the first time run: rails generate symmetric_encryption:new_keys\n\n" + return + end config[:key] = rsa.private_decrypt(encrypted_key) end if encrypted_iv = config.delete(:encrypted_iv) raise "Missing mandatory config parameter :private_rsa_key when :encrypted_iv is supplied" unless rsa # Decode value first using encoding specified encrypted_iv = ::Base64.decode64(encrypted_iv) + if !encrypted_key || encrypted_key.empty? + puts "\nSymmetric Encryption encrypted_iv not found." + puts "To generate the keys for the first time run: rails generate symmetric_encryption:new_keys\n\n" + return + end config[:iv] = rsa.private_decrypt(encrypted_iv) end # Backward compatibility if old_key_name_cipher = config.delete(:cipher) @@ -458,11 +468,11 @@ end # Coerce given value into given type # Does not coerce json or yaml values def self.coerce(value, type, from_type=nil) - return if value.nil? + return if value.nil? || (value.is_a?(String) && (value !~ /[^[:space:]]/)) from_type ||= value.class case type when :json value @@ -517,21 +527,21 @@ COERCION_TYPE_MAP[symbol] end end COERCION_TYPE_MAP = { - :string => String, - :integer => Integer, - :float => Float, - :decimal => BigDecimal, - :datetime => DateTime, - :time => Time, - :date => Date + string: String, + integer: Integer, + float: Float, + decimal: BigDecimal, + datetime: DateTime, + time: Time, + date: Date } # With Ruby 1.9 strings have encodings if defined?(Encoding) BINARY_ENCODING = Encoding.find("binary") UTF8_ENCODING = Encoding.find("UTF-8") end -end \ No newline at end of file +end