mirror of
https://github.com/apache/arrow.git
synced 2026-10-28 16:47:19 +08:00
Implements the function hash_using_SHA128
This commit is contained in:
1 parent
3466df0b60
commit
af05c21171
2 files changed
+48
-6
No files matched your search
@@ -166,10 +166,12 @@ TEST(TestGdvFnStubs, TestSha256Numeric) {
|
||||
int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx);
|
||||
|
||||
// test zero boundaries
|
||||
const char *zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, 0.0);
|
||||
const char *double_zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, 0.0);
|
||||
const char *zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, 0);
|
||||
const char *zero_one_hash = gdv_fn_sha256_from_numeric(ctx_ptr, 0.1);
|
||||
|
||||
EXPECT_STRNE(zero_hash, zero_one_hash);
|
||||
EXPECT_STREQ(double_zero_hash,zero_hash);
|
||||
|
||||
// tests double limits
|
||||
const char *double_positive_limit = gdv_fn_sha256_from_numeric(ctx_ptr, 1.7976931348623158e+308);
|
||||
@@ -178,10 +180,12 @@ TEST(TestGdvFnStubs, TestSha256Numeric) {
|
||||
|
||||
EXPECT_STREQ(expected_hash, double_positive_limit);
|
||||
|
||||
// tests minus zero case
|
||||
const char *minus_zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, -0.0);
|
||||
// tests minus zero cases
|
||||
const char *double_minus_zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, -0.0);
|
||||
const char *minus_zero_hash = gdv_fn_sha256_from_numeric(ctx_ptr, -0);
|
||||
|
||||
EXPECT_STREQ(zero_hash, minus_zero_hash);
|
||||
EXPECT_STREQ(zero_hash, double_minus_zero_hash);
|
||||
|
||||
ctx.Reset();
|
||||
}
|
||||
@@ -235,7 +239,7 @@ TEST(TestGdvFnStubs, TestSha128Numeric) {
|
||||
// tests double limits
|
||||
const char *double_positive_limit = gdv_fn_sha128_from_numeric(ctx_ptr, 1.7976931348623158e+308);
|
||||
|
||||
const char *expected_hash = "b57cccce96b74638b9f9b8a8e1ab45a534954412b6aedf5b329a85f31da6f305";
|
||||
const char *expected_hash = "f6efec8fc6498a89ea8ec4fa8a4a0800d57f2c2e";
|
||||
|
||||
EXPECT_STREQ(expected_hash, double_positive_limit);
|
||||
|
||||
@@ -259,7 +263,7 @@ TEST(TestGdvFnStubs, TestSha128String) {
|
||||
|
||||
const char *first_sha_hash = gdv_fn_hash_sha128_from_string(ctx_ptr, test_string, strlen(test_string));
|
||||
|
||||
const char *expected_first_hash ="55aeb2e789871dbd289edae94d4c1c82a1c25ca0bcd5a873924da2fefdd57acb";
|
||||
const char *expected_first_hash ="160fcdbc2fa694d884868f5fae7a4bae82706185";
|
||||
|
||||
EXPECT_STRNE(first_sha_hash, zero_hash);
|
||||
EXPECT_STREQ(expected_first_hash, first_sha_hash);
|
||||
@@ -270,7 +274,7 @@ TEST(TestGdvFnStubs, TestSha128String) {
|
||||
const char
|
||||
*second_sha_hash = gdv_fn_hash_sha128_from_string(ctx_ptr, test_string_modified, strlen(test_string_modified));
|
||||
|
||||
const char *expected_second_hash ="86b29c13d0d0e26ea8f85bfa649dc9b8622ae59a4da2409d7d9b463e86e796f2";
|
||||
const char *expected_second_hash ="a456b3e0f88669d2482170a42fade226a815bee1";
|
||||
|
||||
EXPECT_STREQ(expected_second_hash,second_sha_hash);
|
||||
EXPECT_STRNE(second_sha_hash, first_sha_hash);
|
||||
|
||||
@@ -47,7 +47,45 @@ namespace gandiva {
|
||||
return result_buffer;
|
||||
}
|
||||
const char * HashUtils::hash_using_SHA128(int64_t context, const void *message, size_t message_length) {
|
||||
if(message == nullptr){
|
||||
HashUtils::error_message(context, "A null value was given to be hashed.");
|
||||
return "";
|
||||
}
|
||||
|
||||
EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
|
||||
|
||||
EVP_DigestInit_ex(md_ctx, EVP_sha1(), nullptr);
|
||||
|
||||
EVP_DigestUpdate(md_ctx, message, message_length);
|
||||
|
||||
int sha128_hash_size = EVP_MD_size(EVP_sha1());
|
||||
|
||||
auto* result = static_cast<unsigned char*>(OPENSSL_malloc(sha128_hash_size));
|
||||
|
||||
unsigned int result_length;
|
||||
|
||||
EVP_DigestFinal_ex(md_ctx, result, &result_length);
|
||||
|
||||
char* hex_buffer = new char[4];
|
||||
char* result_buffer = new char[65];
|
||||
|
||||
clean_char_array(hex_buffer);
|
||||
clean_char_array(result_buffer);
|
||||
|
||||
for (unsigned int j = 0; j < result_length; j++) {
|
||||
unsigned char hex_number = result[j];
|
||||
sprintf(hex_buffer, "%02x", hex_number);
|
||||
|
||||
strcat(result_buffer, hex_buffer);
|
||||
}
|
||||
|
||||
result_buffer[64] = '\0';
|
||||
|
||||
// free the resources to avoid memory leaks
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
delete[] hex_buffer;
|
||||
free(result);
|
||||
|
||||
return result_buffer;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user