Skip to content

Commit a65b1b3

Browse files
committed
Use argon
1 parent 708a736 commit a65b1b3

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

app/models/user.rb

+17-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ def guest?
126126
end
127127

128128
def has_password?(submitted_password)
129-
password_hash == encrypt(submitted_password)
129+
if !salt
130+
return Argon2::Password.verify_password(submitted_password, argon_hash)
131+
end
132+
result = Argon2::Password.verify_password(old_encrypt(submitted_password), argon_hash)
133+
if result && salt
134+
self.argon_hash = generate_argon(submitted_password)
135+
self.salt = nil
136+
save
137+
end
138+
result
130139
end
131140

132141
def self.authenticate(login, submitted_password)
@@ -307,18 +316,16 @@ def submissions_exercises_and_points_for_user
307316
end
308317

309318
def encrypt_password
310-
self.salt = make_salt if new_record?
311-
self.password_hash = encrypt(password) if password.present?
319+
if password.present?
320+
self.argon_hash = generate_argon(password)
321+
self.salt = nil
322+
end
312323
end
313324

314-
def encrypt(string)
325+
def old_encrypt(string)
315326
secure_hash("#{salt}--#{string}")
316327
end
317328

318-
def make_salt
319-
secure_hash("#{Time.now.utc}--#{password}")
320-
end
321-
322329
def secure_hash(string)
323330
Digest::SHA2.hexdigest(string)
324331
end
@@ -331,7 +338,7 @@ def reject_common_login_mistakes
331338
errors.add(:email, 'is incorrect. You probably meant [email protected]. Keep in mind that your email address does not contain your University of Helsinki username.') if email.end_with?('@helsinki.fi') && !/.*\..*@helsinki.fi/.match?(email)
332339
end
333340

334-
def _generate_argon
335-
update(argon_hash: Argon2::Password.new(t_cost: 4, m_cost: 15).create(password_hash))
341+
def generate_argon(input)
342+
Argon2::Password.new(t_cost: 4, m_cost: 15).create(input)
336343
end
337344
end

0 commit comments

Comments
 (0)