68 行
2.8 KiB
C
68 行
2.8 KiB
C
#ifndef PAZE_HASH_H
|
|
#define PAZE_HASH_H
|
|
#include "paze/paze_types.h"
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* 通用哈希算法描述符,供 HMAC/HKDF/PBKDF2 等泛型使用 */
|
|
typedef struct paze_hash_alg {
|
|
const char *name;
|
|
size_t digest_len; /* 输出字节数 */
|
|
size_t block_len; /* 压缩块字节数 */
|
|
size_t ctx_size; /* 上下文字节数 */
|
|
void (*init)(void *ctx);
|
|
void (*update)(void *ctx, const uint8_t *data, size_t len);
|
|
void (*final)(void *ctx, uint8_t *out); /* out 长度 = digest_len */
|
|
} paze_hash_alg_t;
|
|
|
|
const paze_hash_alg_t *paze_hash_md5(void);
|
|
const paze_hash_alg_t *paze_hash_sha1(void);
|
|
const paze_hash_alg_t *paze_hash_sha256(void);
|
|
const paze_hash_alg_t *paze_hash_sha384(void);
|
|
const paze_hash_alg_t *paze_hash_sha512(void);
|
|
|
|
/* 便利单次接口 */
|
|
paze_status_t paze_sha1(const uint8_t *in, size_t n, uint8_t out[20]);
|
|
paze_status_t paze_sha256(const uint8_t *in, size_t n, uint8_t out[32]);
|
|
paze_status_t paze_sha384(const uint8_t *in, size_t n, uint8_t out[48]);
|
|
paze_status_t paze_sha512(const uint8_t *in, size_t n, uint8_t out[64]);
|
|
paze_status_t paze_md5(const uint8_t *in, size_t n, uint8_t out[16]);
|
|
|
|
/* 上下文结构体对外可见,便于栈上分配 */
|
|
typedef struct { uint32_t h[5]; uint64_t len; uint8_t buf[64]; size_t blen; } paze_sha1_ctx_t;
|
|
typedef struct { uint32_t h[8]; uint64_t len; uint8_t buf[64]; size_t blen; } paze_sha256_ctx_t;
|
|
typedef struct { uint64_t h[8]; uint64_t len_lo, len_hi; uint8_t buf[128]; size_t blen; } paze_sha512_ctx_t;
|
|
typedef paze_sha512_ctx_t paze_sha384_ctx_t; /* SHA-384 复用 SHA-512 上下文 */
|
|
typedef struct { uint32_t h[4]; uint64_t len; uint8_t buf[64]; size_t blen; } paze_md5_ctx_t;
|
|
|
|
/* 所有哈希上下文的最大字节数,供泛型代码栈上分配缓冲 */
|
|
#define PAZE_HASH_MAX_CTX 256
|
|
|
|
void paze_sha1_init(paze_sha1_ctx_t *c);
|
|
void paze_sha1_update(paze_sha1_ctx_t *c, const uint8_t *d, size_t n);
|
|
void paze_sha1_final(paze_sha1_ctx_t *c, uint8_t out[20]);
|
|
|
|
void paze_sha256_init(paze_sha256_ctx_t *c);
|
|
void paze_sha256_update(paze_sha256_ctx_t *c, const uint8_t *d, size_t n);
|
|
void paze_sha256_final(paze_sha256_ctx_t *c, uint8_t out[32]);
|
|
|
|
void paze_sha512_init(paze_sha512_ctx_t *c);
|
|
void paze_sha512_update(paze_sha512_ctx_t *c, const uint8_t *d, size_t n);
|
|
void paze_sha512_final(paze_sha512_ctx_t *c, uint8_t out[64]);
|
|
|
|
/* SHA-384: 与 SHA-512 相同核心,不同 IV,输出截断为 48 字节。
|
|
* update 直接复用 SHA-512(同 128 字节块、128 位计数器)。 */
|
|
void paze_sha384_init(paze_sha384_ctx_t *c);
|
|
#define paze_sha384_update paze_sha512_update
|
|
void paze_sha384_final(paze_sha384_ctx_t *c, uint8_t out[48]);
|
|
|
|
void paze_md5_init(paze_md5_ctx_t *c);
|
|
void paze_md5_update(paze_md5_ctx_t *c, const uint8_t *d, size_t n);
|
|
void paze_md5_final(paze_md5_ctx_t *c, uint8_t out[16]);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif
|