From f403b8235a30ca7f267408ff2312f41cee9931f4 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 23 May 2022 06:44:18 +0000 Subject: [PATCH] secret generation extracted to open SSL --- app/controllers/api/projects.php | 6 +++--- app/controllers/api/storage.php | 2 +- src/Appwrite/OpenSSL/OpenSSL.php | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index cd34dade8e..b755e7b4c1 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -104,8 +104,8 @@ App::post('/v1/projects') 'keys' => null, 'domains' => null, 'auths' => $auths, - 'databaseSecrets' => [\uniqid() => \bin2hex(OpenSSL::randomPseudoBytes(128))], - 'jwtSecrets' => \bin2hex(OpenSSL::randomPseudoBytes(128)), + 'databaseSecrets' => [\uniqid() => OpenSSL::secretString()], + 'jwtSecrets' => OpenSSL::secretString(), 'search' => implode(' ', [$projectId, $name]), ])); /** @var array $collections */ @@ -797,7 +797,7 @@ App::post('/v1/projects/:projectId/keys') 'projectId' => $project->getId(), 'name' => $name, 'scopes' => $scopes, - 'secret' => \bin2hex(\random_bytes(128)), + 'secret' => Auth::tokenGenerator(), ]); $key = $dbForConsole->createDocument('keys', $key); diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index eb4beff7c8..cb52182198 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -515,7 +515,7 @@ App::post('/v1/storage/buckets/:bucketId/files') $data = $deviceFiles->read($path); } - $fileSecret = \bin2hex(OpenSSL::randomPseudoBytes(128)); + $fileSecret = OpenSSL::secretString(); $iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM)); $tag = null; diff --git a/src/Appwrite/OpenSSL/OpenSSL.php b/src/Appwrite/OpenSSL/OpenSSL.php index 93cce6862d..b500bdd118 100644 --- a/src/Appwrite/OpenSSL/OpenSSL.php +++ b/src/Appwrite/OpenSSL/OpenSSL.php @@ -20,7 +20,6 @@ class OpenSSL */ public static function encrypt($data, $method, $key, $options = 0, $iv = '', &$tag = null, $aad = '', $tag_length = 16) { - var_dump($data); return \openssl_encrypt($data, $method, $key, $options, $iv, $tag, $aad, $tag_length); } @@ -60,4 +59,20 @@ class OpenSSL { return \openssl_random_pseudo_bytes($length, $crypto_strong); } + + /** + * Secret String + * + * Generate random encryption secret + * + * @param int $length + * + * @return string + * + * @throws \Exception + */ + public static function secretString(int $length = 128):string + { + return \bin2hex(self::randomPseudoBytes($length)); + } }