Skip to content

Instantly share code, notes, and snippets.

@piharpi
Created April 28, 2019 17:27
Show Gist options
  • Save piharpi/993870ce596b9fe7c8cbe20974e8117f to your computer and use it in GitHub Desktop.
Save piharpi/993870ce596b9fe7c8cbe20974e8117f to your computer and use it in GitHub Desktop.
just little sample authentication with bcrypt.
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