Fix for doing base64(hex(sha1))
Instead we want to base64 encode the raw sha1
This commit is contained in:
@@ -33,3 +33,22 @@ std::string sha1(const std::string& input)
|
||||
return digest.str();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string sha1_raw(const std::string& input)
|
||||
{
|
||||
#ifdef BOTAN_FOUND
|
||||
auto sha1 = Botan::HashFunction::create_or_throw("SHA-1");
|
||||
sha1->update(input);
|
||||
const auto digest = sha1->final();
|
||||
return std::string(
|
||||
reinterpret_cast<const char*>(digest.data()),
|
||||
digest.size()
|
||||
);
|
||||
#endif
|
||||
#ifdef GCRYPT_FOUND
|
||||
const auto hash_length = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
|
||||
std::string output(hash_length, '\0');
|
||||
gcry_md_hash_buffer(GCRY_MD_SHA1, output.data(), input.data(), input.size());
|
||||
return output;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
#include <string>
|
||||
|
||||
std::string sha1(const std::string& input);
|
||||
std::string sha1_raw(const std::string& input);
|
||||
|
||||
@@ -42,7 +42,7 @@ const std::string hash_disco_sha1(const Stanza &stanza) {
|
||||
s += var + "<";
|
||||
}
|
||||
|
||||
const std::string hashed = sha1(s);
|
||||
const std::string hashed = sha1_raw(s);
|
||||
const std::string encoded = base64::encode(hashed);
|
||||
|
||||
return encoded;
|
||||
|
||||
@@ -32,7 +32,7 @@ TEST_CASE("Hash disco 1") {
|
||||
|
||||
auto check_stanza = [](const Stanza &stanza) {
|
||||
const std::string hash = Xmpp::hash_disco_sha1(stanza);
|
||||
CHECK(hash == "YjVkN2NhODU2ZWQyZjhhNjJjOTE1MWMxYjQ0YmUwM2Q0ODhhZDI2MA==");
|
||||
CHECK(hash == "tdfKhW7S+KYskVHBtEvgPUiK0mA=");
|
||||
};
|
||||
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
|
||||
check_stanza(stanza);
|
||||
@@ -62,7 +62,7 @@ TEST_CASE("Hash disco 2") {
|
||||
|
||||
auto check_stanza = [](const Stanza &stanza) {
|
||||
const std::string hash = Xmpp::hash_disco_sha1(stanza);
|
||||
CHECK(hash == "ZmY1ZTJjZjUyZjkwMjA2OTliZGFhYzYxMmVmMTI5YjdmYzNhZTIwYw==");
|
||||
CHECK(hash == "/14s9S+QIGmb2qxhLvEpt/w64gw=");
|
||||
};
|
||||
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
|
||||
check_stanza(stanza);
|
||||
@@ -91,7 +91,7 @@ TEST_CASE("Hash disco 2 different order") {
|
||||
|
||||
auto check_stanza = [](const Stanza &stanza) {
|
||||
const std::string hash = Xmpp::hash_disco_sha1(stanza);
|
||||
CHECK(hash == "ZmY1ZTJjZjUyZjkwMjA2OTliZGFhYzYxMmVmMTI5YjdmYzNhZTIwYw==");
|
||||
CHECK(hash == "/14s9S+QIGmb2qxhLvEpt/w64gw=");
|
||||
};
|
||||
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
|
||||
check_stanza(stanza);
|
||||
|
||||
Reference in New Issue
Block a user