2013-11-01 00:15:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 09:58:15 +00:00
|
|
|
* PrivateBin
|
2013-11-01 00:15:14 +00:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 09:58:15 +00:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2013-11-01 00:15:14 +00:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 11:56:52 +00:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-11-09 20:39:42 +00:00
|
|
|
* @version 0.22
|
2013-11-01 00:15:14 +00:00
|
|
|
*/
|
|
|
|
|
2016-08-09 09:54:42 +00:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-07-21 15:09:48 +00:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
2013-11-01 00:15:14 +00:00
|
|
|
/**
|
2016-08-09 09:54:42 +00:00
|
|
|
* ServerSalt
|
2013-11-01 00:15:14 +00:00
|
|
|
*
|
2016-07-11 09:58:15 +00:00
|
|
|
* This is a random string which is unique to each PrivateBin installation.
|
2013-11-01 00:15:14 +00:00
|
|
|
* It is automatically created if not present.
|
|
|
|
*
|
|
|
|
* Salt is used:
|
2016-07-11 09:58:15 +00: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-11-01 00:15:14 +00:00
|
|
|
*/
|
2016-08-09 09:54:42 +00:00
|
|
|
class ServerSalt extends AbstractPersistence
|
2013-11-01 00:15:14 +00:00
|
|
|
{
|
|
|
|
/**
|
2015-08-16 13:55:31 +00:00
|
|
|
* generated salt
|
|
|
|
*
|
2013-11-01 00:15:14 +00:00
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_salt = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generate a large random hexadecimal salt
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function generate()
|
|
|
|
{
|
|
|
|
$randomSalt = '';
|
2016-07-26 06:19:35 +00:00
|
|
|
if (function_exists('mcrypt_create_iv')) {
|
2014-02-06 21:33:55 +00:00
|
|
|
$randomSalt = bin2hex(mcrypt_create_iv(256, MCRYPT_DEV_URANDOM));
|
2016-07-26 06:19:35 +00:00
|
|
|
} else {
|
|
|
|
// fallback to mt_rand()
|
|
|
|
|
|
|
|
for ($i = 0; $i < 256; ++$i) {
|
2014-02-06 21:33:55 +00:00
|
|
|
$randomSalt .= base_convert(mt_rand(), 10, 16);
|
|
|
|
}
|
2013-11-01 00:15:14 +00:00
|
|
|
}
|
2015-08-27 19:41:21 +00:00
|
|
|
return $randomSalt;
|
2013-11-01 00:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get server salt
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-27 19:41:21 +00:00
|
|
|
* @throws Exception
|
2013-11-01 00:15:14 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get()
|
|
|
|
{
|
2016-07-26 06:19:35 +00:00
|
|
|
if (strlen(self::$_salt)) {
|
|
|
|
return self::$_salt;
|
|
|
|
}
|
2013-11-01 00:15:14 +00:00
|
|
|
|
|
|
|
$file = 'salt.php';
|
2015-08-27 19:41:21 +00:00
|
|
|
if (self::_exists($file)) {
|
2016-08-09 09:54:42 +00:00
|
|
|
if (is_readable(self::getPath($file))) {
|
|
|
|
$items = explode('|', file_get_contents(self::getPath($file)));
|
|
|
|
}
|
|
|
|
if (!isset($items) || !is_array($items) || count($items) != 3) {
|
2015-08-27 19:41:21 +00:00
|
|
|
throw new Exception('unable to read file ' . self::getPath($file), 20);
|
|
|
|
}
|
|
|
|
self::$_salt = $items[1];
|
|
|
|
} else {
|
|
|
|
self::$_salt = self::generate();
|
2013-02-24 16:41:32 +00:00
|
|
|
self::_store(
|
2015-08-27 19:41:21 +00:00
|
|
|
$file,
|
2016-08-09 09:54:42 +00:00
|
|
|
'<?php /* |' . self::$_salt . '| */ ?>'
|
2013-02-24 16:41:32 +00:00
|
|
|
);
|
2013-11-01 00:15:14 +00:00
|
|
|
}
|
2015-08-27 19:41:21 +00:00
|
|
|
return self::$_salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setPath($path)
|
|
|
|
{
|
2016-07-26 06:19:35 +00:00
|
|
|
self::$_salt = '';
|
2015-08-27 19:41:21 +00:00
|
|
|
parent::setPath($path);
|
2013-11-01 00:15:14 +00:00
|
|
|
}
|
|
|
|
}
|