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

- old
+ new

@@ -36,20 +36,20 @@ # 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)) + raise(ArgumentError, 'Cipher must respond to :encrypt and :decrypt') 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 # 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 + raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher return @@cipher if version.nil? || (@@cipher.version == version) secondary_ciphers.find {|c| c.version == version} || (@@cipher if version == 0) end # Returns whether a primary cipher has been set @@ -57,13 +57,13 @@ !@@cipher.nil? 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 + raise(ArgumentError, "secondary_ciphers must be a collection") unless secondary_ciphers.respond_to? :each secondary_ciphers.each do |cipher| - raise "secondary_ciphers can only consist of SymmetricEncryption::Ciphers" unless cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt) + raise(ArgumentError, "secondary_ciphers can only consist of SymmetricEncryption::Ciphers") unless cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt) end @@secondary_ciphers = secondary_ciphers end # Returns the Primary Symmetric Cipher being used @@ -104,11 +104,11 @@ # to decrypt the current string. This is because in a very small # yet significant number of cases it is possible to decrypt data using # the incorrect key. Clearly the data returned is garbage, but it still # successfully returns a string of data def self.decrypt(encrypted_and_encoded_string, version=nil, type=:string) - raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher + raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher return encrypted_and_encoded_string if encrypted_and_encoded_string.nil? || (encrypted_and_encoded_string == '') str = encrypted_and_encoded_string.to_s # Decode before decrypting supplied string @@ -174,11 +174,11 @@ # non-string values to string values. # Note: If type is set to something other than :string, it's expected that # the coercible gem is available in the path. # Default: :string def self.encrypt(str, random_iv=false, compress=false, type=:string) - raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher + raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher # Encrypt and then encode the supplied string @@cipher.encrypt(coerce_to_string(str, type), random_iv, compress) end @@ -192,14 +192,14 @@ # YAML config files that contain encrypted development and production passwords # # WARNING: It is possible to decrypt data using the wrong key, so the value # returned should not be relied upon def self.try_decrypt(str) - raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher + raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher begin decrypt(str) - rescue OpenSSL::Cipher::CipherError + rescue OpenSSL::Cipher::CipherError, SymmetricEncryption::CipherError nil end end # Returns [true|false] as to whether the data could be decrypted @@ -208,11 +208,11 @@ # # WARNING: This method can only be relied upon if the encrypted data includes the # symmetric encryption header. In some cases data decrypted using the # wrong key will decrypt and return garbage def self.encrypted?(encrypted_data) - raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher + raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher # For now have to decrypt it fully result = try_decrypt(encrypted_data) !(result.nil? || result == '') end @@ -274,11 +274,11 @@ config_filename = filename || File.join(Rails.root, "config", "symmetric-encryption.yml") config = YAML.load(ERB.new(File.new(config_filename).read).result)[environment || Rails.env] # RSA key to decrypt key files private_rsa_key = config.delete('private_rsa_key') - raise "The configuration file must contain a 'private_rsa_key' parameter to generate symmetric keys" unless private_rsa_key + raise(SymmetricEncryption::ConfigError, "The configuration file must contain a 'private_rsa_key' parameter to generate symmetric keys") unless private_rsa_key rsa_key = OpenSSL::PKey::RSA.new(private_rsa_key) # Check if config file contains 1 or multiple ciphers ciphers = config.delete('ciphers') cfg = ciphers.nil? ? config : ciphers.first @@ -409,11 +409,11 @@ # To decrypt encrypted key or iv files rsa = OpenSSL::PKey::RSA.new(private_rsa_key) if private_rsa_key # 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 + raise(SymmetricEncryption::ConfigError, "Missing mandatory config parameter :private_rsa_key when :key_filename is supplied") unless rsa encrypted_key = begin 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" @@ -421,11 +421,11 @@ end config[:key] = rsa.private_decrypt(encrypted_key) end if iv_filename = config.delete(:iv_filename) - raise "Missing mandatory config parameter :private_rsa_key when :iv_filename is supplied" unless rsa + raise(SymmetricEncryption::ConfigError, "Missing mandatory config parameter :private_rsa_key when :iv_filename is supplied") unless rsa encrypted_iv = begin 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" @@ -433,11 +433,11 @@ end config[:iv] = rsa.private_decrypt(encrypted_iv) end if encrypted_key = config.delete(:encrypted_key) - raise "Missing mandatory config parameter :private_rsa_key when :encrypted_key is supplied" unless rsa + raise(SymmetricEncryption::ConfigError, "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" @@ -445,10 +445,10 @@ 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 + raise(SymmetricEncryption::ConfigError, "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"