hash.h (2912B)
1 /* 2 * Unified Hash API 3 * Simple interface for SHA-256, SHA-512, SHA-1, MD5, and CRC32 4 */ 5 6 #ifndef HASH_H 7 #define HASH_H 8 9 #include <stdint.h> 10 #include <stdlib.h> 11 #include <string.h> 12 13 // Hash algorithm types 14 typedef enum { 15 HASH_SHA256, 16 HASH_SHA512, 17 HASH_SHA1, 18 HASH_MD5, 19 HASH_CRC32 20 } HashAlgorithm; 21 22 // Hash result structure 23 typedef struct { 24 uint8_t *digest; 25 size_t digest_len; 26 HashAlgorithm algorithm; 27 } HashResult; 28 29 // Get digest length for algorithm 30 static inline size_t hash_digest_length(HashAlgorithm algo) { 31 switch (algo) { 32 case HASH_SHA256: return 32; 33 case HASH_SHA512: return 64; 34 case HASH_SHA1: return 20; 35 case HASH_MD5: return 16; 36 case HASH_CRC32: return 4; 37 default: return 0; 38 } 39 } 40 41 // Get algorithm name 42 static inline const char* hash_algorithm_name(HashAlgorithm algo) { 43 switch (algo) { 44 case HASH_SHA256: return "SHA-256"; 45 case HASH_SHA512: return "SHA-512"; 46 case HASH_SHA1: return "SHA-1"; 47 case HASH_MD5: return "MD5"; 48 case HASH_CRC32: return "CRC32"; 49 default: return "Unknown"; 50 } 51 } 52 53 // One-shot hash computation 54 HashResult hash_compute(HashAlgorithm algo, const uint8_t *data, size_t len); 55 56 // Free hash result 57 void hash_free(HashResult *result); 58 59 // Print hash in hex format 60 void hash_print(const char *label, const HashResult *result); 61 62 // Individual hash functions (from separate files) 63 void sha256(const uint8_t *data, size_t len, uint8_t digest[32]); 64 void sha512(const uint8_t *data, size_t len, uint8_t digest[64]); 65 void sha1(const uint8_t *data, size_t len, uint8_t digest[20]); 66 void md5(const uint8_t *data, size_t len, uint8_t digest[16]); 67 uint32_t crc32(const uint8_t *data, size_t len); 68 69 // Convenience macro for hashing strings 70 #define HASH(algo, str) hash_compute(algo, (const uint8_t*)(str), strlen(str)) 71 72 // Convenience functions for string hashing 73 static inline HashResult hash_sha256_str(const char *str) { 74 return hash_compute(HASH_SHA256, (const uint8_t*)str, strlen(str)); 75 } 76 77 static inline HashResult hash_sha512_str(const char *str) { 78 return hash_compute(HASH_SHA512, (const uint8_t*)str, strlen(str)); 79 } 80 81 static inline HashResult hash_sha1_str(const char *str) { 82 return hash_compute(HASH_SHA1, (const uint8_t*)str, strlen(str)); 83 } 84 85 static inline HashResult hash_md5_str(const char *str) { 86 return hash_compute(HASH_MD5, (const uint8_t*)str, strlen(str)); 87 } 88 89 static inline HashResult hash_crc32_str(const char *str) { 90 return hash_compute(HASH_CRC32, (const uint8_t*)str, strlen(str)); 91 } 92 93 // Compare two hash results 94 static inline int hash_equals(const HashResult *a, const HashResult *b) { 95 if (a->digest_len != b->digest_len) return 0; 96 return memcmp(a->digest, b->digest, a->digest_len) == 0; 97 } 98 99 // Convert hash to hex string (caller must free) 100 char* hash_to_hex(const HashResult *result); 101 102 #endif // HASH_H