简体   繁体   English

BCrypt:如何从我正在散列的字符串中生成盐?

[英]BCrypt: how can I generate a salt from the string I am hashing?

Short question: I do NOT want to use bcrypt random generated salts. 简短的问题:我不想使用bcrypt随机生成的盐。 Let's say the password I am hashing is "abcd1234". 假设我正在哈希的密码是“abcd1234”。 In the good old days, the salt would be "ab" or "abcd" or "abcd12", in other words, the salt would be the first N characters of the password where N is the required minimum length of a password. 在过去的好日子里,盐将是“ab”或“abcd”或“abcd12”,换句话说,盐将是密码的前N个字符,其中N是密码所需的最小长度。 So how do I generate a valid bcrypt salt from the password itself? 那么如何从密码本身生成有效的bcrypt盐呢?

Long question: I am working with a messaging system, in other words, a message will be sent out internally for authentication and I cannot include the plain-text password in this message for obvious reasons. 长问题:我正在使用消息传递系统,换句话说,消息将在内部发送以进行身份​​验证,并且由于显而易见的原因,我不能在此消息中包含纯文本密码。 So the flow should be something like that: 所以流程应该是这样的:

  • Machine A gets the password in clear-text 机器A以明文形式获取密码
  • Machine A hashes the password with bcrypt 机器A使用bcrypt对密码进行哈希处理
  • The hashed (ie safe) password is sent in my internal message 散列(即安全)密码在我的内部消息中发送
  • Machine B, which is the authentication machine with the database of hashed passwords, get the message and now it has to compare the hashed password it received with the hashed password in the database for authentication. 机器B是具有散列密码数据库的认证机器,它获取消息,现在它必须将它收到的散列密码与数据库中的散列密码进行比较以进行认证。

But how can I do that if BCrypt does NOT allow me to use a salt derived from my plain-text password? 但是,如果BCrypt不允许我使用从我的纯文本密码派生的盐,我怎么能这样做呢? Machine A knows nothing about any salt. 机器A对任何盐都一无所知。 It does not have any access to the database. 它没有对数据库的任何访问权限。 Machine B is the one who will know that. 机器B是知道这一点的人。 So there must be a way to derive my bcrypt salt from "abcd1234" or bcrypt should have a method: 所以必须有一种方法从“abcd1234”派生我的bcrypt盐,或者bcrypt应该有一个方法:

check(String hashedPasswordWithSaltA, String hashedPasswordWithSaltB);

Putting it down in straightforward terms: Machine A gets the password and Machine B is the one that has the authentication database. 用简单的术语来说明:机器A获取密码,机器B是具有认证数据库的机器。 I don't want to have to pass the password in clear text from A to B, but it looks like bcrypt forces me to do that. 我不想将明文密码从A传递给B,但看起来像bcrypt迫使我这样做。 :( :(

Just transmit the salt with the hashed password. 只需使用哈希密码传输salt即可。 The point of salting a password is so that in a database, two passwords that are the same in plaintext have different hashes. 对密码进行腌制的要点是,在数据库中,明文中相同的两个密码具有不同的哈希值。 If you generate this salt from the password itself, this logic falls over. 如果从密码本身生成此salt,则此逻辑会失效。 Additionally, as for decrypting purposes you have to store the salt in plaintext, you are essentially revealing a chunk of the users password. 此外,至于解密目的,您必须以纯文本形式存储salt,您实际上是泄露了一大块用户密码。

TLDR: Let Bcrypt generate salts for you. TLDR:让Bcrypt为您生成盐。 Then transmit the salts with the hashed password. 然后用散列密码传输盐。 Salts are not meant to be secret. 盐不是秘密的。

You have actually two options to solve this problem. 实际上有两种方法可以解决这个问题。

1) Every website has this problem, because the password must be sent to the server. 1)每个网站都有这个问题,因为密码必须发送到服务器。 This is usually solved by using an encrypted connection (HTTPS/SSL). 这通常通过使用加密连接(HTTPS / SSL)来解决。 The password will only be encrypted (two-way), transferred to machine2 (server), decrypted and afterwards hashed. 密码只会被加密(双向),传输到machine2(服务器),解密后再进行哈希处理。

2) You can hash the password on machine1 with BCrypt with a random salt, send it to machine2 and store the hash. 2)您可以使用随机盐在BC机器上使用BCrypt散列密码,将其发送到machine2并存储散列。 To validate the password, machine1 would first have to ask machine2 for the used salt, then it hashes the password with this salt, and afterwards sends the hash to machine2. 要验证密码,machine1首先必须向machine2询问使用过的salt,然后使用此salt哈希密码,然后将哈希值发送到machine2。 Machine2 can verify the password because the same salt was used. Machine2可以验证密码,因为使用了相同的盐。

I had a quick look at a jBCrypt example , it seems that you can generate the salt yourself and pass it as parameter. 我快速浏览了一个jBCrypt示例 ,似乎您可以自己生成盐并将其作为参数传递。 So you could actually derrive the salt from the password, but this makes the salt useless, it becomes just a more complex hash function. 所以你实际上可以从密码中驱除盐,但这会使盐变得无用,它变成了一个更复杂的哈希函数。

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

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