Sha256: 1a1ab2d906f87fc94ed69f25928e01632f0b9a87cde68cba9a515f305423c721

Contents?: true

Size: 854 Bytes

Versions: 21

Compression:

Stored size: 854 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_salt
        end
      end

      def generate_salt
        SecureRandom.hex(20).encode('UTF-8')
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
clearance-1.9.0 lib/clearance/password_strategies/sha1.rb
clearance-1.8.1 lib/clearance/password_strategies/sha1.rb
clearance-1.8.0 lib/clearance/password_strategies/sha1.rb
clearance-1.7.0 lib/clearance/password_strategies/sha1.rb
clearance-1.6.1 lib/clearance/password_strategies/sha1.rb
clearance-1.6.0 lib/clearance/password_strategies/sha1.rb
clearance-1.5.1 lib/clearance/password_strategies/sha1.rb
clearance-1.5.0 lib/clearance/password_strategies/sha1.rb
clearance-1.4.3 lib/clearance/password_strategies/sha1.rb
clearance-1.4.2 lib/clearance/password_strategies/sha1.rb
clearance-1.4.1 lib/clearance/password_strategies/sha1.rb
clearance-1.4.0 lib/clearance/password_strategies/sha1.rb
clearance-1.3.0 lib/clearance/password_strategies/sha1.rb
clearance-1.2.1 lib/clearance/password_strategies/sha1.rb
clearance-1.2.0 lib/clearance/password_strategies/sha1.rb
clearance-1.1.0 lib/clearance/password_strategies/sha1.rb
clearance-1.0.1 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0.rc8 lib/clearance/password_strategies/sha1.rb
clearance-1.0.0.rc7 lib/clearance/password_strategies/sha1.rb