Page 1 of 1

Secure cookies via fingerprint (hash sum)

Posted: Wed Jun 27, 2007 9:57 am
by VladSun
Hello, everybody!
I have to develope a www application which should not require user login, but still has to have some "per user" features.
I know that using cookies is unsecure, but I want to simplify the parsing and validation of the user side data. I wrote a set of functions for manipulating cookie infermation:

Code: Select all

 
$seed = "kjHGjhGjhGChgfKJ5jhg687gFjhg67g";
 
/* ----------- SECURE COOKIES ----------------- */
 
function getSecCInfo($name)
{
    global $seed;
 
    if (!empty($_COOKIE[$name]) && !empty($_COOKIE[$name."_HASH"]))
    {
        $pref = unserialize(base64_decode($_COOKIE[$name]));
        if (base64_decode($_COOKIE[$name."_HASH"]) != md5($_COOKIE[$name].$seed) )
        {
            return null;
        }
        return $pref;
    }
    return null;
}
 
function setSecCInfo($pref, $name, $months = 1)
{
    global $seed;
 
    $ser = base64_encode(serialize($pref));
    setcookie($name, $ser, time() + 3600*24*30*$months);
    setcookie($name."_HASH", base64_encode(md5($ser.$seed)), time() + 3600*24*30*$months);
}
 
function delSecCInfo($name)
{
    setcookie($name, '', time() - 3600);
    setcookie($name."_HASH", '', time() - 3600);
}
 
 
You can see the idea - along with the data cookie, I write a second cookie which is the hash sum (or fingerprint) of the data concatenated with a secret string ($seed). When I read the cookie information I compare the results and decide whether the data has been modified or not.

My questions are:
1. Is it secure enough?
2. Do I have to use more powerfull algorithms than md5?
3. Any other ideas for this issue?

Regards, Vladimir Mirchev.

Posted: Wed Jun 27, 2007 10:37 am
by Weirdan
hmac-* is what you're looking for. HMAC-MD5 or, even better, HMAC-SHA1.

http://www.faqs.org/rfcs/rfc2104.html
http://www-cse.ucsd.edu/~mihir/papers/kmd5.pdf

Posted: Wed Jun 27, 2007 1:14 pm
by VladSun
Thank you!

Posted: Fri Jun 29, 2007 7:58 am
by Mordred
Since the string output of md5() is a safe cookie value, you can strip a pair of base64_(en|de)code() calls.

Posted: Sat Jun 30, 2007 9:55 am
by VladSun
Mordred wrote:Since the string output of md5() is a safe cookie value, you can strip a pair of base64_(en|de)code() calls.
I thought about this before, but I was not sure if it is safe ... Now you've made this clear :)
Thank you!