dongyu2047 2013-06-21 08:52
浏览 36
已采纳

如何最好地使用加密方法来确认代码

How best to use the encryption method for confirm code on email notification: md5, sha1 or sha256

I make email notification for confirm some actions by email;

I generated confirm code with salt and some parameters.

How best to use the encryption method for confirm code?

  • 写回答

2条回答 默认 最新

  • douganggu4392 2013-06-22 05:38
    关注

    You have two main options here:

    1. generated some random data, store it in your database associated with that profile, and put only this data on the link;
    2. generate some kind of secure hash based on, say, the user id, and then pass both the user id and that hash on the link

    The first option is secure (supposing you are using either a real random number generator or a very, very good pseudo-random number generator), but you have to store data on a database.

    I usually prefer the second option, since there's no need to store anything on a database, and no need to query the database to check if the link is valid.

    Choose a secret key that only your server will know, then define exactly what parameters you want to validate in the URL (for instance, only the user id might be enough; however if you want the link to expire, you could add a timestamp to the url and validate both the user id and the timestamp with your hash). Mix your secret key with the parameters, generate a hash based on this, and create a link specifying both your parameters and the hash. When the user clicks the link, you take the parameters from the url, combine them again with the secret key, hash the result and compare with the hash that came with the url.

    A secure way to do this is to use HMAC, which is Hash-based Message Authentication Code. See: http://php.net/manual/en/function.hash-hmac.php.

    Note that this mechanism exposes the data you are validating in the url. If the data is secret (ie, suppose you want to send the link to the user containing both the username and his password, without saving anything at all on the database before he clicks the link), you must use Authenticated Encryption, which is a mix of an encryption algorithm (which guarantees that no one can read the data) and an authentication algorithm (which guarantees that no one can temper with the encrypted data to produce something meaningful -- only your server is able to generate such code).

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

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题