Skip to content

Instantly share code, notes, and snippets.

@ltw
Created March 23, 2015 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltw/8b6602c1d93029e1ed99 to your computer and use it in GitHub Desktop.
Save ltw/8b6602c1d93029e1ed99 to your computer and use it in GitHub Desktop.
A very, very simplified explanation of how BCrypt equality works.
module BCrypt
class Password
def initialize(password_hash)
@password_hash = password_hash
end
def self.create(password)
@password_hash = encrypt(password)
end
# overrides the equality operator
# This allows BCrypt to accept plaintext strings, and to match their equality against encrypted strings.
def ==(value)
@password_hash == BCrypt::Password.encrypt(value)
end
def to_s
@password_hash
end
private
def self.encrypt(value)
# this is a mystery function that takes in a plaintext string and returns an encrypted string.
#
# e.g. BCrypt::Password.encrypt("password") #=> "$2a$10$A1sbW0NafppF58qfQqzp0eTro0h9TIQ8MSxMZR3o14ACYwtAQbVlC"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment