Bcrypt generates different hashes for the same input?

后端 未结 2 2126
孤城傲影
孤城傲影 2020-12-09 07:42

I just added a registration functionality to my new grails project. For testing it, I registered by giving an email and a password. I am using bcrypt algorithm for hashing t

2条回答
  •  旧巷少年郎
    2020-12-09 08:16

    A BCrypt hash includes salt and as a result this algorithm returns different hashes for the same input. Allow me to demonstrate it in Ruby.

    > require 'bcrypt'
    > p = BCrypt::Password.create "foobar"
    => "$2a$10$DopJPvHidYqWVKq.Sdcy5eTF82MvG1btPO.81NUtb/4XjiZa7ctQS"
    > r = BCrypt::Password.create "foobar"
    => "$2a$10$FTHN0Dechb/IiQuyeEwxaOCSdBss1KcC5fBKDKsj85adOYTLOPQf6"
    > p == "foobar"
    => true
    > r == "foobar"
    => true
    

    Consequently, BCrypt cannot be used for finding users in the way presented in your example. An alternative unambiguous field should be used instead, e.g. user's name or e-mail address.

提交回复
热议问题