Sha256: dcc76a7496db6e3cbab3ede6fa7a35c4fa040c86c4caf7055c5b1bc9d1d921fd

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module PluginAWeek #:nodoc:
  module EncryptedAttributes
    # Supports encryption for ActiveRecord models by adding dynamically generated
    # salts
    class ShaEncryptor < PluginAWeek::EncryptedStrings::ShaEncryptor
      def initialize(record, value, operation, options = {}) #:nodoc:
        if operation == :write
          # Figure out the actual salt value
          if salt = options[:salt]
            options[:salt] =
              case salt
              when Symbol
                record.send(salt).to_s
              when Proc
                salt.call(record).to_s
              else
                salt
              end
          end
          
          @dynamic_salt = salt != options[:salt]
          super(options)
        else
          # The salt is in the value if it's dynamic
          salt = value[40..-1]
          if @dynamic_salt = !salt.blank?
            options.merge!(:salt => salt) 
          end
          
          super(options)
        end
      end
      
      # Encrypts the data, appending the salt to the end of the string
      def encrypt(data)
        encrypted_data = Digest::SHA1.hexdigest(data + salt)
        encrypted_data << salt if @dynamic_salt
        encrypted_data
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
encrypted_attributes-0.1.0 lib/encrypted_attributes/sha_encryptor.rb
encrypted_attributes-0.1.1 lib/encrypted_attributes/sha_encryptor.rb