简体   繁体   English

在 Javascript 中重建/验证 Ruby on Rails Bcrypt 密码哈希

[英]Rebuild/Verify Ruby on Rails Bcrypt password hash in Javascript

I have to rebuild a project from Ruby on Rails to Node.js.我必须从 Ruby on Rails 重建一个项目到 Node.js。 Bcrypt was used to hash passwords in the Ruby project and I'm trying to rebuild the same hash so I can copy the hashed password and users can login with the same credentials on the node version. Bcrypt 用于在 Ruby 项目中散列密码,我正在尝试重建相同的散列,以便我可以复制散列密码,并且用户可以在节点版本上使用相同的凭据登录。

This hash $2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2 is used for the password Test1234 .此哈希$2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2用于密码Test1234 I've checked the Ruby on Rails code and I saw the following function to hash a password我检查了 Ruby on Rails 代码,看到了以下函数来散列密码

General info基本信息

COST = 11
SALT = 1234567890

Create hash创建哈希

def password_hash(password)
  pwd = "#{password}#{SALT}"
  ::BCrypt::Password.create(pwd, cost: COST)
end

Does passwords match?密码是否匹配?

def password_match?(password = nil)
  password ||= @params[:password]
  encrypted_password = get_encrypted_password
  return false if !encrypted_password || encrypted_password.size < 8

  pwd = "#{password}#{SALT}"
  BCrypt::Password.new(encrypted_password) == pwd
end

def get_encrypted_password
  return unless @account

  @account.encrypted_password
end

As far as I know something about Ruby this means that in the password_match function, pwd would be Test12341234567890 and BCrypt::Password.new($2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2) checks if Test12341234567890 (pwd) matches the hash.据我所知,这意味着在password_match函数中,pwd 将是Test12341234567890BCrypt::Password.new($2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2)检查Test12341234567890 (pwd)是否匹配哈希。

When I use an online Bcrypt verifier like https://bcrypt.online/ and enter the hash together with the pwd value I don't get a match.当我使用像https://bcrypt.online/这样的在线 Bcrypt 验证器并输入哈希值和 pwd 值时,我没有得到匹配。

I also tried to use the bcrypt.compare method in the Javascript package but this didn't work either.我还尝试使用 Javascript 包中的bcrypt.compare方法,但这也不起作用。

What am I missing?我错过了什么?

So we did an oopsie and solved this issue in the comments.所以我们做了一个oopsie并在评论中解决了这个问题。 Here is a recap of it all as an answer.这是一个作为答案的回顾。

Me:我:

I tried your password_hash function with "Test1234" and the salt you provided.我用“Test1234”和你提供的盐尝试了你的密码哈希函数。 Got $2a$11$bhdYASCaEPv/0HXv3OFtIupv8CgHFoEWkMonShKnNN1fkmRIg.07S as a result, ran it through the online verifier you linked and got "The supplied hash matches with supplied plain text".结果得到了 $2a$11$bhdYASCaEPv/0HXv3OFtIupv8CgHFoEWkMonShKnNN1fkmRIg.07S,通过您链接的在线验证程序运行它并得到“提供的哈希与提供的纯文本匹配”。

BCrypt::Password.new(password_hash("Test1234")) == "Test12341234567890" Also works out fine and returns true. BCrypt::Password.new(password_hash("Test1234")) == "Test12341234567890" 也可以正常工作并返回true。

Also tried Node.js with bcryptjs and did bcrypt.compareSync('Test12341234567890', "$2a$11$bhdYASCaEPv/0HXv3OFtIupv8CgHFoEWkMonShKnNN1fkmRIg.07S").还尝试了 Node.js 和 bcryptjs 并做了 bcrypt.compareSync('Test12341234567890', "$2a$11$bhdYASCaEPv/0HXv3OFtIupv8CgHFoEWkMonShKnNN1fkmRIg.07S")。 Also works out fine and returns true.也可以正常工作并返回true。

Thore:托雷:

Yes that's a new hash you created with the same methods.是的,这是您使用相同方法创建的新哈希。 But for some reason the already created hash $2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2 for password Test1234 that is currently stored in the database doesn't pass the online verification test and the compareSync test in node.js但由于某种原因,当前存储在数据库中的密码 Test1234 的已创建哈希 $2a$11$j2IA8cPRFFC4YOXTl5kb9eF02fwNdLyFAPOvflQ3h/QdX8mE1SNK2 未通过在线验证测试和 node.js 中的 compareSync 测试

Me:我:

That should give you the idea that the problem is not with the Ruby's hash creation function, nor is it with the Node's hash testing function, but somewhere else (which is technically outside the scope of this question).这应该让您知道问题不在于 Ruby 的哈希创建函数,也不在于节点的哈希测试函数,而是其他地方(技术上超出了这个问题的范围)。 Maybe your database doesn't store the full hash for some reason, or stores it in a wrong way.也许您的数据库由于某种原因没有存储完整的哈希,或者以错误的方式存储它。 Maybe something somewhere strips or escapes some characters in the hash, leaving you with incomplete one.也许某处的某些东西会剥离或转义散列中的某些字符,从而使您得到不完整的字符。 Maybe some of the aforementioned happens to the salt.也许前面提到的一些事情发生在盐上。 Maybe the hash in the database was generated by a different or older hash function.也许数据库中的散列是由不同的或更旧的散列函数生成的。 Maybe the salt has changed.也许盐变了。

Thore:托雷:

Thanks for the comment!感谢您的评论! With your comment I found out that they used a different salt than the one inside the .env file.根据您的评论,我发现他们使用的盐与 .env 文件中的盐不同。 It was stored in the config variables of Heroku.它存储在 Heroku 的配置变量中。 Everything is working now!现在一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM