Skip to content

Commit 33e4a94

Browse files
committed
[libs][encrypt] initialize plaintext only if file is open
1 parent 6d7179f commit 33e4a94

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

libs/libencrypt/src/encryption.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,19 @@ bool Encryption::encryptFile(const std::string& publicKeyFile, std::string& file
204204

205205
// Read file contents
206206
std::ifstream file(filePath, std::ios::binary);
207+
std::string plaintext;
207208
if (!file) {
208209
std::cerr << "Unable to open file: " << filePath << std::endl;
209210
EVP_PKEY_free(publicKey);
210211
return false;
212+
} else {
213+
std::istreambuf_iterator<char> begin(file);
214+
std::istreambuf_iterator<char> end;
215+
plaintext = std::string(begin, end);
216+
file.close();
211217
}
212218

213-
std::istreambuf_iterator<char> begin(file);
214-
std::istreambuf_iterator<char> end;
215-
std::string plaintext(begin, end);
216-
file.close();
219+
217220

218221
// Encrypt session key
219222
std::string encryptedSessionKey = encryptSessionKey(sessionKey, publicKey);

0 commit comments

Comments
 (0)