Created
January 4, 2021 14:47
-
-
Save TyeolRik/a30234c39b8c697d4da916940c275f32 to your computer and use it in GitHub Desktop.
OpenSSL SHA256 Simple Usage Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In Ubuntu 20.04 LTS | |
// g++ -g [cpp file name] -o [output file name] -O2 -lcrypto | |
#include <iostream> | |
#include <iomanip> | |
#include <sstream> | |
#include <openssl/sha.h> | |
std::string sha256(const std::string str) | |
{ | |
unsigned char hash[SHA256_DIGEST_LENGTH]; | |
SHA256_CTX sha256; | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, str.c_str(), str.size()); | |
SHA256_Final(hash, &sha256); | |
std::stringstream ss; | |
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) | |
{ | |
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; | |
} | |
return ss.str(); | |
} | |
int main() { | |
std::cout << sha256("hello world"); // SHA-256 example. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment