-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(mask): mask funcs #33479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3.0
Are you sure you want to change the base?
feat(mask): mask funcs #33479
Changes from all commits
8b3608b
36b1751
c154911
25876aa
096c37a
d6c20c2
1174581
21c59f5
ff235a7
9befeea
964deba
3ec33b2
34b81f8
ab5561a
6c8e3d5
0292444
c62f648
3be1841
3620dfb
fc79257
7967e6a
7c1a035
265c6c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #ifndef _TD_UTIL_SHA_H | ||
| #define _TD_UTIL_SHA_H | ||
|
|
||
| #include "os.h" | ||
|
|
||
| #define SHA1_OUTPUT_LEN 40 | ||
| #define SHA2_OUTPUT_LEN 128 | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct { | ||
| uint32_t state[5]; | ||
| uint32_t count[2]; | ||
| unsigned char buffer[64]; | ||
| } T_SHA1_CTX; | ||
|
|
||
| void tSHA1Transform(uint32_t state[5], const unsigned char buffer[64]); | ||
| void tSHA1Init(T_SHA1_CTX *context); | ||
| void tSHA1Update(T_SHA1_CTX *context, const unsigned char *data, uint32_t len); | ||
| void tSHA1Final(unsigned char digest[20], T_SHA1_CTX *context); | ||
| void tSHA1(char *hash_out, const char *str, uint32_t len); | ||
|
|
||
| #define SHA224_DIGEST_SIZE (224 / 8) | ||
| #define SHA256_DIGEST_SIZE (256 / 8) | ||
| #define SHA384_DIGEST_SIZE (384 / 8) | ||
| #define SHA512_DIGEST_SIZE (512 / 8) | ||
|
|
||
| #define SHA256_BLOCK_SIZE (512 / 8) | ||
| #define SHA512_BLOCK_SIZE (1024 / 8) | ||
| #define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE | ||
| #define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE | ||
|
|
||
| #ifndef SHA2_TYPES | ||
| #define SHA2_TYPES | ||
| typedef unsigned char uint8; | ||
| typedef unsigned int uint32; | ||
| typedef unsigned long long uint64; | ||
| #endif | ||
|
|
||
| typedef struct { | ||
| uint64 tot_len; | ||
| uint64 len; | ||
| uint8 block[2 * SHA256_BLOCK_SIZE]; | ||
| uint32 h[8]; | ||
| } sha256_ctx; | ||
|
|
||
| typedef struct { | ||
| uint64 tot_len; | ||
| uint64 len; | ||
| uint8 block[2 * SHA512_BLOCK_SIZE]; | ||
| uint64 h[8]; | ||
| } sha512_ctx; | ||
|
|
||
| typedef sha512_ctx sha384_ctx; | ||
| typedef sha256_ctx sha224_ctx; | ||
|
|
||
| void sha224_init(sha224_ctx *ctx); | ||
| void sha224_update(sha224_ctx *ctx, const uint8 *message, uint64 len); | ||
| void sha224_final(sha224_ctx *ctx, uint8 *digest); | ||
| void sha224(const uint8 *message, uint64 len, uint8 *digest); | ||
|
|
||
| void sha256_init(sha256_ctx *ctx); | ||
| void sha256_update(sha256_ctx *ctx, const uint8 *message, uint64 len); | ||
| void sha256_final(sha256_ctx *ctx, uint8 *digest); | ||
| void sha256(const uint8 *message, uint64 len, uint8 *digest); | ||
|
|
||
| void sha384_init(sha384_ctx *ctx); | ||
| void sha384_update(sha384_ctx *ctx, const uint8 *message, uint64 len); | ||
| void sha384_final(sha384_ctx *ctx, uint8 *digest); | ||
| void sha384(const uint8 *message, uint64 len, uint8 *digest); | ||
|
|
||
| void sha512_init(sha512_ctx *ctx); | ||
| void sha512_update(sha512_ctx *ctx, const uint8 *message, uint64 len); | ||
| void sha512_final(sha512_ctx *ctx, uint8 *digest); | ||
| void sha512(const uint8 *message, uint64 len, uint8 *digest); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /*_TD_UTIL_SHA_H*/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| #include "tdef.h" | ||
| #include "thash.h" | ||
| #include "tmd5.h" | ||
| #include "tsha.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
|
|
@@ -215,17 +216,82 @@ static FORCE_INLINE int32_t taosHashBinary(char *pBuf, int32_t len) { | |
|
|
||
| static FORCE_INLINE int32_t taosCreateMD5Hash(char *pBuf, int32_t len) { | ||
| T_MD5_CTX ctx; | ||
|
|
||
| tMD5Init(&ctx); | ||
| tMD5Update(&ctx, (uint8_t *)pBuf, len); | ||
| tMD5Final(&ctx); | ||
| char *p = pBuf; | ||
| int32_t resLen = 0; | ||
|
|
||
| return sprintf(pBuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", ctx.digest[0], ctx.digest[1], | ||
| ctx.digest[2], ctx.digest[3], ctx.digest[4], ctx.digest[5], ctx.digest[6], ctx.digest[7], | ||
| ctx.digest[8], ctx.digest[9], ctx.digest[10], ctx.digest[11], ctx.digest[12], ctx.digest[13], | ||
| ctx.digest[14], ctx.digest[15]); | ||
| } | ||
|
|
||
| static FORCE_INLINE int32_t taosCreateSHA1Hash(char *pBuf, int32_t len) { | ||
| uint8_t result[21] = {0}; | ||
|
|
||
| tSHA1((char *)result, pBuf, len); | ||
|
|
||
| return sprintf(pBuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], | ||
| result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], | ||
| result[10], result[11], result[12], result[13], result[14], result[15], result[16], result[17], | ||
| result[18], result[19]); | ||
| } | ||
|
|
||
| static FORCE_INLINE int32_t taosCreateSHA2Hash(char *pBuf, int32_t len, uint32_t digestSize) { | ||
| uint8_t result[2 * SHA512_DIGEST_SIZE + 1] = {0}; | ||
|
|
||
| switch (digestSize / 8) { | ||
| case SHA224_DIGEST_SIZE: | ||
| sha224((const uint8_t *)pBuf, len, result); | ||
| return sprintf(pBuf, | ||
| "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%" | ||
| "02x%02x%02x%02x", | ||
| result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], | ||
| result[9], result[10], result[11], result[12], result[13], result[14], result[15], result[16], | ||
| result[17], result[18], result[19], result[20], result[21], result[22], result[23], result[24], | ||
| result[25], result[26], result[27]); | ||
| case SHA256_DIGEST_SIZE: | ||
| sha256((const uint8_t *)pBuf, len, result); | ||
| return sprintf(pBuf, | ||
| "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%" | ||
| "02x%02x%02x%02x%02x%02x%02x%02x", | ||
| result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], | ||
| result[9], result[10], result[11], result[12], result[13], result[14], result[15], result[16], | ||
| result[17], result[18], result[19], result[20], result[21], result[22], result[23], result[24], | ||
| result[25], result[26], result[27], result[28], result[29], result[30], result[31]); | ||
| case SHA384_DIGEST_SIZE: | ||
| sha384((const uint8_t *)pBuf, len, result); | ||
| return sprintf(pBuf, | ||
| "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%" | ||
| "02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", | ||
| result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], | ||
| result[9], result[10], result[11], result[12], result[13], result[14], result[15], result[16], | ||
| result[17], result[18], result[19], result[20], result[21], result[22], result[23], result[24], | ||
| result[25], result[26], result[27], result[28], result[29], result[30], result[31], result[32], | ||
| result[33], result[34], result[35], result[36], result[37], result[38], result[39], result[40], | ||
| result[41], result[42], result[43], result[44], result[45], result[46], result[47]); | ||
| case SHA512_DIGEST_SIZE: | ||
| sha512((const uint8_t *)pBuf, len, result); | ||
| return sprintf(pBuf, | ||
| "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%" | ||
| "02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%" | ||
| "02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", | ||
| result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], | ||
| result[9], result[10], result[11], result[12], result[13], result[14], result[15], result[16], | ||
| result[17], result[18], result[19], result[20], result[21], result[22], result[23], result[24], | ||
| result[25], result[26], result[27], result[28], result[29], result[30], result[31], result[32], | ||
| result[33], result[34], result[35], result[36], result[37], result[38], result[39], result[40], | ||
| result[41], result[42], result[43], result[44], result[45], result[46], result[47], result[48], | ||
| result[49], result[50], result[51], result[52], result[53], result[54], result[55], result[56], | ||
| result[57], result[58], result[59], result[60], result[61], result[62], result[63]); | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
Comment on lines
+241
to
+293
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The functions |
||
|
|
||
| static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, int32_t method, int32_t prefix, | ||
| int32_t suffix) { | ||
| if ((prefix == 0 && suffix == 0) || (tblen <= (prefix + suffix)) || (tblen <= -1 * (prefix + suffix)) || | ||
|
|
@@ -301,7 +367,7 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, | |
|
|
||
| #define VND_CHECK_CODE(CODE, LINO, LABEL) TSDB_CHECK_CODE(CODE, LINO, LABEL) | ||
|
|
||
| #define TCONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) | ||
| #define TCONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member))) | ||
|
|
||
| #define TAOS_GET_TERRNO(code) (terrno == 0 ? code : terrno) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom integer type definitions
uint8,uint32, anduint64are inconsistent with the rest of the codebase, which appears to use standardstdint.htypes likeuint32_t(as seen inT_SHA1_CTX). Usingunsigned intforuint32is not portable as it's not guaranteed to be 32 bits on all platforms. Sinceos.his already included and it provides<stdint.h>, it's better to use the standard fixed-width integer types (uint8_t,uint32_t,uint64_t) for portability and consistency.