Module: Sorcery::CryptoProviders::Common

Included in:
MD5, SHA1, SHA256, SHA512
Defined in:
lib/sorcery/crypto_providers/common.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) included(base)



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sorcery/crypto_providers/common.rb', line 4

def self.included(base)
  base.class_eval do
    class << self
      attr_accessor :join_token

      # The number of times to loop through the encryption.
      def stretches
        @stretches ||= 1
      end
      attr_writer :stretches

      def encrypt(*tokens)
        digest = tokens.flatten.compact.join(join_token)
        stretches.times { digest = secure_digest(digest) }
        digest
      end

      # Does the crypted password match the tokens? Uses the same tokens that were used to encrypt.
      def matches?(crypted, *tokens)
        encrypt(*tokens.compact) == crypted
      end

      def reset!
        @stretches = 1
        @join_token = nil
      end
    end
  end
end