Sha256: 9dc714d606c3817ae20e49e18e98c530574d1ae95ced33e3532362dea5608e13

Contents?: true

Size: 983 Bytes

Versions: 2

Compression:

Stored size: 983 Bytes

Contents

require 'digest/sha1'

module Clearance
  module PasswordStrategies
    module SHA1
      # Am I authenticated with given password?
      #
      # @param [String] plain-text password
      # @return [true, false]
      # @example
      #   user.authenticated?('password')
      def authenticated?(password)
        encrypted_password == encrypt(password)
      end

      protected

      def encrypt_password
        initialize_salt_if_necessary
        if password.present?
          self.encrypted_password = encrypt(password)
        end
      end

      def generate_hash(string)
        if RUBY_VERSION >= '1.9'
          Digest::SHA1.hexdigest(string).encode('UTF-8')
        else
          Digest::SHA1.hexdigest(string)
        end
      end

      def encrypt(string)
        generate_hash("--#{salt}--#{string}--")
      end

      def initialize_salt_if_necessary
        if salt.blank?
          self.salt = generate_random_code
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clearance-0.16.3 lib/clearance/password_strategies/sha1.rb
clearance-0.16.2 lib/clearance/password_strategies/sha1.rb