update
This commit is contained in:
parent
cc9a995488
commit
4f9596e33e
29
Encryption.cpp
Normal file
29
Encryption.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user