Sha256: b5665e13e93c3051c90e40e604e99a19774db6e077144ab71ffb59927f1b3f74

Contents?: true

Size: 845 Bytes

Versions: 4

Compression:

Stored size: 845 Bytes

Contents

require 'openssl'

module Clearance
  module PasswordStrategies
    module Blowfish
      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

      protected

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

      def generate_hash(string)
        cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').encrypt
        cipher.key = Digest::SHA256.digest(salt)
        cipher.update(string) << cipher.final
      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/blowfish.rb
clearance-1.0.0.rc3 lib/clearance/password_strategies/blowfish.rb
clearance-1.0.0.rc2 lib/clearance/password_strategies/blowfish.rb
clearance-1.0.0.rc1 lib/clearance/password_strategies/blowfish.rb