2013-11-01 08:15:14 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ZeroBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-09-04 04:22:59 +08:00
|
|
|
* @version 0.20
|
2013-11-01 08:15:14 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serversalt
|
|
|
|
*
|
|
|
|
* This is a random string which is unique to each ZeroBin installation.
|
|
|
|
* It is automatically created if not present.
|
|
|
|
*
|
|
|
|
* Salt is used:
|
|
|
|
* - to generate unique VizHash in discussions (which are not reproductible across ZeroBin servers)
|
|
|
|
* - to generate unique deletion token (which are not re-usable across ZeroBin servers)
|
|
|
|
*/
|
|
|
|
class serversalt extends persistence
|
|
|
|
{
|
|
|
|
/**
|
2015-08-16 21:55:31 +08:00
|
|
|
* generated salt
|
|
|
|
*
|
2013-11-01 08:15:14 +08: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 = '';
|
2014-02-07 05:33:55 +08:00
|
|
|
if (function_exists('mcrypt_create_iv'))
|
|
|
|
{
|
|
|
|
$randomSalt = bin2hex(mcrypt_create_iv(256, MCRYPT_DEV_URANDOM));
|
|
|
|
}
|
|
|
|
else // fallback to mt_rand()
|
|
|
|
{
|
2015-08-28 03:41:21 +08:00
|
|
|
for($i = 0; $i < 256; ++$i) {
|
2014-02-07 05:33:55 +08:00
|
|
|
$randomSalt .= base_convert(mt_rand(), 10, 16);
|
|
|
|
}
|
2013-11-01 08:15:14 +08:00
|
|
|
}
|
2015-08-28 03:41:21 +08:00
|
|
|
return $randomSalt;
|
2013-11-01 08:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get server salt
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2015-08-28 03:41:21 +08:00
|
|
|
* @throws Exception
|
2013-11-01 08:15:14 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get()
|
|
|
|
{
|
2015-08-28 03:41:21 +08:00
|
|
|
if (strlen(self::$_salt)) return self::$_salt;
|
2013-11-01 08:15:14 +08:00
|
|
|
|
|
|
|
$file = 'salt.php';
|
2015-08-28 03:41:21 +08:00
|
|
|
if (self::_exists($file)) {
|
|
|
|
$items = explode('|', @file_get_contents(self::getPath($file)));
|
|
|
|
if (!is_array($items) || count($items) != 3) {
|
|
|
|
throw new Exception('unable to read file ' . self::getPath($file), 20);
|
|
|
|
}
|
|
|
|
self::$_salt = $items[1];
|
|
|
|
} else {
|
|
|
|
self::$_salt = self::generate();
|
2013-02-25 00:41:32 +08:00
|
|
|
self::_store(
|
2015-08-28 03:41:21 +08:00
|
|
|
$file,
|
|
|
|
'<?php /* |'. self::$_salt . '| */ ?>'
|
2013-02-25 00:41:32 +08:00
|
|
|
);
|
2013-11-01 08:15:14 +08:00
|
|
|
}
|
2015-08-28 03:41:21 +08:00
|
|
|
return self::$_salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the path
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setPath($path)
|
|
|
|
{
|
|
|
|
self::$_salt = '';
|
|
|
|
parent::setPath($path);
|
2013-11-01 08:15:14 +08:00
|
|
|
}
|
|
|
|
}
|