Support PECL xxtea

This commit is contained in:
djmaze 2020-03-19 23:11:04 +01:00
parent 3948f73fb1
commit 8fe8369b2a
3 changed files with 77 additions and 68 deletions

View file

@ -18,12 +18,35 @@ namespace MailSo\Base;
class Crypt
{
public static function XxteaEncrypt(string $sString, string $sKey) : string
public static function Encrypt(string $sString, string $sKey, string $sCipher = '') : string
{
if (0 === \strlen($sString))
{
if (0 === \strlen($sString)) {
return '';
}
if ($sCipher && \is_callable('openssl_encrypt')) {
$iv = str_pad('', openssl_cipher_iv_length($sCipher), sha1($sKey));
return openssl_encrypt($sString, $sCipher, $sKey, OPENSSL_RAW_DATA, $iv);
}
return static::XxteaEncrypt($sString, $sKey);
}
public static function Decrypt(string $sString, string $sKey, string $sCipher = '') : string
{
if (0 === \strlen($sString)) {
return '';
}
if ($sCipher && \is_callable('openssl_encrypt')) {
$iv = str_pad('', openssl_cipher_iv_length($sCipher), sha1($sKey));
return openssl_decrypt($sString, $sCipher, $sKey, OPENSSL_RAW_DATA, $iv);
}
return static::XxteaDecrypt($sString, $sKey);
}
private static function XxteaEncrypt(string $sString, string $sKey) : string
{
if (\is_callable('xxtea_encrypt')) {
return xxtea_encrypt($sString, $sKey);
}
$aV = self::str2long($sString, true);
$aK = self::str2long($sKey, false);
@ -61,14 +84,13 @@ class Crypt
return self::long2str($aV, false);
}
public static function XxteaDecrypt(string $sEncriptedString, string $sKey) : string
private static function XxteaDecrypt(string $sEncryptedString, string $sKey) : string
{
if (0 === \strlen($sEncriptedString))
{
return '';
if (\is_callable('xxtea_decrypt')) {
return xxtea_decrypt($sString, $sKey);
}
$aV = self::str2long($sEncriptedString, false);
$aV = self::str2long($sEncryptedString, false);
$aK = self::str2long($sKey, false);
if (\count($aK) < 4)
@ -128,10 +150,7 @@ class Crypt
{
return \substr(\join('', $aS), 0, $iN);
}
else
{
return \join('', $aS);
}
return \join('', $aS);
}
private static function str2long(string $sS, string $sW) : array
@ -147,14 +166,6 @@ class Crypt
private static function int32(int $iN) : int
{
while ($iN >= 2147483648)
{
$iN -= 4294967296;
}
while ($iN <= -2147483649)
{
$iN += 4294967296;
}
return (int) $iN;
return ($n & 0xffffffff);
}
}

View file

@ -1925,7 +1925,7 @@ END;
public static function HashToId(string $sHash, string $sSalt = '') : int
{
$sData = $sHash ? @\MailSo\Base\Crypt::XxteaDecrypt(\hex2bin($sHash), \md5($sSalt)) : null;
$sData = $sHash ? @\MailSo\Base\Crypt::Decrypt(\hex2bin($sHash), \md5($sSalt)) : null;
$aMatch = array();
if ($sData && preg_match('/^id:(\d+)$/', $sData, $aMatch) && isset($aMatch[1]))
@ -1938,8 +1938,6 @@ END;
public static function IdToHash(int $iID, string $sSalt = '') : string
{
return is_int($iID) ?
\bin2hex(\MailSo\Base\Crypt::XxteaEncrypt('id:'.$iID, \md5($sSalt))) : null
;
return \bin2hex(\MailSo\Base\Crypt::Encrypt('id:'.$iID, \md5($sSalt)));
}
}

View file

@ -36,15 +36,15 @@ class Utils
static public function RsaPrivateKey() : string
{
if (!empty(\RainLoop\Utils::$RsaKey))
if (!empty(static::$RsaKey))
{
return \RainLoop\Utils::$RsaKey;
return static::$RsaKey;
}
\RainLoop\Utils::$RsaKey = \file_exists(APP_PRIVATE_DATA.'rsa/private') ?
static::$RsaKey = \file_exists(APP_PRIVATE_DATA.'rsa/private') ?
\file_get_contents(APP_PRIVATE_DATA.'rsa/private') : '';
\RainLoop\Utils::$RsaKey = \is_string(\RainLoop\Utils::$RsaKey) ? \RainLoop\Utils::$RsaKey : '';
static::$RsaKey = \is_string(static::$RsaKey) ? static::$RsaKey : '';
}
static public function EncryptStringRSA(string $sString, string $sKey = '') : string
@ -52,7 +52,7 @@ class Utils
$sResult = '';
$sKey = \md5($sKey);
$sPrivateKey = \RainLoop\Utils::RsaPrivateKey();
$sPrivateKey = static::RsaPrivateKey();
if (!empty($sPrivateKey))
{
$oPrivKey = \openssl_pkey_get_private($sPrivateKey);
@ -89,7 +89,7 @@ class Utils
$sResult = '';
$sKey = \md5($sKey);
$sPrivateKey = \RainLoop\Utils::RsaPrivateKey();
$sPrivateKey = static::RsaPrivateKey();
if (!empty($sPrivateKey) && !empty($sString))
{
$oPrivKey = \openssl_pkey_get_private($sPrivateKey);
@ -118,48 +118,48 @@ class Utils
static public function EncryptString(string $sString, string $sKey) : string
{
return \MailSo\Base\Crypt::XxteaEncrypt($sString, $sKey);
return \MailSo\Base\Crypt::Encrypt($sString, $sKey);
}
static public function DecryptString(string $sEncriptedString, string $sKey) : string
static public function DecryptString(string $sEncryptedString, string $sKey) : string
{
return \MailSo\Base\Crypt::XxteaDecrypt($sEncriptedString, $sKey);
return \MailSo\Base\Crypt::Decrypt($sEncryptedString, $sKey);
}
static public function EncryptStringQ(string $sString, string $sKey) : string
{
// if (\MailSo\Base\Utils::FunctionExistsAndEnabled('openssl_pkey_get_private'))
// {
// return \RainLoop\Utils::EncryptStringRSA($sString,
// $sKey.'Q'.\RainLoop\Utils::GetShortToken());
// return static::EncryptStringRSA($sString,
// $sKey.'Q'.static::GetShortToken());
// }
return \MailSo\Base\Crypt::XxteaEncrypt($sString,
$sKey.'Q'.\RainLoop\Utils::GetShortToken());
return \MailSo\Base\Crypt::Encrypt($sString,
$sKey.'Q'.static::GetShortToken());
}
static public function DecryptStringQ(string $sEncriptedString, string $sKey) : string
static public function DecryptStringQ(string $sEncryptedString, string $sKey) : string
{
// if (\MailSo\Base\Utils::FunctionExistsAndEnabled('openssl_pkey_get_private'))
// {
// return \RainLoop\Utils::DecryptStringRSA($sEncriptedString,
// $sKey.'Q'.\RainLoop\Utils::GetShortToken());
// return static::DecryptStringRSA($sEncryptedString,
// $sKey.'Q'.static::GetShortToken());
// }
return \MailSo\Base\Crypt::XxteaDecrypt($sEncriptedString,
$sKey.'Q'.\RainLoop\Utils::GetShortToken());
return \MailSo\Base\Crypt::Decrypt($sEncryptedString,
$sKey.'Q'.static::GetShortToken());
}
static public function EncodeKeyValues(array $aValues, string $sCustomKey = '') : string
{
return \MailSo\Base\Utils::UrlSafeBase64Encode(
\RainLoop\Utils::EncryptString(@\serialize($aValues), \md5(APP_SALT.$sCustomKey)));
static::EncryptString(@\serialize($aValues), \md5(APP_SALT.$sCustomKey)));
}
static public function DecodeKeyValues(string $sEncodedValues, string $sCustomKey = '') : array
{
$aResult = @\unserialize(
\RainLoop\Utils::DecryptString(
static::DecryptString(
\MailSo\Base\Utils::UrlSafeBase64Decode($sEncodedValues), \md5(APP_SALT.$sCustomKey)));
return \is_array($aResult) ? $aResult : array();
@ -168,14 +168,14 @@ class Utils
static public function EncodeKeyValuesQ(array $aValues, string $sCustomKey = '') : string
{
return \MailSo\Base\Utils::UrlSafeBase64Encode(
\RainLoop\Utils::EncryptStringQ(
static::EncryptStringQ(
@\serialize($aValues), \md5(APP_SALT.$sCustomKey)));
}
static public function DecodeKeyValuesQ(string $sEncodedValues, string $sCustomKey = '') : array
{
$aResult = @\unserialize(
\RainLoop\Utils::DecryptStringQ(
static::DecryptStringQ(
\MailSo\Base\Utils::UrlSafeBase64Decode($sEncodedValues), \md5(APP_SALT.$sCustomKey)));
return \is_array($aResult) ? $aResult : array();
@ -185,11 +185,11 @@ class Utils
{
$sKey = 'rltoken';
$sToken = \RainLoop\Utils::GetCookie($sKey, null);
$sToken = static::GetCookie($sKey, null);
if (null === $sToken)
{
$sToken = \MailSo\Base\Utils::Md5Rand(APP_SALT);
\RainLoop\Utils::SetCookie($sKey, $sToken, \time() + 60 * 60 * 24 * 30);
static::SetCookie($sKey, $sToken, \time() + 60 * 60 * 24 * 30);
}
return \md5('Connection'.APP_SALT.$sToken.'Token'.APP_SALT);
@ -204,11 +204,11 @@ class Utils
{
$sKey = 'rlsession';
$sToken = \RainLoop\Utils::GetCookie($sKey, null);
$sToken = static::GetCookie($sKey, null);
if (null === $sToken)
{
$sToken = \MailSo\Base\Utils::Md5Rand(APP_SALT);
\RainLoop\Utils::SetCookie($sKey, $sToken, 0);
static::SetCookie($sKey, $sToken, 0);
}
return \md5('Session'.APP_SALT.$sToken.'Token'.APP_SALT);
@ -218,10 +218,10 @@ class Utils
{
$sKey = 'rltoken';
$sToken = \RainLoop\Utils::GetCookie($sKey, '');
$sToken = static::GetCookie($sKey, '');
if (!empty($sToken))
{
\RainLoop\Utils::SetCookie($sKey, $sToken, \time() + 60 * 60 * 24 * 30);
static::SetCookie($sKey, $sToken, \time() + 60 * 60 * 24 * 30);
}
}
@ -271,7 +271,7 @@ class Utils
}
else
{
$aLang = \RainLoop\Utils::CustomParseIniFile($sFileName, true);
$aLang = static::CustomParseIniFile($sFileName, true);
}
if (\is_array($aLang))
@ -349,7 +349,7 @@ class Utils
{
if (\file_exists($sDirName))
{
$aFileList = \RainLoop\Utils::FolderFiles($sDirName, '.html');
$aFileList = static::FolderFiles($sDirName, '.html');
foreach ($aFileList as $sName)
{
@ -365,47 +365,47 @@ class Utils
*/
public static function GetCookie(string $sName, $mDefault = null)
{
if (null === \RainLoop\Utils::$Cookies)
if (null === static::$Cookies)
{
\RainLoop\Utils::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
static::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
}
return isset(\RainLoop\Utils::$Cookies[$sName]) ? \RainLoop\Utils::$Cookies[$sName] : $mDefault;
return isset(static::$Cookies[$sName]) ? static::$Cookies[$sName] : $mDefault;
}
public static function SetCookie(string $sName, string $sValue = '', int $iExpire = 0, ?string $sPath = null, ?string $sDomain = null, ?bool $bSecure = null, bool $bHttpOnly = true)
{
if (null === \RainLoop\Utils::$Cookies)
if (null === static::$Cookies)
{
\RainLoop\Utils::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
static::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
}
if (null === $sPath)
{
$sPath = \RainLoop\Utils::$CookieDefaultPath;
$sPath = static::$CookieDefaultPath;
$sPath = $sPath && 0 < \strlen($sPath) ? $sPath : null;
}
if (null === $bSecure)
{
$bSecure = \RainLoop\Utils::$CookieDefaultSecure;
$bSecure = static::$CookieDefaultSecure;
}
\RainLoop\Utils::$Cookies[$sName] = $sValue;
static::$Cookies[$sName] = $sValue;
@\setcookie($sName, $sValue, $iExpire, $sPath, $sDomain, $bSecure, $bHttpOnly);
}
public static function ClearCookie(string $sName)
{
if (null === \RainLoop\Utils::$Cookies)
if (null === static::$Cookies)
{
\RainLoop\Utils::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
static::$Cookies = is_array($_COOKIE) ? $_COOKIE : array();
}
$sPath = \RainLoop\Utils::$CookieDefaultPath;
$sPath = static::$CookieDefaultPath;
$sPath = $sPath && 0 < \strlen($sPath) ? $sPath : null;
unset(\RainLoop\Utils::$Cookies[$sName]);
unset(static::$Cookies[$sName]);
@\setcookie($sName, '', \time() - 3600 * 24 * 30, $sPath);
}
@ -490,7 +490,7 @@ class Utils
if ($sFromBaseInput != '0123456789')
{
$sBase10 = \RainLoop\Utils::CustomBaseConvert($sNumberInput, $sFromBaseInput, '0123456789');
$sBase10 = static::CustomBaseConvert($sNumberInput, $sFromBaseInput, '0123456789');
}
else
{