Skip to content

BCrypt::Engine.hash_secret returning wrong values #208

Closed

Description

@daya

see proof below and let me know if I am missing something

encrypted_password = "$2a$11$s0mPGls88ceF8TfGgdzCYennR/KF.bElu6XDgXZWmCaHbL3oMGcve"

bcrypt = ::BCrypt::Password.new(encrypted_password)

pwd = "5}rmfd+@\"6?5Yor{qgS)UGtn,T&V=h3)KYT.joWv6*Da\"iP\"t)ChCj!A[m}/)rW_:Q?(f~2fn(<(f)b_3+_kE$fV66YUz=#M7fjC<=R6"
pwd1 = pwd.sub("R6", "R9") #substitutes last 2 chars of pwd from R6 to R9

BCrypt::Engine.hash_secret(pwd, bcrypt.salt) == BCrypt::Engine.hash_secret(pwd1, bcrypt.salt) #returns true when it should return false

pwd2 = pwd.sub("5}", "6}") #substitutes first 2 chars of pwd from 5} to 6}

BCrypt::Engine.hash_secret(pwd, bcrypt.salt) == BCrypt::Engine.hash_secret(pwd2, bcrypt.salt) #returns false as expected

Seems like a bug to me.

Activity

  1. tjschuck commented on Aug 13, 2019

    @tjschuck
    Collaborator

    Your password is longer than bcrypt supports. (Note: bcrypt itself, not bcrypt-ruby.)

    Anything after 72 bytes is truncated, so any two passwords for which the first 72 bytes match will pass, even if they're different after that. Since you're changing the last 2 characters of a 104 character string, they fall outside of this window.

    If you want to reject passwords longer than 72 bytes to avoid this issue, you should add it to your app code. For example, this is what Rails' has_secure_password does. (Docs here.)

    If you Google something like "bcrypt 72", you'll see that this is an issue common to most bcrypt implementations — it's part of the design of it. See also: bcrypt#User Input

  2. daya commented on Aug 13, 2019

    @daya
    Author

    @tjschuck thanks for explanation. The logical question now is, is there a alternative encryptor to BCrypt that is at least as good if not better than BCrypte and is it worth using any one of them

  3. tjschuck commented on Aug 13, 2019

    @tjschuck
    Collaborator

    bcrypt is still perfectly good. 72 bytes of entropy for your password is — dare I say — more than enough.

    I saw you linked this to an issue in a Devise extension — note that Devise also validates that passwords should be < 72, like Rails' has_secure_password.

    If you want to consider alternatives, I'll refer you to the "Password handling" section of Cryptographic Right Answers:

    In order of preference, use scrypt, argon2, bcrypt, and then if nothing else is available PBKDF2. [...] But, seriously: you can throw a dart at a wall to pick one of these.

  4. daya commented on Aug 14, 2019

    @daya
    Author

    Thanks @tjschuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions