Sha256: dcc0e601be0b50cb38bc82499a0b3fde322c8ada789d1d6301c359c975e342c8

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

require "digest/sha2"

module Sorcery
  # The activate_sorcery method has a custom_crypto_provider configuration option. This allows you to use any type of encryption you like.
  # Just create a class with a class level encrypt and matches? method. See example below.
  #
  # === Example
  #
  #   class MyAwesomeEncryptionMethod
  #     def self.encrypt(*tokens)
  #       # the tokens passed will be an array of objects, what type of object is irrelevant,
  #       # just do what you need to do with them and return a single encrypted string.
  #       # for example, you will most likely join all of the objects into a single string and then encrypt that string
  #     end
  #
  #     def self.matches?(crypted, *tokens)
  #       # return true if the crypted string matches the tokens.
  #       # depending on your algorithm you might decrypt the string then compare it to the token, or you might
  #       # encrypt the tokens and make sure it matches the crypted string, its up to you
  #     end
  #   end
  module CryptoProviders
    # = Sha256
    #
    # Uses the Sha256 hash algorithm to encrypt passwords.
    class SHA256
      class << self
        attr_accessor :join_token
        
        # The number of times to loop through the encryption.
        def stretches
          @stretches ||= 20
        end
        attr_writer :stretches
        
        # Turns your raw password into a Sha256 hash.
        def encrypt(*tokens)
          digest = tokens.flatten.join(join_token)
          stretches.times { digest = Digest::SHA256.hexdigest(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) == crypted
        end
        
        def reset!
          @stretches = 20
          @join_token = nil
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sorcery-0.3.1 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.3.0 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.2.1 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.2.0 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.1.4 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.1.3 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.1.2 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.1.1 lib/sorcery/crypto_providers/sha256.rb
sorcery-0.1.0 lib/sorcery/crypto_providers/sha256.rb