I need to get the SHA1 digest of a Unicode string (e.g. Hi), but I don't know how to do this.
The trick here is you need to know how to encode a Unicode string. On Windows, wchar_t is 2 octets; while on Linux a wchar_t is 4 off. There is a Crypto ++ wiki page on Character Set Features , but that's not so good.
To interact most effectively, always use UTF-8. This means that you are converting UTF-16 or UTF-32 to UTF-8. Since you are on Windows, you will need to call the WideCharToMultiByte function to convert it using CP_UTF8 . If you were on Linux, you would use libiconv .
Crypto ++ has a built-in StringNarrow function that uses C ++. Its in the misc.h file. Be sure to call setlocale before using it.
There are a few questions about using the Windows feature. See, for example, How to use WideCharToMultiByte correctly .
I need - 8dbe718ab1e0c4d75f7ab50fc9a53ec4f0528373
What is a hash (SHA-1, SHA-256, ...)? Is this HMAC (keyed hash)? Is the information salted (for example, the password in the repository)? How is this encoded? I have to ask because I cannot reproduce the desired results:
SHA-1: 2805AE8E7E12F182135F92FB90843BB1080D3BE8 SHA-224: 891CFB544EB6F3C212190705F7229D91DB6CECD4718EA65E0FA1B112 SHA-256: DD679C0B9FD408A04148AA7D30C9DF393F67B7227F65693FFFE0ED6D0F0ADE59 SHA-384: 0D83489095F455E4EF5186F2B071AB28E0D06132ABC9050B683DA28A463697AD 1195FF77F050F20AFBD3D5101DF18C0D SHA-512: 0F9F88EE4FA40D2135F98B839F601F227B4710F00C8BC48FDE78FF3333BD17E4 1D80AF9FE6FD68515A5F5F91E83E87DE3C33F899661066B638DB505C9CC0153D
Here is the program I used. Be sure to specify the length of the wide string. If you do not (and use -1 for length) then WideCharToMultiByte will include trailing ASCII-Z in its calculations. Since we use std::string , we do not need a function to include the ASCII-Z terminator.
int main(int argc, char* argv[]) { wstring m1 = L""; string m2; int req = WideCharToMultiByte(CP_UTF8, 0, m1.c_str(), (int)m1.length(), NULL, 0, NULL, NULL); if(req < 0 || req == 0) throw runtime_error("Failed to convert string"); m2.resize((size_t)req); int cch = WideCharToMultiByte(CP_UTF8, 0, m1.c_str(), (int)m1.length(), &m2[0], (int)m2.length(), NULL, NULL); if(cch < 0 || cch == 0) throw runtime_error("Failed to convert string");