ServerSalt.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Exception;
  14. use PrivateBin\Data\AbstractData;
  15. /**
  16. * ServerSalt
  17. *
  18. * This is a random string which is unique to each PrivateBin installation.
  19. * It is automatically created if not present.
  20. *
  21. * Salt is used:
  22. * - to generate unique VizHash in discussions (which are not reproductible across PrivateBin servers)
  23. * - to generate unique deletion token (which are not re-usable across PrivateBin servers)
  24. */
  25. class ServerSalt extends AbstractPersistence
  26. {
  27. /**
  28. * file where salt is saved to
  29. *
  30. * @access private
  31. * @static
  32. * @var string
  33. */
  34. private static $_file = 'salt.php';
  35. /**
  36. * generated salt
  37. *
  38. * @access private
  39. * @static
  40. * @var string
  41. */
  42. private static $_salt = '';
  43. /**
  44. * generate a large random hexadecimal salt
  45. *
  46. * @access public
  47. * @static
  48. * @return string
  49. */
  50. public static function generate()
  51. {
  52. $randomSalt = bin2hex(random_bytes(256));
  53. return $randomSalt;
  54. }
  55. /**
  56. * get server salt
  57. *
  58. * @access public
  59. * @static
  60. * @throws Exception
  61. * @return string
  62. */
  63. public static function get()
  64. {
  65. if (strlen(self::$_salt)) {
  66. return self::$_salt;
  67. }
  68. $salt = self::$_store->getValue('salt');
  69. if ($salt) {
  70. self::$_salt = $salt;
  71. } else {
  72. self::$_salt = self::generate();
  73. self::$_store->setValue(self::$_salt, 'salt');
  74. }
  75. return self::$_salt;
  76. }
  77. /**
  78. * set the path
  79. *
  80. * @access public
  81. * @static
  82. * @param AbstractData $store
  83. */
  84. public static function setStore(AbstractData $store)
  85. {
  86. self::$_salt = '';
  87. parent::setStore($store);
  88. }
  89. }