ServerSalt.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * file where salt is saved to
  28. *
  29. * @access private
  30. * @static
  31. * @var string
  32. */
  33. private static $_file = 'salt.php';
  34. /**
  35. * generated salt
  36. *
  37. * @access private
  38. * @static
  39. * @var string
  40. */
  41. private static $_salt = '';
  42. /**
  43. * generate a large random hexadecimal salt
  44. *
  45. * @access public
  46. * @static
  47. * @return string
  48. */
  49. public static function generate()
  50. {
  51. return bin2hex(random_bytes(256));
  52. }
  53. /**
  54. * get server salt
  55. *
  56. * @access public
  57. * @static
  58. * @return string
  59. */
  60. public static function get()
  61. {
  62. if (strlen(self::$_salt)) {
  63. return self::$_salt;
  64. }
  65. $salt = self::$_store->getValue('salt');
  66. if ($salt) {
  67. self::$_salt = $salt;
  68. } else {
  69. self::$_salt = self::generate();
  70. self::$_store->setValue(self::$_salt, 'salt');
  71. }
  72. return self::$_salt;
  73. }
  74. /**
  75. * set the path
  76. *
  77. * @access public
  78. * @static
  79. * @param AbstractData $store
  80. */
  81. public static function setStore(AbstractData $store)
  82. {
  83. self::$_salt = '';
  84. parent::setStore($store);
  85. }
  86. }