paste.chapril.org-privatebin/lib/Persistence/ServerSalt.php

88 lines
2.0 KiB
PHP
Raw Normal View History

2020-01-14 21:42:06 +01:00
<?php
/**
* PrivateBin
*
* a zero-knowledge paste bin
*
* @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
2022-05-02 17:08:35 +02:00
* @version 1.4.0
2020-01-14 21:42:06 +01:00
*/
namespace PrivateBin\Persistence;
2022-05-02 17:08:35 +02:00
use PrivateBin\Data\AbstractData;
2020-01-14 21:42:06 +01:00
/**
* ServerSalt
*
* This is a random string which is unique to each PrivateBin installation.
* It is automatically created if not present.
*
* Salt is used:
* - 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()
{
2022-05-02 17:08:35 +02:00
return bin2hex(random_bytes(256));
2020-01-14 21:42:06 +01:00
}
/**
* get server salt
*
* @access public
* @static
* @return string
*/
public static function get()
{
if (strlen(self::$_salt)) {
return self::$_salt;
}
2022-05-02 17:08:35 +02:00
$salt = self::$_store->getValue('salt');
if ($salt) {
self::$_salt = $salt;
2020-01-14 21:42:06 +01:00
} else {
self::$_salt = self::generate();
2022-05-02 17:08:35 +02: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');
}
2020-01-14 21:42:06 +01:00
}
return self::$_salt;
}
/**
* set the path
*
* @access public
* @static
2022-05-02 17:08:35 +02:00
* @param AbstractData $store
2020-01-14 21:42:06 +01:00
*/
2022-05-02 17:08:35 +02:00
public static function setStore(AbstractData $store)
2020-01-14 21:42:06 +01:00
{
self::$_salt = '';
2022-05-02 17:08:35 +02:00
parent::setStore($store);
2020-01-14 21:42:06 +01:00
}
}