]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Crypto.php
Display mentioned users and in the comment box by default
[friendica.git] / src / Util / Crypto.php
index 3426babe36e27aa2102bf6d36152f80a939903ed..2ab97e5373b5a53fd276ba5ab859af6f0fbf5c2c 100644 (file)
@@ -4,10 +4,9 @@
  */
 namespace Friendica\Util;
 
-use Friendica\Core\Addon;
 use Friendica\Core\Config;
+use Friendica\Core\Hook;
 use Friendica\Core\Logger;
-use Friendica\Util\Strings;
 use ASN_BASE;
 use ASNValue;
 
@@ -43,7 +42,7 @@ class Crypto
 
        /**
         * @param string $Der     der formatted string
-        * @param string $Private key type optional, default false
+        * @param bool   $Private key type optional, default false
         * @return string
         */
        private static function DerToPem($Der, $Private = false)
@@ -150,6 +149,7 @@ class Crypto
         * @param string $m   modulo reference
         * @param object $e   exponent reference
         * @return void
+        * @throws \Exception
         */
        private static function pubRsaToMe($key, &$m, &$e)
        {
@@ -167,6 +167,7 @@ class Crypto
        /**
         * @param string $key key
         * @return string
+        * @throws \Exception
         */
        public static function rsaToPem($key)
        {
@@ -177,6 +178,7 @@ class Crypto
        /**
         * @param string $key key
         * @return string
+        * @throws \Exception
         */
        private static function pemToRsa($key)
        {
@@ -189,6 +191,7 @@ class Crypto
         * @param string $m   modulo reference
         * @param string $e   exponent reference
         * @return void
+        * @throws \Exception
         */
        public static function pemToMe($key, &$m, &$e)
        {
@@ -218,6 +221,7 @@ class Crypto
        /**
         * @param integer $bits number of bits
         * @return mixed
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function newKeypair($bits)
        {
@@ -319,14 +323,15 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param string $data
         * @param string $pubkey The public key.
         * @param string $alg    The algorithm used for encryption.
-        * 
+        *
         * @return array
+        * @throws \Exception
         */
        public static function encapsulate($data, $pubkey, $alg = 'aes256cbc')
        {
@@ -337,14 +342,15 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param type $data
-        * @param type $pubkey The public key.
-        * @param type $alg    The algorithm used for encryption.
-        * 
+        *
+        * @param string $data
+        * @param string $pubkey The public key.
+        * @param string $alg    The algorithm used for encryption.
+        *
         * @return array
+        * @throws \Exception
         */
        private static function encapsulateOther($data, $pubkey, $alg)
        {
@@ -372,20 +378,21 @@ class Crypto
                        return $result;
                } else {
                        $x = ['data' => $data, 'pubkey' => $pubkey, 'alg' => $alg, 'result' => $data];
-                       Addon::callHooks('other_encapsulate', $x);
+                       Hook::callAll('other_encapsulate', $x);
 
                        return $x['result'];
                }
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param string $data
         * @param string $pubkey
-        * 
+        *
         * @return array
+        * @throws \Exception
         */
        private static function encapsulateAes($data, $pubkey)
        {
@@ -413,15 +420,16 @@ class Crypto
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param string $data
-        * @param string $prvkey  The private key used for decryption.
-        * 
+        *
+        * @param array $data ['iv' => $iv, 'key' => $key, 'alg' => $alg, 'data' => $data]
+        * @param string $prvkey The private key used for decryption.
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Exception
         */
-       public static function unencapsulate($data, $prvkey)
+       public static function unencapsulate(array $data, $prvkey)
        {
                if (!$data) {
                        return;
@@ -429,22 +437,23 @@ class Crypto
 
                $alg = ((array_key_exists('alg', $data)) ? $data['alg'] : 'aes256cbc');
                if ($alg === 'aes256cbc') {
-                       return self::encapsulateAes($data, $prvkey);
+                       return self::encapsulateAes($data['data'], $prvkey);
                }
-               return self::encapsulateOther($data, $prvkey, $alg);
+               return self::encapsulateOther($data['data'], $prvkey, $alg);
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
-        * @param string $data
-        * @param string $prvkey  The private key used for decryption.
+        *
+        * @param array $data
+        * @param string $prvkey The private key used for decryption.
         * @param string $alg
-        * 
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function unencapsulateOther($data, $prvkey, $alg)
+       private static function unencapsulateOther(array $data, $prvkey, $alg)
        {
                $fn = 'decrypt' . strtoupper($alg);
 
@@ -455,20 +464,21 @@ class Crypto
                        return self::$fn(Strings::base64UrlDecode($data['data']), $k, $i);
                } else {
                        $x = ['data' => $data, 'prvkey' => $prvkey, 'alg' => $alg, 'result' => $data];
-                       Addon::callHooks('other_unencapsulate', $x);
+                       Hook::callAll('other_unencapsulate', $x);
 
                        return $x['result'];
                }
        }
 
        /**
-        * 
+        *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/crypto.php
-        * 
+        *
         * @param array  $data
-        * @param string $prvkey  The private key used for decryption.
-        * 
+        * @param string $prvkey The private key used for decryption.
+        *
         * @return string|boolean The decrypted string or false on failure.
+        * @throws \Exception
         */
        private static function unencapsulateAes($data, $prvkey)
        {