This commit is contained in:
Yavuz Sava 2024-12-13 09:41:29 +03:00
parent cc9a995488
commit 4f9596e33e
2 changed files with 34 additions and 1 deletions

29
Encryption.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <openssl/sha.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <sstream>
#include <iomanip>
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;
}

View File

@ -1,3 +1,7 @@
# Sha256EncryptionInCpp
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: