diff --git a/app/models/user.rb b/app/models/user.rb index f9d7ee0..8e2b65f 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,7 +25,6 @@ class User < ActiveRecord::Base def self.authenticate(email, password) auth = nil user = find_by_email(email) - # I heard something about hashing, dunno, why bother really. Nobody will get access to my stuff! if user if user.password == Digest::MD5.hexdigest(password) auth = user diff --git a/app/views/layouts/tutorial/crypto/_password_hashing.html.erb b/app/views/layouts/tutorial/crypto/_password_hashing.html.erb index acd4113..b6e220e 100755 --- a/app/views/layouts/tutorial/crypto/_password_hashing.html.erb +++ b/app/views/layouts/tutorial/crypto/_password_hashing.html.erb @@ -1,7 +1,7 @@
@@ -30,7 +38,35 @@+ Within app/models/user.rb: +
++ before_save :encrypt_password + + def self.authenticate(email, password) + auth = nil + user = find_by_email(email) + if user + if user.password == Digest::MD5.hexdigest(password) + auth = user + else + raise "Incorrect Password!" + end + else + raise "#{email} doesn't exist!" + end + return auth + end + + def encrypt_password + if self.password.present? + self.password = Digest::MD5.hexdigest(password) + end + end + ++
Password Storage - ATTACK
++ Using the passwords stored within db/seeds.rb file, create a wordlist and leverage a password cracking tool such as John The Ripper to crack those passwords. +
+Password Storage - SOLUTION
++ A simple solution here would be to enforce a per-user salt in creating a BCrypt hash. You would need to alter the db schema to add a password_salt and password_hash columns to the table. +
++ def self.authenticate(email, password) + user = find_by_email(email) + if user and user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) + user + else + "Invalid Credentials Supplied" + end + end + + def encrypt_password + if self.password.present? + self.password_salt = BCrypt::Engine.generate_salt + self.password_hash = BCrypt::Engine.hash_secret(self.password, self.password_salt) + end + end +