dongpu1315 2017-08-13 10:15
浏览 276
已采纳

加密和比较哈希值

Checked a lot of tutorials and guides about Encrypting and Hashing on StackOver and I do now understand the difference between both of them. Encryption when we need decryption. Hash when you don't (e.g: passwords).

But my question today is, for generating random unique tokens. A lot of people recommend using base64_encode(rand_bytes(32)); because it generates a secure cryptographical id with random bytes/letters so it takes a long time to be cracked by a brute force ( to be predicted ).

But when you, for example, hash_hmac or crypt that id will it make it weaker? If so it won't matter if you use mt_rand or random_bytes as it will generate other letters/id. So what do you guys recommend?

Also, Is crypt better than hash_hmac for hashing?

The last question, Since blowfish has a really strong hashing algorithm, do I need to store my, for example, PHPSESSID or CSRF token with it or just a normal random_bytes?

Please guys, provide me with detailed information about these questions or documentation. Sorry for my lack of information and inexperience, I am new to PHP coding and thanks!

Edit: I have seen some guides on SO, not covering all my questions, so I need a 2017 detailed information not a 10 years ago post where md5 was the best etc..!

  • 写回答

3条回答 默认 最新

  • dpizd08264 2017-08-13 11:04
    关注

    When you just want tokens that are unpredictable, use random_bytes(32). As long as you keep the tokens secret, there is no reason to alter the result of this function.

    But you ask a lot of specific questions, so I will try to answer them as best I can:

    When you hash or crypt() that id will it make it weaker?

    This depends on the HMAC or crypt routine. When you generate 32 bytes of random data, and then use a function to reduce the size to 16 bytes, the security will be halved. However, if you use a cryptographically secure function that has at least 32 bytes of output, the security is not reduced. Now I say this, because crypt() is not cryptographically secure function. When hashing passwords, use password_hash() instead.

    Is there a difference between using mt_rand and using random_bytes?

    Yes, it will matter. mt_rand() uses a Mersenne Twister random number generator. These are not secure. This means that an attacker could use token values to guess other tokens. In the case of mt_rand(), this is often very easy. If you need the tokens to be secure (authentication tokens, session cookies, CSRF tokens, id's that may not be enumerated, etc.), use random_bytes. Also, if you are not sure, use random_bytes.

    Also, Is crypt better than hash_hmac for hashing?

    No. Crypt is deprecated and you should not use it. Use HMAC when you need message authentication codes (for example for password reset URLs). For password hashing, use password_hash.

    Do I need to store my, for example, PHPSESSID or CSRF token with Blowfish or just a normal random_bytes?

    Just use random_bytes. The random data is already random, it does not need to be randomized again. At least, as long as you keep these tokens secret and revoke them immediately when they have been leaked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘