1 /*********************************************************************************************************************************** 2 Cryptographic Hash 3 4 Generate a hash (sha1, md5, etc.) from a string, Buffer, or using an IoFilter. 5 ***********************************************************************************************************************************/ 6 #ifndef COMMON_CRYPTO_HASH_H 7 #define COMMON_CRYPTO_HASH_H 8 9 #include "common/io/filter/filter.h" 10 #include "common/type/string.h" 11 12 /*********************************************************************************************************************************** 13 Filter type constant 14 ***********************************************************************************************************************************/ 15 #define CRYPTO_HASH_FILTER_TYPE "hash" 16 STRING_DECLARE(CRYPTO_HASH_FILTER_TYPE_STR); 17 18 /*********************************************************************************************************************************** 19 Hash types 20 ***********************************************************************************************************************************/ 21 #define HASH_TYPE_MD5 "md5" 22 STRING_DECLARE(HASH_TYPE_MD5_STR); 23 #define HASH_TYPE_SHA1 "sha1" 24 STRING_DECLARE(HASH_TYPE_SHA1_STR); 25 #define HASH_TYPE_SHA256 "sha256" 26 STRING_DECLARE(HASH_TYPE_SHA256_STR); 27 28 /*********************************************************************************************************************************** 29 Hashes for zero-length files (i.e., starting hash) 30 ***********************************************************************************************************************************/ 31 #define HASH_TYPE_MD5_ZERO "d41d8cd98f00b204e9800998ecf8427e" 32 #define HASH_TYPE_SHA1_ZERO "da39a3ee5e6b4b0d3255bfef95601890afd80709" 33 STRING_DECLARE(HASH_TYPE_SHA1_ZERO_STR); 34 #define HASH_TYPE_SHA256_ZERO \ 35 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" 36 STRING_DECLARE(HASH_TYPE_SHA256_ZERO_STR); 37 38 /*********************************************************************************************************************************** 39 Hash type sizes 40 ***********************************************************************************************************************************/ 41 #define HASH_TYPE_M5_SIZE 16 42 #define HASH_TYPE_MD5_SIZE_HEX (HASH_TYPE_M5_SIZE * 2) 43 44 #define HASH_TYPE_SHA1_SIZE 20 45 #define HASH_TYPE_SHA1_SIZE_HEX (HASH_TYPE_SHA1_SIZE * 2) 46 47 #define HASH_TYPE_SHA256_SIZE 32 48 #define HASH_TYPE_SHA256_SIZE_HEX (HASH_TYPE_SHA256_SIZE * 2) 49 50 /*********************************************************************************************************************************** 51 Constructors 52 ***********************************************************************************************************************************/ 53 IoFilter *cryptoHashNew(const String *type); 54 IoFilter *cryptoHashNewVar(const VariantList *paramList); 55 56 /*********************************************************************************************************************************** 57 Helper functions 58 ***********************************************************************************************************************************/ 59 // Get hash for one buffer 60 Buffer *cryptoHashOne(const String *type, const Buffer *message); 61 62 // Get hmac for one message/key 63 Buffer *cryptoHmacOne(const String *type, const Buffer *key, const Buffer *message); 64 65 #endif 66