Created
March 23, 2015 16:14
-
-
Save ltw/8b6602c1d93029e1ed99 to your computer and use it in GitHub Desktop.
A very, very simplified explanation of how BCrypt equality works.
This file contains hidden or 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
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