From 4f9596e33eb488a07ac84cce5d623dd1dec4a9e7 Mon Sep 17 00:00:00 2001 From: Woose Date: Fri, 13 Dec 2024 09:41:29 +0300 Subject: [PATCH] update --- Encryption.cpp | 29 +++++++++++++++++++++++++++++ README.md | 6 +++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Encryption.cpp diff --git a/Encryption.cpp b/Encryption.cpp new file mode 100644 index 0000000..c367dea --- /dev/null +++ b/Encryption.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +string SHA256(const string str) +{ + unsigned char hash[SHA256_DIGEST_LENGTH]; // Initialize a new context for digest operations using the EVP API + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); // Set up the context to use the SHA-256 algorithm + EVP_DigestUpdate(ctx, str.c_str(), str.size()); // Update the digest with the input string's bytes + EVP_DigestFinal_ex(ctx, hash, NULL); // Finish computing the hash and store it in the 'hash' buffer + EVP_MD_CTX_free(ctx); // Free up the context used for digest operations + stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) // Convert each byte of the hash into a two-digit hexadecimal number + ss << hex << setw(2) << setfill('0') << (int)hash[i]; + return ss.str(); +} +int main() +{ + string str; + cout << "Enter a string to encrypt: "; + getline(cin, str); + // Compute the SHA-256 hash of the input string + cout << "Encrypted string using SHA-256 algorithm: " << SHA256(str) << endl; + return 0; +} diff --git a/README.md b/README.md index 1e5d365..ea8b942 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # Sha256EncryptionInCpp -Sha256 encryption program written in C++ \ No newline at end of file +Sha256 encryption program written in C++ + + Usage example: + cd "/VSCodeLibrary/C++/" && g++ Encryption.cpp -o Encryption -lssl -lcrypto && "/VSCodeLibrary/C++/"Encryption + Enter a string to encrypt: \ No newline at end of file