forked from binarylogic/authlogic
/
sha1.rb
36 lines (32 loc) · 1.16 KB
/
sha1.rb
1
2
3
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
33
34
35
36
require "digest/sha1"
module Authlogic
module CryptoProviders
# = Sha1
#
# This class was made for the users transitioning from restful_authentication. I highly discourage using this crypto provider as it inferior to your other options.
# Please use the Sha512 crypto provider or the BCrypt provider.
class Sha1
class << self
def join_token
@join_token ||= "--"
end
attr_writer :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 ||= 10
end
attr_writer :stretches
# Turns your raw password into a Sha1 hash.
def encrypt(*tokens)
tokens = tokens.flatten
digest = tokens.shift
stretches.times { digest = Digest::SHA1.hexdigest([digest, *tokens].compact.join(join_token)) }
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