Class: PasswordRehasher

Inherits:
Object
  • Object
show all
Defined in:
lib/password_rehasher.rb

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.hash_password(plaintext_password) ⇒ Object



30
31
32
# File 'lib/password_rehasher.rb', line 30

def self.hash_password(plaintext_password)
	SCrypt::Password.create(plaintext_password).to_s
end

.nested_hash(sha1_password) ⇒ Object



34
35
36
# File 'lib/password_rehasher.rb', line 34

def self.nested_hash(sha1_password)
	"nested hash: #{SCrypt::Password.create(sha1_password)}"
end

.password_valid?(plaintext_password, hashed_password, salt = nil) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/password_rehasher.rb', line 7

def self.password_valid?(plaintext_password, hashed_password, salt = nil)
	case hashed_password.length
	when 40
		return false unless salt
		hashed_password == Digest::SHA1.hexdigest("--#{salt}--#{plaintext_password}--")
	when 90
		password = SCrypt::Password.new(hashed_password)
		password == plaintext_password
	when 103
		return false unless salt
		scrypt_plus_sha1_hash =  hashed_password[13..-1]
		sha1_hashed_password = Digest::SHA1.hexdigest("--#{salt}--#{plaintext_password}--")
		password = SCrypt::Password.new(scrypt_plus_sha1_hash)
		password == sha1_hashed_password
	else
		false
	end
end

.rehash_needed?(hashed_password) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/password_rehasher.rb', line 26

def self.rehash_needed?(hashed_password)
	hashed_password.length != 90
end

.validate_and_rehash?(user, plaintext_password, hashed_password) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/password_rehasher.rb', line 38

def self.validate_and_rehash?(user, plaintext_password, hashed_password)
	if (plaintext_password && password_valid?(plaintext_password, hashed_password, user.salt))
		if (rehash_needed?(hashed_password))
			user.update_attribute("crypted_password", hash_password(plaintext_password))
		end
		return true
	else
		return false
	end
end