forked from kuzudb/kuzu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility hash functions (kuzudb#3212)
* Add utility hash functions * Run clang-format --------- Co-authored-by: CI Bot <[email protected]>
- Loading branch information
Showing
81 changed files
with
336 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "common/sha256.h" | ||
|
||
#include "common/exception/runtime.h" | ||
|
||
namespace kuzu { | ||
namespace common { | ||
|
||
SHA256::SHA256() { | ||
mbedtls_sha256_init(&shaContext); | ||
|
||
// These errors would only occur if there's an issue with shaContext which is wrapped inside | ||
// SHA256, or with the mbedtls library itself | ||
if (mbedtls_sha256_starts(&shaContext, false)) { | ||
throw RuntimeException{"SHA256 Error"}; | ||
} | ||
} | ||
|
||
SHA256::~SHA256() { | ||
mbedtls_sha256_free(&shaContext); | ||
} | ||
|
||
void SHA256::addString(const std::string& str) { | ||
if (mbedtls_sha256_update(&shaContext, reinterpret_cast<const unsigned char*>(str.data()), | ||
str.size())) { | ||
throw RuntimeException{"SHA256 Error"}; | ||
} | ||
} | ||
|
||
void SHA256::finishSHA256(char* out) { | ||
std::string hash; | ||
hash.resize(SHA256_HASH_LENGTH_BYTES); | ||
|
||
if (mbedtls_sha256_finish(&shaContext, reinterpret_cast<unsigned char*>(hash.data()))) { | ||
throw RuntimeException{"SHA256 Error"}; | ||
} | ||
|
||
toBase16(hash.c_str(), out, SHA256_HASH_LENGTH_BYTES); | ||
} | ||
|
||
void SHA256::toBase16(const char* in, char* out, size_t len) { | ||
static char const HEX_CODES[] = "0123456789abcdef"; | ||
size_t i, j; | ||
|
||
for (j = i = 0; i < len; i++) { | ||
int a = in[i]; | ||
out[j++] = HEX_CODES[(a >> 4) & 0xf]; | ||
out[j++] = HEX_CODES[a & 0xf]; | ||
} | ||
} | ||
|
||
} // namespace common | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "mbedtls/sha256.h" | ||
|
||
namespace kuzu { | ||
namespace common { | ||
|
||
class SHA256 { | ||
public: | ||
static constexpr size_t SHA256_HASH_LENGTH_BYTES = 32; | ||
static constexpr size_t SHA256_HASH_LENGTH_TEXT = 64; | ||
|
||
public: | ||
SHA256(); | ||
~SHA256(); | ||
void addString(const std::string& str); | ||
void finishSHA256(char* out); | ||
static void toBase16(const char* in, char* out, size_t len); | ||
|
||
private: | ||
typedef mbedtls_sha256_context SHA256Context; | ||
|
||
SHA256Context shaContext; | ||
}; | ||
|
||
} // namespace common | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "common/md5.h" | ||
#include "common/vector/value_vector.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace function { | ||
|
||
struct MD5Operator { | ||
static void operation(ku_string_t& operand, ku_string_t& result, ValueVector& resultVector) { | ||
MD5 hasher; | ||
hasher.addToMD5(reinterpret_cast<const char*>(operand.getData())); | ||
StringVector::addString(&resultVector, result, std::string(hasher.finishMD5())); | ||
} | ||
}; | ||
|
||
} // namespace function | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "common/sha256.h" | ||
#include "common/vector/value_vector.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace function { | ||
|
||
struct SHA256Operator { | ||
static void operation(ku_string_t& operand, ku_string_t& result, ValueVector& resultVector) { | ||
StringVector::reserveString(&resultVector, result, SHA256::SHA256_HASH_LENGTH_TEXT); | ||
SHA256 hasher; | ||
hasher.addString(operand.getAsString()); | ||
hasher.finishSHA256(reinterpret_cast<char*>(result.getDataUnsafe())); | ||
} | ||
}; | ||
|
||
} // namespace function | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.