dongsi7067 2016-06-27 00:42
浏览 105
已采纳

我应该为多个设备创建多少个cookie?

I'm trying to create a cookie to keep my website's users logged. I let the person log in from multiple devices. Well can I create one identical cookie for all user's devices? Or should I store multiple different cookies for different devices (and why)?


Actually I don't see any problem with having one common cookie for an specific user on multiple devices. Also that cookie isn't changeable. That will be always fixed until the user changes his password. I create that cookie like this:

md5($password.$user_id.$username);

That cookie is based on the password because I want to log-out him when he changes his password, otherwise he will be logged into all devices that he already logged.

Am I doing that right correct?

  • 写回答

2条回答 默认 最新

  • dongwen7283 2016-06-27 13:12
    关注

    The cryptographic hash algorithm MD5 has already begun to be broken, mainly in regards to its collision resistance property, as well as preimage resistance slightly. See the Wikipedia article, in the security section.

    Therefore I would not use MD5 for anything slightly security related. A similar story with SHA-1 in that it has began to be broken, so I would use SHA-2 if you do indeed need a hashing algorithm.

    The problem with your approach is that you cannot revoke the token from individual devices easily. Also having password as input to your algorithm could make it vulnerable to a hash cracking attack, should the other values be known. Don't rely on your method being secret either, Kerckhoffs's principle states "A cryptosystem should be secure even if everything about the system, except the key, is public knowledge.".

    Also another problem with your approach is that your hashing algorithm needs plaintext access to the password, which could suggest you are storing passwords insecurely. In short ensure you are using bcrypt for password storage. Of course, you may be creating the cookie and storing a server-side version of it at a point where the user enters their password, however given that you want to invalidate cookies automatically upon password change makes me think not.

    The most secure way to manage devices with tokens is to generate a 128-bit random token per device, and store this hashed with SHA-2 in your database. Ensure a CSPRNG is used to generate the token.

    Therefore:

        Cookie value: 128-bit token
        Database value: SHA-2(128-bit token)
    

    Note that salts are not required for values of such bit strength. Then when a user changes their password, you simply delete all server-side tokens for the user. Additionally, you will be able to allow the user to revoke tokens for different devices individually without any password change required.

    The reason for hashing on the server-side is to mitigate any session hijacking should an attacker gain access to your sessions table.

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

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题