lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-4.0.0 vs lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-4.0.1

- old
+ new

@@ -6,11 +6,10 @@ # Encrypt using 256 Bit AES CBC symmetric key and initialization vector # The symmetric key is protected using the private key below and must # be distributed separately from the application module SymmetricEncryption - # Defaults @@cipher = nil @@secondary_ciphers = [] @@select_cipher = nil @@ -24,11 +23,11 @@ # :datetime => DateTime # :time => Time # :date => Date # :json => Uses JSON serialization, useful for hashes and arrays # :yaml => Uses YAML serialization, useful for hashes and arrays - COERCION_TYPES = [:string, :integer, :float, :decimal, :datetime, :time, :date, :boolean, :json, :yaml] + COERCION_TYPES = %i[string integer float decimal datetime time date boolean json yaml].freeze # Set the Primary Symmetric Cipher to be used # # Example: For testing purposes the following test cipher can be used: # @@ -46,14 +45,19 @@ # Returns the Primary Symmetric Cipher being used # 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(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless cipher? + unless cipher? + raise( + SymmetricEncryption::ConfigError, + 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data' + ) + end return @@cipher if version.nil? || (@@cipher.version == version) - secondary_ciphers.find { |c| c.version == version } || (@@cipher if version == 0) + secondary_ciphers.find { |c| c.version == version } || (@@cipher if version.zero?) end # Returns whether a primary cipher has been set def self.cipher? !@@cipher.nil? @@ -137,13 +141,11 @@ end c.binary_decrypt(decoded) end # Try to force result to UTF-8 encoding, but if it is not valid, force it back to Binary - unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding? - decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING) - end + decrypted.force_encoding(SymmetricEncryption::BINARY_ENCODING) unless decrypted.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding? Coerce.coerce_from_string(decrypted, type) end # Returns the header for the encrypted string # Returns [nil] if no header is present @@ -153,10 +155,10 @@ # Decode before decrypting supplied string decoded = cipher.encoder.decode(encrypted_and_encoded_string.to_s) return if decoded.nil? || decoded.empty? h = Header.new - h.parse(decoded) == 0 ? nil : h + h.parse(decoded).zero? ? nil : h end # AES Symmetric Encryption of supplied string # Returns result as a Base64 encoded string # Returns nil if the supplied str is nil