forked from thoughtbot/clearance
/
sha1.rb
42 lines (33 loc) · 854 Bytes
/
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
37
38
39
40
41
42
module Clearance
module PasswordStrategies
module SHA1
require 'digest/sha1'
extend ActiveSupport::Concern
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
private
def encrypt(string)
generate_hash "--#{salt}--#{string}--"
end
def generate_hash(string)
Digest::SHA1.hexdigest(string).encode 'UTF-8'
end
def initialize_salt_if_necessary
if salt.blank?
self.salt = generate_salt
end
end
def generate_salt
SecureRandom.hex(20).encode('UTF-8')
end
end
end
end