Created
April 28, 2019 17:27
-
-
Save piharpi/993870ce596b9fe7c8cbe20974e8117f to your computer and use it in GitHub Desktop.
just little sample authentication with bcrypt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bcrypt' | |
# if u not ready with bcrypt u can install by typing 'gem install bcrypt' | |
accounts = [ | |
{ username: 'choirul', password: 'passwd' }, | |
{ username: 'harpi', password: 'strongpass' }, | |
{ username: 'mahendra', password: 'isverysecret' } | |
] | |
def create_hash(string) | |
BCrypt::Password.create(string) | |
end | |
def verify_hash(string) | |
BCrypt::Password.new(string) | |
end | |
def create_password(users) | |
users.each { |user| user[:password] = create_hash(user[:password]) } | |
end | |
def authentication(username, password, users) | |
users.each do |u| | |
if u[:username] == username && verify_hash(u[:password]) == password | |
return users | |
end | |
end | |
'Your username and password is not right' | |
end | |
create_password(accounts) | |
puts authentication('harpi', 'strongspass', accounts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment