Skip to content

Commit

Permalink
Adding SHA256 as a crypto provider
Browse files Browse the repository at this point in the history
  • Loading branch information
vinibaggio authored and binarylogic committed Oct 20, 2009
1 parent 36ad855 commit 0ac3088
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/authlogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require File.dirname(__FILE__) + "/authlogic/crypto_providers/md5"
require File.dirname(__FILE__) + "/authlogic/crypto_providers/sha1"
require File.dirname(__FILE__) + "/authlogic/crypto_providers/sha256"
require File.dirname(__FILE__) + "/authlogic/crypto_providers/sha512"
require File.dirname(__FILE__) + "/authlogic/crypto_providers/bcrypt"
require File.dirname(__FILE__) + "/authlogic/crypto_providers/aes256"
Expand Down
50 changes: 50 additions & 0 deletions lib/authlogic/crypto_providers/sha256.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "digest/sha2"

module Authlogic
# The acts_as_authentic method has a crypto_provider 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. This is ten because that is what restful_authentication defaults to.
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
end
end
end
end
14 changes: 14 additions & 0 deletions test/crypto_provider_test/sha256_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require File.dirname(__FILE__) + '/../test_helper.rb'

module CryptoProviderTest
class Sha256Test < ActiveSupport::TestCase
def test_encrypt
assert Authlogic::CryptoProviders::Sha256.encrypt("mypass")
end

def test_matches
hash = Authlogic::CryptoProviders::Sha256.encrypt("mypass")
assert Authlogic::CryptoProviders::Sha256.matches?(hash, "mypass")
end
end
end

0 comments on commit 0ac3088

Please sign in to comment.