2013-10-31 20:15:14 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 05:58:15 -04:00
|
|
|
* PrivateBin
|
2013-10-31 20:15:14 -04:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2013-10-31 20:15:14 -04:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 07:56:52 -04:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2021-04-05 10:44:12 -04:00
|
|
|
* @version 1.3.5
|
2013-10-31 20:15:14 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:49:08 -05:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
|
|
|
use Exception;
|
2021-06-08 16:01:29 -04:00
|
|
|
use PrivateBin\Data\AbstractData;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2013-10-31 20:15:14 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* ServerSalt
|
2013-10-31 20:15:14 -04:00
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* This is a random string which is unique to each PrivateBin installation.
|
2013-10-31 20:15:14 -04:00
|
|
|
* It is automatically created if not present.
|
|
|
|
*
|
|
|
|
* Salt is used:
|
2016-07-11 05:58:15 -04: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)
|
2013-10-31 20:15:14 -04:00
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class ServerSalt extends AbstractPersistence
|
2013-10-31 20:15:14 -04:00
|
|
|
{
|
2016-08-10 17:15:06 -04:00
|
|
|
/**
|
|
|
|
* file where salt is saved to
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_file = 'salt.php';
|
|
|
|
|
2013-10-31 20:15:14 -04:00
|
|
|
/**
|
2015-08-16 09:55:31 -04:00
|
|
|
* generated salt
|
|
|
|
*
|
2013-10-31 20:15:14 -04:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_salt = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generate a large random hexadecimal salt
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generate()
|
|
|
|
{
|
2016-08-10 17:15:06 -04:00
|
|
|
$randomSalt = bin2hex(random_bytes(256));
|
2015-08-27 15:41:21 -04:00
|
|
|
return $randomSalt;
|
2013-10-31 20:15:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get server salt
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-27 15:41:21 -04:00
|
|
|
* @throws Exception
|
2013-10-31 20:15:14 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get()
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (strlen(self::$_salt)) {
|
|
|
|
return self::$_salt;
|
|
|
|
}
|
2013-10-31 20:15:14 -04:00
|
|
|
|
2021-06-08 16:01:29 -04:00
|
|
|
$salt = self::$_store->getValue('salt');
|
|
|
|
if ($salt) {
|
|
|
|
self::$_salt = $salt;
|
2015-08-27 15:41:21 -04:00
|
|
|
} else {
|
|
|
|
self::$_salt = self::generate();
|
2021-06-08 16:01:29 -04:00
|
|
|
self::$_store->setValue(self::$_salt, 'salt');
|
2013-10-31 20:15:14 -04:00
|
|
|
}
|
2015-08-27 15:41:21 -04:00
|
|
|
return self::$_salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2021-06-08 16:01:29 -04:00
|
|
|
* @param AbstractData $store
|
2015-08-27 15:41:21 -04:00
|
|
|
*/
|
2021-06-08 16:01:29 -04:00
|
|
|
public static function setStore(AbstractData $store)
|
2015-08-27 15:41:21 -04:00
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
self::$_salt = '';
|
2021-06-08 16:01:29 -04:00
|
|
|
parent::setStore($store);
|
2013-10-31 20:15:14 -04:00
|
|
|
}
|
|
|
|
}
|