ServerSalt.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.6.1
  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. if (!self::$_store->setValue(self::$_salt, 'salt')) {
  63. error_log('failed to store the server salt, delete tokens, traffic limiter and user icons won\'t work');
  64. }
  65. }
  66. return self::$_salt;
  67. }
  68. /**
  69. * set the path
  70. *
  71. * @access public
  72. * @static
  73. * @param AbstractData $store
  74. */
  75. public static function setStore(AbstractData $store)
  76. {
  77. self::$_salt = '';
  78. parent::setStore($store);
  79. }
  80. }