Using a time-based, rotating hash or string for security

前端 未结 4 1173
无人共我
无人共我 2021-01-13 08:06

In a CMS app I occasionally need to open an iframe of another domain. At the moment I am setting the URL for that iframe to something very obscure. Like http://domain.com/if

4条回答
  •  佛祖请我去吃肉
    2021-01-13 08:41

    You could add a key to your hash and send the timestamp with the query, e.g.:

    $key = "YOUR_SECRET_KEY";
    $time = time();
    $hash = hash_hmac('sha256', $time, $key);
    $url = "https://example.com/iframe?hash=$hash&time=$time";
    

    On the other side you should first check if the timestamp is in the limits (e.g. not older than five minutes) and than rehash with the key and the submitted timestamp. If you get the same hash the request is valid.

    Notes:

    • don't use MD5: the algorithm is completely broken and doesn't provide any security anymore (although it's supposed to still be ok when used with an HMAC…)
    • you should use hash_equals for comparing hashes to prevent timing attacks
    • we use an HMAC to guarantee data integrity and authentication. See https://crypto.stackexchange.com/questions/1070/why-is-hkx-not-a-secure-mac-construction for why we mustn't just concatenate time & key

提交回复
热议问题