Sha256: 08866068452acf05b978266f4945dfa9aa7ccc4cc6297a8f9519adc3ebca6692

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module EncryptedAttributes
  # Adds support for dynamically generated salts
  class ShaCipher < EncryptedStrings::ShaCipher
    # Encrypts a string using a Secure Hash Algorithm (SHA), specifically SHA-1.
    # 
    # The <tt>:salt</tt> configuration option can be any one of the following types:
    # * +symbol+ - Calls the method on the object whose value is being encrypted
    # * +proc+ - A block that will be invoked, providing it with the object whose value is being encrypted
    # * +string+ - The actual salt value to use
    def initialize(object, value, operation, options = {}) #:nodoc:
      if operation == :write
        # Figure out the actual salt value
        if salt = options[:salt]
          options[:salt] =
            case salt
            when Symbol
              object.send(salt)
            when Proc
              salt.call(object)
            else
              salt
            end
        end
        
        # Track whether or not the salt was generated dynamically
        @dynamic_salt = salt != options[:salt]
        
        super(options)
      else
        # The salt is at the end of the value if it's dynamic
        salt = value[40..-1]
        if @dynamic_salt = !salt.blank?
          options[:salt] = salt 
        end
        
        super(options)
      end
    end
    
    # Encrypts the data, appending the salt to the end of the string if it
    # was created dynamically
    def encrypt(data)
      encrypted_data = super
      encrypted_data << salt if @dynamic_salt
      encrypted_data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encrypted_attributes-0.3.0 lib/encrypted_attributes/sha_cipher.rb