Sha256: 462ca36a6eeafdeee80d6c78fcf3d318b73373c8db893254d6c1ea58e4cf30a9

Contents?: true

Size: 781 Bytes

Versions: 4

Compression:

Stored size: 781 Bytes

Contents

module Clearance
  module PasswordStrategies
    module SHA1
      require 'digest/sha1'

      extend ActiveSupport::Concern

      def authenticated?(password)
        encrypted_password == encrypt(password)
      end

      def password=(new_password)
        @password = new_password
        initialize_salt_if_necessary

        if new_password.present?
          self.encrypted_password = encrypt(new_password)
        end
      end

      private

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

      def generate_hash(string)
        Digest::SHA1.hexdigest(string).encode 'UTF-8'
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
clearance-1.0.0.rc4 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0.rc3 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0.rc2 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0.rc1 lib/clearance/password_strategies/sha1.rb