1
0

ServerSalt.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.3.5
  11. */
  12. namespace PrivateBin\Persistence;
  13. use PrivateBin\Data\AbstractData;
  14. /**
  15. * ServerSalt
  16. *
  17. * This is a random string which is unique to each PrivateBin installation.
  18. * It is automatically created if not present.
  19. *
  20. * Salt is used:
  21. * - to generate unique VizHash in discussions (which are not reproductible across PrivateBin servers)
  22. * - to generate unique deletion token (which are not re-usable across PrivateBin servers)
  23. */
  24. class ServerSalt extends AbstractPersistence
  25. {
  26. /**
  27. * generated salt
  28. *
  29. * @access private
  30. * @static
  31. * @var string
  32. */
  33. private static $_salt = '';
  34. /**
  35. * generate a large random hexadecimal salt
  36. *
  37. * @access public
  38. * @static
  39. * @return string
  40. */
  41. public static function generate()
  42. {
  43. return bin2hex(random_bytes(256));
  44. }
  45. /**
  46. * get server salt
  47. *
  48. * @access public
  49. * @static
  50. * @return string
  51. */
  52. public static function get()
  53. {
  54. if (strlen(self::$_salt)) {
  55. return self::$_salt;
  56. }
  57. $salt = self::$_store->getValue('salt');
  58. if ($salt) {
  59. self::$_salt = $salt;
  60. } else {
  61. self::$_salt = self::generate();
  62. self::$_store->setValue(self::$_salt, 'salt');
  63. }
  64. return self::$_salt;
  65. }
  66. /**
  67. * set the path
  68. *
  69. * @access public
  70. * @static
  71. * @param AbstractData $store
  72. */
  73. public static function setStore(AbstractData $store)
  74. {
  75. self::$_salt = '';
  76. parent::setStore($store);
  77. }
  78. }