【发布时间】:2015-06-27 14:43:59
【问题描述】:
我独立学习 C++,但有一个问题,我无法解决超过一周的问题。我希望你能帮助我。
我需要获取 Unicode 字符串的 SHA1 摘要(如 Привет),但我不知道该怎么做。
我尝试这样做,但它返回错误的摘要!
对于wstring('Ы')
它返回 - A469A61DF29A7568A6CC63318EA8741FA1CF2A7
我需要-8dbe718ab1e0c4d75f7ab50fc9a53ec4f0528373
对于我的英语的问候和抱歉:)。
加密PP 5.6.2 MVC++ 2013
#include <iostream>
#include "cryptopp562\cryptlib.h"
#include "cryptopp562\sha.h"
#include "cryptopp562\hex.h"
int main() {
std::wstring string(L"Ы");
int bs_size = (int)string.length() * sizeof(wchar_t);
byte* bytes_string = new byte[bs_size];
int n = 0; //real bytes count
for (int i = 0; i < string.length(); i++) {
wchar_t wcharacter = string[i];
int high_byte = wcharacter & 0xFF00;
high_byte = high_byte >> 8;
int low_byte = wcharacter & 0xFF;
if (high_byte != 0) {
bytes_string[n++] = (byte)high_byte;
}
bytes_string[n++] = (byte)low_byte;
}
CryptoPP::SHA1 sha1;
std::string hash;
CryptoPP::StringSource ss(bytes_string, n, true,
new CryptoPP::HashFilter(sha1,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(hash)
)
)
);
std::cout << hash << std::endl;
return 0;
}
【问题讨论】:
-
“我试着这样做,但它返回错误的摘要!” - Crypto++ 代码看起来不错,所以问题可能出在其他地方。它产生了什么摘要,您期望什么摘要?我怀疑您需要转换为 UTF-8 的宽字符串的摘要。 UTF-8 是最具互操作性的。通过单击编辑将预期的和实际的摘要添加到您的问题中(不要将其作为评论发布)。
标签: c++ unicode sha1 crypto++ digest