While learning some basics with OpenSSL I came across code to create an SHA256 hash:
using namespace std;
#include <openssl/sha.h>
string sha256(const 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);
stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
Can someone please explain what ss << hex << setw(2) << setfill('0') << (int)hash[i];
does in this specific example as simply as possible, while still explaining it efficiently?
As I can't seem to make sense of these answers, they aren't helping with understanding my specific snippet: