我可以使用加密哈希作为文件名还是必须先将其转换?
我正在尝试对字符串进行哈希处理,然后使用该哈希值作为文件名。
我的问题:是我遇到的所有 C++ 哈希器/加密器都对 std::string 或 char* & 进行哈希处理。将散列字符串作为 unsigned char* 返回?
如何将 unsigned char* 转换为 char* 或 std::string 以便我可以将其写入文件或文件名?或者我不需要将其转换为普通字符串即可使用它?
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
散列的结果可能包含映射到文件名中不允许的字符的字节(例如空字符、“+”、“?”等......)。因此,代码中的“hashStr”不太可能是字符串,而只是一个不以 null 结尾的字节数组。
尝试这个小函数来从“二进制哈希转换为适合文件名的字符串”
在上面的代码示例中,您可以按如下方式调用它:
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: