Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Log hash of key also when logging key prefix.

kishorence5c3cddfcc8c
diff --git a/include/string_utils.h b/include/string_utils.h
index 2f2276be..7f641527 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -291,5 +291,7 @@ struct StringUtils {
static std::string hmac(const std::string& key, const std::string& msg);
+ static std::string hash_sha256(const std::string& str);
+
//static size_t unicode_length(const std::string& bytes);
};
\ No newline at end of file
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 50b4b943..592a3a67 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -298,7 +298,10 @@ Option<bool> AuthManager::authenticate_parse_params(const std::string& scoped_ap
}
std::string AuthManager::fmt_error(std::string&& error, const std::string& key) {
- return error + " Key prefix: " + key.substr(0, api_key_t::PREFIX_LEN);
+ std::stringstream ss;
+ ss << error << " Key prefix: " << key.substr(0, api_key_t::PREFIX_LEN) << ", SHA256: "
+ << StringUtils::hash_sha256(key);
+ return ss.str();
}
Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
diff --git a/src/string_utils.cpp b/src/string_utils.cpp
index da0a0582..644a7d3e 100644
--- a/src/string_utils.cpp
+++ b/src/string_utils.cpp
@@ -3,6 +3,7 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <random>
+#include <openssl/sha.h>
std::string lower_and_no_special_chars(const std::string & str) {
std::stringstream ss;
@@ -106,6 +107,13 @@ std::string StringUtils::str2hex(const std::string &str, bool capital) {
return hexstr;
}
+std::string StringUtils::hash_sha256(const std::string& str) {
+ const size_t SHA256_SIZE = 32;
+ unsigned char hash_buf[SHA256_SIZE];
+ SHA256(reinterpret_cast<const unsigned char *>(str.c_str()), str.size(), hash_buf);
+ return StringUtils::str2hex(std::string(reinterpret_cast<char*>(hash_buf), SHA256_SIZE));
+}
+
/*size_t StringUtils::unicode_length(const std::string& bytes) {
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf8conv;
return utf8conv.from_bytes(bytes).size();
diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cpp
index 54a4b119..d2b39550 100644
--- a/test/string_utils_test.cpp
+++ b/test/string_utils_test.cpp
@@ -123,3 +123,14 @@ TEST(StringUtilsTest, ShouldTrimString) {
StringUtils::trim(str);
ASSERT_STREQ("", str.c_str());
}
+
+TEST(StringUtilsTest, ShouldComputeSHA256) {
+ ASSERT_STREQ("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
+ StringUtils::hash_sha256("foobar").c_str());
+
+ ASSERT_STREQ("d8705968091d40b60436675240712c584c187eef091514d4092483dc342ca3de",
+ StringUtils::hash_sha256("some random key").c_str());
+
+ ASSERT_STREQ("6613f67d3d78d48e2678faf55c33fabc5895c538ce70ea10218ce9b7eccbf394",
+ StringUtils::hash_sha256("791a27668b3e01fc6ab3482b6e6a36255154df3ecd7dcec").c_str());
+}