Bcrypt generates different hashes for the same input?

后端 未结 2 2112
孤城傲影
孤城傲影 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 07:54

    Jan is correct - bcrypt by design doesn't generate the same hash for each input string. But there's a way to check that a hashed password is valid, and it's incorporated into the associated password encoder. So add a dependency injection for the passwordEncoder bean in your controller (def passwordEncoder) and change the lookup to

    def handleLogin = {
    
       if (springSecurityService.isLoggedIn()) {
          render(view: "../homepage")
          return
       }
    
       def user = Registration.findByEmail(params.email)
       if (user && !passwordEncoder.isPasswordValid(user.password, params.password, null)) {
          user = null
       }
    
       if (!user) {
          flash.message = "User not found for email: ${params.email}"
          render(view: "../index")
          return
       }
    
       session.user = user
       render(view: "../homepage")
    }
    

    Note that you don't encode the password for the isPasswordValid call - pass in the cleartext submitted password.

    Also - completely unrelated - it's a bad idea to store the user in the session. The auth principal is readily available and stores the user id to make it easy to reload the user as needed (e.g. User.get(springSecurityService.principal.id). Storing disconnected potentially large Hibernate objects works great in dev mode when you're the only user of your server, but can be a significant waste of memory and forces you to work around the objects being disconnected (e.g. having to use merge, etc.).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题