1
0

ServerSalt.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php declare(strict_types=1);
  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. */
  11. namespace PrivateBin\Persistence;
  12. use PrivateBin\Data\AbstractData;
  13. /**
  14. * ServerSalt
  15. *
  16. * This is a random string which is unique to each PrivateBin installation.
  17. * It is automatically created if not present.
  18. *
  19. * Salt is used:
  20. * - to generate unique VizHash in discussions (which are not reproductible across PrivateBin servers)
  21. * - to generate unique deletion token (which are not re-usable across PrivateBin servers)
  22. */
  23. class ServerSalt extends AbstractPersistence
  24. {
  25. /**
  26. * generated salt
  27. *
  28. * @access private
  29. * @static
  30. * @var string
  31. */
  32. private static $_salt = '';
  33. /**
  34. * generate a large random hexadecimal salt
  35. *
  36. * @access public
  37. * @static
  38. * @return string
  39. */
  40. public static function generate()
  41. {
  42. return bin2hex(random_bytes(256));
  43. }
  44. /**
  45. * get server salt
  46. *
  47. * @access public
  48. * @static
  49. * @return string
  50. */
  51. public static function get()
  52. {
  53. if (!empty(self::$_salt)) {
  54. return self::$_salt;
  55. }
  56. $salt = self::$_store->getValue('salt');
  57. if ($salt) {
  58. self::$_salt = $salt;
  59. } else {
  60. self::$_salt = self::generate();
  61. if (!self::$_store->setValue(self::$_salt, 'salt')) {
  62. error_log('failed to store the server salt, delete tokens, traffic limiter and user icons won\'t work');
  63. }
  64. }
  65. return self::$_salt;
  66. }
  67. /**
  68. * set the path
  69. *
  70. * @access public
  71. * @static
  72. * @param AbstractData $store
  73. */
  74. public static function setStore(AbstractData $store)
  75. {
  76. self::$_salt = '';
  77. parent::setStore($store);
  78. }
  79. }