2

I'm using md5(uniqid()) to generate a unique hash for my token hidden input on my forms (like Log in, Sign up, Settings, etc) for my File Sharing and Hosting service and for the user session, so I can compare those two after the form is submitted.

But I'm wondering if md5(uniqid()) is good enough after I've read that md5 has lots of security flaws.

Are there better or more secure ways of generating tokens for my forms?

Output example

<input type="hidden" name="token" value="4c1dd84d3458964ee6d59c728dc70160">
5
  • What is the token used for? Session? Apr 17, 2014 at 6:52
  • Yes, it is used for a Session. Apr 17, 2014 at 6:52
  • What are you using this token for? What are the consequences if someone guesses one?
    – user149341
    Apr 17, 2014 at 7:07
  • When using CSRF what they could do is log a user in, change their account settings (name, email and password), upload files to their personal storage and retrieve files. Apr 17, 2014 at 7:21
  • @Jack It needs to be unpredictable.
    – Gumbo
    Apr 17, 2014 at 7:50

4 Answers 4

3

This token should just be an unpredictable code. The best you can do to get such an unpredictable code with a deterministic computer, is to generate a really random number.

When you use the MD5 function with your uniqid, it does not add any randomness/unpredictability to your token, you (mis)use it as an encoder. The same goal you get with using the bin2hex() function, that's what MD5 does by default after calculating the binary hash. That said, the MD5 function is not unsafe here but has no advantage neither.

The more important point is, that the function uniqid() is not unpredictable, it is based on the current timestamp. This is the unsafe part in your code. To get an unpredictable number you can use the function mcrypt_create_iv() which reads from the random source of the operating system.

I would recommend to let PHP create the session token for you, with the session_start() function. If you really have reasons not to use a normal PHP session, then use mcrypt_create_iv() together with an encoding function like bin2hex() or base64_encode().

EDIT:

From your comments i see that this token is not used to maintain the session, instead to mitigate csrf. In this case of course the session_start function won't help (the session id should not be used as csrf token), but creating an unpredictable token is still important. This is an example of how this can be done:

/**
 * Generates a random string of a given length, using the random source of
 * the operating system. The string contains only characters of this
 * alphabet: +/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
 * @param int $length Number of characters the string should have.
 * @return string A random base64 encoded string.
 */
protected static function generateRandomBase64String($length)
{
  if (!defined('MCRYPT_DEV_URANDOM')) throw new Exception('The MCRYPT_DEV_URANDOM source is required (PHP 5.3).');

  // Generate random bytes, using the operating system's random source.
  // Since PHP 5.3 this also uses the random source on a Windows server.
  // Unlike /dev/random, the /dev/urandom does not block the server, if
  // there is not enough entropy available.
  $binaryLength = (int)($length * 3 / 4 + 1);
  $randomBinaryString = mcrypt_create_iv($binaryLength, MCRYPT_DEV_URANDOM);
  $randomBase64String = base64_encode($randomBinaryString);
  return substr($randomBase64String, 0, $length);
}
3
  • Isn't it that session_id are not unpredictable and that several sites may use the same id, if let's say you have open browser tabs? Apr 17, 2014 at 7:32
  • @KidDiamond - The session id generated by PHP is partly a random code, otherwise this would be a security flaw and could be used to hijeack a session. This answer gives a bit more insight. The session id is normally stored in a cookie in the browser and each cookie belongs to a single domain, so even if you would have equal session ids, they would not interfere between several sites. Apr 17, 2014 at 8:00
  • @KidDiamond - I misread your first comment about using it for sessions, so i made an edit. Apr 17, 2014 at 8:16
1

There was a great answer to the first half of this on https://security.stackexchange.com/a/19710 , the second half god a good answer from the manpage, as posted by rmcfrazier: http://www.php.net/manual/en/function.uniqid.php

quote combinatorics: "There are devastating collision attacks on MD5. (...) In contrast, SHA1 appears to be much more secure. While there are some known attacks on SHA1, they are much less serious than the attacks on MD5. For this reason, SHA1 is a much better choice than MD5 in many settings." <- MD5

"Warning: This function does not create random nor unpredictable strings. This function must not be used for security purposes. Use a cryptographically secure random function/generator and cryptographically secure hash functions to create unpredictable secure IDs." <- uniqid

0

Per the man page, this should not be used for cryptographically secure tokens http://www.php.net/manual/en/function.uniqid.php

You should use openssl_random_pseudo_bytes for your tokens http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php

0

It really depends on how secure you need the token to be. Using the PHP session ID should suffice unless you need to verify the integrity of the key after submission.

Eg. PHP session id:

php > session_start();
php > echo session_id();
ritig5ecgp6ebmnq8p5imbdhl3

However, you can always generate more secure IDs using the hash() function eg:

https://www.php.net/manual/en/function.hash.php Don't use RipeMD

echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');
ec457d0a974c48d5685a7efa03d137dc8bbde7e3

Unless the token needs to be cryptographically secure, the session key should suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.