Skip to content

Commit 4548495

Browse files
committed
Extract some code to hash_functions.cpp/hpp
1 parent 3251e7c commit 4548495

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ build_static_extension(
1717
extension/httpfs/httpfs_client.cpp
1818
extension/httpfs/http_state.cpp
1919
extension/httpfs/crypto.cpp
20+
extension/httpfs/hash_functions.cpp
2021
extension/httpfs/create_secret_functions.cpp
2122
extension/httpfs/httpfs_extension.cpp)
2223

@@ -30,6 +31,7 @@ build_loadable_extension(
3031
extension/httpfs/httpfs_client.cpp
3132
extension/httpfs/http_state.cpp
3233
extension/httpfs/crypto.cpp
34+
extension/httpfs/hash_functions.cpp
3335
extension/httpfs/create_secret_functions.cpp
3436
extension/httpfs/httpfs_extension.cpp)
3537

extension/httpfs/crypto.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "crypto.hpp"
2+
#include "hash_functions.hpp"
23
#include "mbedtls_wrapper.hpp"
34
#include <iostream>
45
#include "duckdb/common/common.hpp"
@@ -9,28 +10,6 @@
910

1011
namespace duckdb {
1112

12-
void sha256(const char *in, size_t in_len, hash_bytes &out) {
13-
duckdb_mbedtls::MbedTlsWrapper::ComputeSha256Hash(in, in_len, (char *)out);
14-
}
15-
16-
void hmac256(const std::string &message, const char *secret, size_t secret_len, hash_bytes &out) {
17-
duckdb_mbedtls::MbedTlsWrapper::Hmac256(secret, secret_len, message.data(), message.size(), (char *)out);
18-
}
19-
20-
void hmac256(std::string message, hash_bytes secret, hash_bytes &out) {
21-
hmac256(message, (char *)secret, sizeof(hash_bytes), out);
22-
}
23-
24-
void hex256(hash_bytes &in, hash_str &out) {
25-
const char *hex = "0123456789abcdef";
26-
unsigned char *pin = in;
27-
unsigned char *pout = out;
28-
for (; pin < in + sizeof(in); pout += 2, pin++) {
29-
pout[0] = hex[(*pin >> 4) & 0xF];
30-
pout[1] = hex[*pin & 0xF];
31-
}
32-
}
33-
3413
AESStateSSL::AESStateSSL(const std::string *key) : context(EVP_CIPHER_CTX_new()) {
3514
if (!(context)) {
3615
throw InternalException("AES GCM failed with initializing context");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "mbedtls_wrapper.hpp"
2+
#include "hash_functions.hpp"
3+
4+
namespace duckdb {
5+
6+
void sha256(const char *in, size_t in_len, hash_bytes &out) {
7+
duckdb_mbedtls::MbedTlsWrapper::ComputeSha256Hash(in, in_len, (char *)out);
8+
}
9+
10+
void hmac256(const std::string &message, const char *secret, size_t secret_len, hash_bytes &out) {
11+
duckdb_mbedtls::MbedTlsWrapper::Hmac256(secret, secret_len, message.data(), message.size(), (char *)out);
12+
}
13+
14+
void hmac256(std::string message, hash_bytes secret, hash_bytes &out) {
15+
hmac256(message, (char *)secret, sizeof(hash_bytes), out);
16+
}
17+
18+
void hex256(hash_bytes &in, hash_str &out) {
19+
const char *hex = "0123456789abcdef";
20+
unsigned char *pin = in;
21+
unsigned char *pout = out;
22+
for (; pin < in + sizeof(in); pout += 2, pin++) {
23+
pout[0] = hex[(*pin >> 4) & 0xF];
24+
pout[1] = hex[*pin & 0xF];
25+
}
26+
}
27+
28+
} // namespace duckdb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "duckdb/common/helper.hpp"
4+
5+
namespace duckdb {
6+
7+
typedef unsigned char hash_bytes[32];
8+
typedef unsigned char hash_str[64];
9+
10+
void sha256(const char *in, size_t in_len, hash_bytes &out);
11+
12+
void hmac256(const std::string &message, const char *secret, size_t secret_len, hash_bytes &out);
13+
14+
void hmac256(std::string message, hash_bytes secret, hash_bytes &out);
15+
16+
void hex256(hash_bytes &in, hash_str &out);
17+
18+
} // namespace duckdb

0 commit comments

Comments
 (0)