first commit

This commit is contained in:
lnk
2026-07-10 14:13:02 +08:00
commit b61388443f
459 changed files with 143602 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BASE64_H
#define BASE64_H
/* Get size_t. */
#include <stddef.h>
/* Get bool. */
#include <stdbool.h>
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
/* This uses that the expression (n+(k-1))/k means the smallest
integer >= n/k, i.e., the ceiling of n/k. */
#define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
extern bool isbase64(char ch);
extern void base64_encode(const char* in, size_t inlen, char* out, size_t outlen);
extern size_t base64_encode_alloc(const char* in, size_t inlen, char** out);
extern bool base64_decode(const char* in, size_t inlen, char* out, size_t* outlen);
extern bool base64_decode_alloc(const char* in, size_t inlen, char** out, size_t* outlen);
#ifdef __cplusplus
}
#endif
#endif /* BASE64_H */

View File

@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _HMAC_HMAC_H
#define _HMAC_HMAC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#ifndef SHA1_DIGEST_LEN
#define SHA1_DIGEST_LEN 20
#endif
#ifndef SHA256_DIGEST_LEN
#define SHA256_DIGEST_LEN 32
#endif
#ifndef SHA512_DIGEST_LEN
#define SHA512_DIGEST_LEN 64
#endif
/*
* hmac_sha1:
* hmac_sha256:
* hmac_sha512:
* Calculate Hashed Message Authentication Code with sha1/256/512 algorithm
* Caution: ret_buf should provide enough space for HMAC result.
*
* @key [in]: the secure-key string
* @key_len [in]: the length of secure-key
* @data [in]: data string could be calculated.
* @data_len [in]: the length of data. length is needed because strlen could not take effect.
* @ret_buf [out]: HMAC result stored in ret_buf.
*/
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
extern int hmac_sha1(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf);
extern int hmac_sha256(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf);
extern int hmac_sha512(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PARAM_LIST_H
#define PARAM_LIST_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
typedef struct _spas_param_node {
char* name;
char* value;
struct _spas_param_node* pnext;
} SPAS_PARAM_NODE;
typedef struct _spas_param_list {
SPAS_PARAM_NODE* phead;
unsigned int length; /* count of nodes */
unsigned int size; /* total size of string presentation */
} SPAS_PARAM_LIST;
extern SPAS_PARAM_LIST* create_param_list(void);
extern int add_param_to_list(SPAS_PARAM_LIST* list, const char* name, const char* value);
extern void free_param_list(SPAS_PARAM_LIST* list);
extern char* param_list_to_str(const SPAS_PARAM_LIST* list);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SHA1_H
#define SHA1_H 1
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
#define SHA1_DIGEST_SIZE 20
/* Structure to save state of computation between the single steps. */
struct sha1_ctx {
uint32_t A;
uint32_t B;
uint32_t C;
uint32_t D;
uint32_t E;
uint32_t total[2];
uint32_t buflen;
uint32_t buffer[32];
};
/* Initialize structure containing state of computation. */
extern void sha1_init_ctx(struct sha1_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is necessary that LEN is a multiple of 64!!! */
extern void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is NOT required that LEN is a multiple of 64. */
extern void sha1_process_bytes(const void* buffer, size_t len, struct sha1_ctx* ctx);
/* Process the remaining bytes in the buffer and put result from CTX
in first 20 bytes following RESBUF. The result is always in little
endian byte order, so that a byte-wise output yields to the wanted
ASCII representation of the message digest. */
extern void* sha1_finish_ctx(struct sha1_ctx* ctx, void* resbuf);
/* Put result from CTX in first 20 bytes following RESBUF. The result is
always in little endian byte order, so that a byte-wise output yields
to the wanted ASCII representation of the message digest. */
extern void* sha1_read_ctx(const struct sha1_ctx* ctx, void* resbuf);
/* Compute SHA1 message digest for bytes read from STREAM. The
resulting message digest number will be written into the 20 bytes
beginning at RESBLOCK. */
extern int sha1_stream(FILE* stream, void* resblock);
/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
digest. */
extern void* sha1_buffer(const char* buffer, size_t len, void* resblock);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SHA256_H
#define SHA256_H 1
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
/* Structure to save state of computation between the single steps. */
struct sha256_ctx {
uint32_t state[8];
uint32_t total[2];
size_t buflen;
uint32_t buffer[32];
};
enum { SHA224_DIGEST_SIZE = 28 };
enum { SHA256_DIGEST_SIZE = 32 };
/* Initialize structure containing state of computation. */
extern void sha256_init_ctx(struct sha256_ctx* ctx);
extern void sha224_init_ctx(struct sha256_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is necessary that LEN is a multiple of 64!!! */
extern void sha256_process_block(const void* buffer, size_t len, struct sha256_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is NOT required that LEN is a multiple of 64. */
extern void sha256_process_bytes(const void* buffer, size_t len, struct sha256_ctx* ctx);
/* Process the remaining bytes in the buffer and put result from CTX
in first 32 (28) bytes following RESBUF. The result is always in little
endian byte order, so that a byte-wise output yields to the wanted
ASCII representation of the message digest. */
extern void* sha256_finish_ctx(struct sha256_ctx* ctx, void* resbuf);
extern void* sha224_finish_ctx(struct sha256_ctx* ctx, void* resbuf);
/* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
always in little endian byte order, so that a byte-wise output yields
to the wanted ASCII representation of the message digest. */
extern void* sha256_read_ctx(const struct sha256_ctx* ctx, void* resbuf);
extern void* sha224_read_ctx(const struct sha256_ctx* ctx, void* resbuf);
/* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The
resulting message digest number will be written into the 32 (28) bytes
beginning at RESBLOCK. */
extern int sha256_stream(FILE* stream, void* resblock);
extern int sha224_stream(FILE* stream, void* resblock);
/* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
digest. */
extern void* sha256_buffer(const char* buffer, size_t len, void* resblock);
extern void* sha224_buffer(const char* buffer, size_t len, void* resblock);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SHA512_H
#define SHA512_H 1
#include <stdio.h>
#include "u64.h"
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
/* Structure to save state of computation between the single steps. */
struct sha512_ctx {
u64 state[8];
u64 total[2];
size_t buflen;
u64 buffer[32];
};
enum { SHA384_DIGEST_SIZE = 48 };
enum { SHA512_DIGEST_SIZE = 64 };
/* Initialize structure containing state of computation. */
extern void sha512_init_ctx(struct sha512_ctx* ctx);
extern void sha384_init_ctx(struct sha512_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is necessary that LEN is a multiple of 128!!! */
extern void sha512_process_block(const void* buffer, size_t len, struct sha512_ctx* ctx);
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is NOT required that LEN is a multiple of 128. */
extern void sha512_process_bytes(const void* buffer, size_t len, struct sha512_ctx* ctx);
/* Process the remaining bytes in the buffer and put result from CTX
in first 64 (48) bytes following RESBUF. The result is always in little
endian byte order, so that a byte-wise output yields to the wanted
ASCII representation of the message digest. */
extern void* sha512_finish_ctx(struct sha512_ctx* ctx, void* resbuf);
extern void* sha384_finish_ctx(struct sha512_ctx* ctx, void* resbuf);
/* Put result from CTX in first 64 (48) bytes following RESBUF. The result is
always in little endian byte order, so that a byte-wise output yields
to the wanted ASCII representation of the message digest.
IMPORTANT: On some systems it is required that RESBUF is correctly
aligned for a 32 bits value. */
extern void* sha512_read_ctx(const struct sha512_ctx* ctx, void* resbuf);
extern void* sha384_read_ctx(const struct sha512_ctx* ctx, void* resbuf);
/* Compute SHA512 (SHA384) message digest for bytes read from STREAM. The
resulting message digest number will be written into the 64 (48) bytes
beginning at RESBLOCK. */
extern int sha512_stream(FILE* stream, void* resblock);
extern int sha384_stream(FILE* stream, void* resblock);
/* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
digest. */
extern void* sha512_buffer(const char* buffer, size_t len, void* resblock);
extern void* sha384_buffer(const char* buffer, size_t len, void* resblock);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SPAS_CLIENT_H
#define SPAS_CLIENT_H
#include "param_list.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
namespace rocketmqSignature {
#endif
#define SPAS_MAX_KEY_LEN 128 /* max access_key/secret_key length */
#define SPAS_MAX_PATH 256 /* max credential file path length */
#define SPAS_ACCESS_KEY_TAG \
"accessKey" /* access_key tag in credential file \
*/
#define SPAS_SECRET_KEY_TAG \
"secretKey" /* secret_key tag in credential file \
*/
#define SPAS_CREDENTIAL_ENV "SPAS_CREDENTIAL" /* credential file environment variable */
typedef enum {
SIGN_HMACSHA1 = 0, /* HmacSHA1 */
SIGN_HMACSHA256 = 1, /* HmacSHA256 */
} SPAS_SIGN_ALGORITHM;
typedef enum {
NO_UPDATE = 0, /* do not update credential */
UPDATE_BY_ALARM = 1, /* update credential by SIGALRM */
#ifdef SPAS_MT
UPDATE_BY_THREAD = 2, /* update credential by standalone thread */
#endif
} CREDENTIAL_UPDATE_MODE;
typedef enum {
SPAS_NO_ERROR = 0, /* success */
ERROR_INVALID_PARAM = -1, /* invalid parameter */
ERROR_NO_CREDENTIAL = -2, /* credential file not specified */
ERROR_FILE_OPEN = -3, /* file open failed */
ERROR_MEM_ALLOC = -4, /* memory allocation failed */
ERROR_MISSING_KEY = -5, /* missing access_key/secret_key */
ERROR_KEY_LENGTH = -6, /* key length exceed limit */
ERROR_UPDATE_CREDENTIAL = -7 /* update credential file failed */
} SPAS_ERROR_CODE;
typedef struct _spas_credential {
char access_key[SPAS_MAX_KEY_LEN];
char secret_key[SPAS_MAX_KEY_LEN];
} SPAS_CREDENTIAL;
extern int spas_load_credential(char* path, CREDENTIAL_UPDATE_MODE mode);
extern int spas_set_access_key(char* key);
extern int spas_set_secret_key(char* key);
extern char* spas_get_access_key(void);
extern char* spas_get_secret_key(void);
extern SPAS_CREDENTIAL* spas_get_credential(void);
#ifdef SPAS_MT
extern int spas_load_thread_credential(char* path);
extern int spas_set_thread_access_key(char* key);
extern int spas_set_thread_secret_key(char* key);
extern char* spas_get_thread_access_key(void);
extern char* spas_get_thread_secret_key(void);
#endif
extern char* spas_get_signature(const SPAS_PARAM_LIST* list, const char* key);
extern char* spas_get_signature2(const SPAS_PARAM_LIST* list, const char* key, SPAS_SIGN_ALGORITHM algorithm);
extern char* spas_sign(const char* data, size_t size, const char* key);
extern char* spas_sign2(const char* data, size_t size, const char* key, SPAS_SIGN_ALGORITHM algorithm);
extern void spas_mem_free(char* pSignature);
extern char* spas_get_version(void);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stddef.h>
#include <stdint.h>
/* Return X rotated left by N bits, where 0 < N < 64. */
#define u64rol(x, n) u64or(u64shl(x, n), u64shr(x, 64 - n))
#ifdef UINT64_MAX
/* Native implementations are trivial. See below for comments on what
these operations do. */
typedef uint64_t u64;
#define u64hilo(hi, lo) ((u64)(((u64)(hi) << 32) + (lo)))
#define u64init(hi, lo) u64hilo(hi, lo)
#define u64lo(x) ((u64)(x))
#define u64lt(x, y) ((x) < (y))
#define u64and(x, y) ((x) & (y))
#define u64or(x, y) ((x) | (y))
#define u64xor(x, y) ((x) ^ (y))
#define u64plus(x, y) ((x) + (y))
#define u64shl(x, n) ((x) << (n))
#define u64shr(x, n) ((x) >> (n))
#else
/* u64 is a 64-bit unsigned integer value.
u64init (HI, LO), is like u64hilo (HI, LO), but for use in
initializer contexts. */
#ifdef WORDS_BIGENDIAN
typedef struct { uint32_t hi, lo; } u64;
#define u64init(hi, lo) \
{ hi, lo }
#else
typedef struct { uint32_t lo, hi; } u64;
#define u64init(hi, lo) \
{ lo, hi }
#endif
/* Given the high and low-order 32-bit quantities HI and LO, return a u64
value representing (HI << 32) + LO. */
static inline u64 u64hilo(uint32_t hi, uint32_t lo) {
u64 r;
r.hi = hi;
r.lo = lo;
return r;
}
/* Return a u64 value representing LO. */
static inline u64 u64lo(uint32_t lo) {
u64 r;
r.hi = 0;
r.lo = lo;
return r;
}
/* Return X < Y. */
static inline int u64lt(u64 x, u64 y) {
return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo);
}
/* Return X & Y. */
static inline u64 u64and(u64 x, u64 y) {
u64 r;
r.hi = x.hi & y.hi;
r.lo = x.lo & y.lo;
return r;
}
/* Return X | Y. */
static inline u64 u64or(u64 x, u64 y) {
u64 r;
r.hi = x.hi | y.hi;
r.lo = x.lo | y.lo;
return r;
}
/* Return X ^ Y. */
static inline u64 u64xor(u64 x, u64 y) {
u64 r;
r.hi = x.hi ^ y.hi;
r.lo = x.lo ^ y.lo;
return r;
}
/* Return X + Y. */
static inline u64 u64plus(u64 x, u64 y) {
u64 r;
r.lo = x.lo + y.lo;
r.hi = x.hi + y.hi + (r.lo < x.lo);
return r;
}
/* Return X << N. */
static inline u64 u64shl(u64 x, int n) {
u64 r;
if (n < 32) {
r.hi = (x.hi << n) | (x.lo >> (32 - n));
r.lo = x.lo << n;
} else {
r.hi = x.lo << (n - 32);
r.lo = 0;
}
return r;
}
/* Return X >> N. */
static inline u64 u64shr(u64 x, int n) {
u64 r;
if (n < 32) {
r.hi = x.hi >> n;
r.lo = (x.hi << (32 - n)) | (x.lo >> n);
} else {
r.hi = 0;
r.lo = x.hi >> (n - 32);
}
return r;
}
#endif