Merge branch 'develop' into pr/session-class

This commit is contained in:
Kijin Sung 2016-08-15 15:59:19 +09:00
commit 6de0bc45db
51 changed files with 358 additions and 96 deletions

View file

@ -160,7 +160,7 @@ class ConfigParser
// Create new crypto keys.
$config['crypto']['encryption_key'] = Security::getRandom(64, 'alnum');
$config['crypto']['authentication_key'] = Security::getRandom(64, 'alnum');
$config['crypto']['authentication_key'] = $db_info->secret_key ?: Security::getRandom(64, 'alnum');
$config['crypto']['session_key'] = Security::getRandom(64, 'alnum');
// Convert language configuration.

View file

@ -112,6 +112,40 @@ class Security
return \CryptoCompat::decrypt($ciphertext, $key);
}
/**
* Create a digital signature to verify the authenticity of a string.
*
* @param string $string
* @return string
*/
public static function createSignature($string)
{
$key = config('crypto.authentication_key');
$salt = self::getRandom(8, 'alnum');
$hash = substr(base64_encode(hash_hmac('sha256', hash_hmac('sha256', $string, $salt), $key, true)), 0, 32);
return $salt . strtr($hash, '+/', '-_');
}
/**
* Check whether a signature is valid.
*
* @param string $string
* @param string $signature
* @return bool
*/
public static function verifySignature($string, $signature)
{
if(strlen($signature) !== 40)
{
return false;
}
$key = config('crypto.authentication_key');
$salt = substr($signature, 0, 8);
$hash = substr(base64_encode(hash_hmac('sha256', hash_hmac('sha256', $string, $salt), $key, true)), 0, 32);
return self::compareStrings(substr($signature, 8), strtr($hash, '+/', '-_'));
}
/**
* Generate a cryptographically secure random string.
*

View file

@ -256,17 +256,17 @@ class Storage
flock($fp, \LOCK_EX);
if (is_resource($content))
{
$result = stream_copy_to_stream($content, $fp) ? true : false;
$result = stream_copy_to_stream($content, $fp);
}
else
{
$result = fwrite($fp, $content) ? true : false;
$result = fwrite($fp, $content);
}
fflush($fp);
flock($fp, \LOCK_UN);
fclose($fp);
if (!$result)
if ($result === false)
{
trigger_error('Cannot write file: ' . (isset($original_filename) ? $original_filename : $filename), \E_USER_WARNING);
return false;
@ -303,7 +303,7 @@ class Storage
}
clearstatcache(true, $filename);
return $result;
return true;
}
/**