PrivateBin/lib/Persistence/ServerSalt.php

88 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
2016-07-11 17:58:15 +08:00
* PrivateBin
*
* a zero-knowledge paste bin
*
2016-07-11 17:58:15 +08:00
* @link https://github.com/PrivateBin/PrivateBin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
2023-12-15 14:20:20 +08:00
* @version 1.6.2
*/
2016-12-13 01:43:23 +08:00
2016-12-13 01:49:08 +08:00
namespace PrivateBin\Persistence;
2016-07-21 23:09:48 +08:00
use PrivateBin\Data\AbstractData;
2016-07-21 23:09:48 +08:00
/**
* ServerSalt
*
2016-07-11 17:58:15 +08:00
* This is a random string which is unique to each PrivateBin installation.
* It is automatically created if not present.
*
* Salt is used:
2016-07-11 17:58:15 +08:00
* - to generate unique VizHash in discussions (which are not reproductible across PrivateBin servers)
* - to generate unique deletion token (which are not re-usable across PrivateBin servers)
*/
class ServerSalt extends AbstractPersistence
{
/**
* generated salt
*
* @access private
* @static
* @var string
*/
private static $_salt = '';
/**
* generate a large random hexadecimal salt
*
* @access public
* @static
* @return string
*/
public static function generate()
{
2021-06-13 16:44:26 +08:00
return bin2hex(random_bytes(256));
}
/**
* get server salt
*
* @access public
* @static
* @return string
*/
public static function get()
{
if (strlen(self::$_salt)) {
return self::$_salt;
}
$salt = self::$_store->getValue('salt');
if ($salt) {
self::$_salt = $salt;
} else {
self::$_salt = self::generate();
2021-06-16 11:32:45 +08:00
if (!self::$_store->setValue(self::$_salt, 'salt')) {
error_log('failed to store the server salt, delete tokens, traffic limiter and user icons won\'t work');
}
}
return self::$_salt;
}
/**
* set the path
*
* @access public
* @static
* @param AbstractData $store
*/
public static function setStore(AbstractData $store)
{
self::$_salt = '';
parent::setStore($store);
}
}