我有一个计算URL哈希的函数:
#include <openssl/sha.h>
#include <string>
void url_hash(
std::string const& scheme,
std::string const& userinfo,
std::string const& host,
std::string const& domain,
std::string const& port,
std::string const& path,
std::string const& query,
std::string & url_hash)
{
std::string key(scheme + userinfo + host + domain + port + path + query);
char digest[SHA_DIGEST_LENGTH];
SHA1((const unsigned char*)(key.data()), key.size(), (unsigned char*)digest);
url_hash = base64::encode(digest + 0, digest + SHA_DIGEST_LENGTH);
}
这正常,通过'openssl sha1'和www.sha1-online.com
进行验证我想,让我们避免创建副本;我们可以在件上做SHA1。
void url_hash(
std::string const& scheme,
std::string const& userinfo,
std::string const& host,
std::string const& domain,
std::string const& port,
std::string const& path,
std::string const& query,
std::string & url_hash)
{
#define SHA_IT(s) SHA1_Update(&ctx, s.data(), s.size())
char digest[SHA_DIGEST_LENGTH]; // SHA1 is fixed size, 160 bits
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA_IT(scheme);
SHA_IT(userinfo);
SHA_IT(host);
SHA_IT(domain);
SHA_IT(port);
SHA_IT(path);
SHA_IT(query);
SHA_Final((unsigned char*)&digest, &ctx);
url_hash = base64::encode(digest + 0, digest + SHA_DIGEST_LENGTH);
#undef SHA_IT
}
不幸的是,以这种方式计算的摘要与一次计算的摘要不匹配:(
我一定是做错了,但我看不出它是什么。