I am trying to hash a string then use that hash as the name of a file.
My problem: is that all the C++ hashers/crypters I have come across hash a std::string or char* & return the hashed string as an unsigned char*?
How do I convert that unsigned char* to a char* or std::string so I can then write it to a file or filename? Or do I not need to convert it to a normal string to use it?
tstring hashString( tstring str )
{
// Post:
unsigned char hashStr[SHA256_DIGEST_SIZE];
std::string messageStr = str;
SHA256::getInstance()->digest( messageStr, hashStr );
//TCHAR *hashStrSigned = reinterpret_cast <TCHAR*>(hashStr);
// can I just use this hashStr to create a file? Or do I have to convert it to char* to use?
Handle newF = CreateFile( (LPTSTR)hashStr, GENERIC_ALL, 0, NULL, CREATE_ALWAYS,
0, NULL );
return tstring(hashStrSigned);
}
The result of the hash likely contain bytes that map to chars that are not allowed in a filename (e.g. null-char, ‘+’, ‘?’, etc….). So “hashStr” in your code isn’t likely to be a string, but just an array of bytes that aren’t null terminated.
Try this little function to convert from “binary hash to string suitable for file name”
In your above code example, you’d call it as follows: