1
0

serversalt.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.19
  11. */
  12. /**
  13. * serversalt
  14. *
  15. * This is a random string which is unique to each ZeroBin installation.
  16. * It is automatically created if not present.
  17. *
  18. * Salt is used:
  19. * - to generate unique VizHash in discussions (which are not reproductible across ZeroBin servers)
  20. * - to generate unique deletion token (which are not re-usable across ZeroBin servers)
  21. */
  22. class serversalt extends persistence
  23. {
  24. /**
  25. * generated salt
  26. *
  27. * @access private
  28. * @static
  29. * @var string
  30. */
  31. private static $_salt = '';
  32. /**
  33. * generate a large random hexadecimal salt
  34. *
  35. * @access public
  36. * @static
  37. * @return string
  38. */
  39. public static function generate()
  40. {
  41. $randomSalt = '';
  42. if (function_exists('mcrypt_create_iv'))
  43. {
  44. $randomSalt = bin2hex(mcrypt_create_iv(256, MCRYPT_DEV_URANDOM));
  45. }
  46. else // fallback to mt_rand()
  47. {
  48. for($i = 0; $i < 16; ++$i) {
  49. $randomSalt .= base_convert(mt_rand(), 10, 16);
  50. }
  51. }
  52. self::$_salt = $randomSalt;
  53. return self::$_salt;
  54. }
  55. /**
  56. * get server salt
  57. *
  58. * @access public
  59. * @static
  60. * @return string
  61. */
  62. public static function get()
  63. {
  64. if (strlen(self::$_salt)) return self::$_salt;
  65. $file = 'salt.php';
  66. if (!self::_exists($file)) {
  67. self::_store(
  68. $file,
  69. '<?php /* |'. self::generate() . '| */ ?>'
  70. );
  71. }
  72. $items = explode('|', file_get_contents(self::getPath($file)));
  73. self::$_salt = $items[1];
  74. return $items[1];
  75. }
  76. }