Sha256: f7dc345038648762aa3a304865f1a34157751476f2d2c61cd4c66d18f4e152e7

Contents?: true

Size: 1014 Bytes

Versions: 13

Compression:

Stored size: 1014 Bytes

Contents

require 'digest/sha1'
module Sentry
  class ShaSentry
    @@salt = 'salt'
    attr_accessor :salt
    
    # Encrypts data using SHA.
    def encrypt(data)
      self.class.encrypt(data + salt.to_s)
    end
    
    # Initialize the class.  
    # Used by ActiveRecord::Base#generates_crypted to set up as a callback object for a model
    def initialize(attribute = nil)
      @attribute = attribute
    end
    
    # Performs encryption on before_validation Active Record callback
    def before_validation(model)
      return unless model.send(@attribute)
      model.send("crypted_#{@attribute}=", encrypt(model.send(@attribute)))
    end
        
    class << self
      # Gets the class salt value used when encrypting
      def salt
        @@salt
      end
      
      # Sets the class salt value used when encrypting
      def salt=(value)
        @@salt = value
      end
      
      # Encrypts the data
      def encrypt(data)
        Digest::SHA1.hexdigest(data + @@salt)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 5 rubygems

Version Path
loyal3-sentry-0.4.2 lib/sentry/sha_sentry.rb
loyal3-sentry-0.4.3 lib/sentry/sha_sentry.rb
loyal3-sentry-0.4.4 lib/sentry/sha_sentry.rb
loyal3-sentry-0.5.0 lib/sentry/sha_sentry.rb
loyal3-sentry-0.5.2 lib/sentry/sha_sentry.rb
pivotal-sentry-0.4.0 lib/sentry/sha_sentry.rb
shipstar-sentry-0.5.2.1 lib/sentry/sha_sentry.rb
stderr-sentry-0.5.4 lib/sentry/sha_sentry.rb
sentry-0.5.3 lib/sentry/sha_sentry.rb
sentry-0.2.8 lib/sentry/sha_sentry.rb
sentry-0.3.1 lib/sentry/sha_sentry.rb
sentry-0.2.9 lib/sentry/sha_sentry.rb
sentry-0.3 lib/sentry/sha_sentry.rb