简体   繁体   English

比较哈希

[英]Comparing hashes

I m trying to compare a hash of password created from nodeJs crypto module's PBKDF2sync function with the one store in my database using "===".我正在尝试将使用“===”从 nodeJs 加密模块的 PBKDF2sync function 创建的密码 hash 与我的数据库中的一个商店进行比较。 But I am getting false.但我越来越假了。 I have checked using console.log that salt for both are equal but still showing false.我已经使用 console.log 检查了两者的盐是相等的,但仍然显示为假。 I dont understand what's happening?我不明白发生了什么?

When I try to console.log the hash from the PBKSF2sync function I get this thing on my terminal当我尝试从 PBKSF2sync function 控制台登录 hash 时,我在终端上得到了这个东西

<SlowBuffer bf 36 32 7e b8 8e 6d a7 8f 8a 9d cb 7e 99 11 47 a7 17 80 17 df ef 8b b4 36 f1 18 1d c5 de ab 1e b3 69 f1 2f 1a 20 09 1f e6 2c c6 2e e9 ca 1f 5a 58 19 46 ...>

Is this the reason why I am getting false.这就是我变得虚假的原因吗? Please help me.请帮我。

The === also makes sure both datatypes match precisely, so if say one is a buffer and the other a (typed) array then these may have similar content but will still fail to compare. ===还确保两种数据类型都精确匹配,因此如果说一个是缓冲区而另一个是(类型化)数组,那么它们可能具有相似的内容,但仍然无法进行比较。

You could either use == to compare which will try to convert both to a matching type but is unsafe thus not advised in hash operations or you just convert both to the same type before comparing them. 您可以使用==来比较哪个会尝试将两者都转换为匹配类型,但是不安全,因此在哈希操作中不建议或者您只是在比较它们之前将它们转换为相同的类型。

The problem was that I was storing the password in database as string and the PBKDF2sync function was returning me an buffer . 问题是我将密码存储在数据库中作为字符串 ,而PBKDF2sync函数正在返回一个缓冲区 So === wasn't working. 所以===没有用。 Thanks to some readings I did I figured it out and after returning the PBKDF2 has I just used .toString() on it and it worked. 感谢一些阅读,我做了我想出来,并在返回PBKDF2后,我刚刚使用.toString()并且它工作。

Hope this solves any problem that anyone else might be having on the same subject. 希望这可以解决任何其他人可能在同一主题上遇到的任何问题。

Please, refrain from using === for hash comparison, as it opens window for timing attacks, using crypto.timingSafeEqual is a better alternative请不要使用 === 进行 hash 比较,因为它会打开 window 进行定时攻击,使用crypto.timingSafeEqual是更好的选择

Refer to this thread for more information on why you should use it有关为什么要使用它的更多信息,请参阅此线程

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

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