ReferenceSessionIdHashes.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Testing the untestable - Session ID generation
  4. */
  5. class ReferenceSessionIdHashes
  6. {
  7. // Session ID hashes
  8. protected static $sidHashes = null;
  9. /**
  10. * Generates session ID hashes for all algorithms & bit representations
  11. */
  12. public static function genAllHashes()
  13. {
  14. foreach (hash_algos() as $algo) {
  15. self::$sidHashes[$algo] = array();
  16. foreach (array(4, 5, 6) as $bpc) {
  17. self::$sidHashes[$algo][$bpc] = self::genSidHash($algo, $bpc);
  18. }
  19. }
  20. }
  21. /**
  22. * Generates a session ID for a given hash algorithm and bit representation
  23. *
  24. * @param string $function name of the hash function
  25. * @param int $bits_per_character representation type
  26. *
  27. * @return string the generated session ID
  28. */
  29. protected static function genSidHash($function, $bits_per_character)
  30. {
  31. if (session_id()) {
  32. session_destroy();
  33. }
  34. ini_set('session.hash_function', $function);
  35. ini_set('session.hash_bits_per_character', $bits_per_character);
  36. session_start();
  37. return session_id();
  38. }
  39. /**
  40. * Returns the reference hash array
  41. *
  42. * @return array session IDs generated for all available algorithms and bit
  43. * representations
  44. */
  45. public static function getHashes()
  46. {
  47. return self::$sidHashes;
  48. }
  49. }