rhash.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /** @file rhash.h LibRHash interface */
  2. #ifndef RHASH_H
  3. #define RHASH_H
  4. #include <stdio.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #ifndef RHASH_API
  9. /* modifier for LibRHash functions */
  10. # define RHASH_API
  11. #endif
  12. /**
  13. * Identifiers of supported hash functions.
  14. * The rhash_init() function allows mixing several ids using
  15. * binary OR, to calculate several hash functions for one message.
  16. */
  17. enum rhash_ids
  18. {
  19. #if 0
  20. RHASH_CRC32 = 0x01,
  21. RHASH_MD4 = 0x02,
  22. RHASH_MD5 = 0x04,
  23. RHASH_SHA1 = 0x08,
  24. RHASH_TIGER = 0x10,
  25. RHASH_TTH = 0x20,
  26. RHASH_BTIH = 0x40,
  27. RHASH_ED2K = 0x80,
  28. RHASH_AICH = 0x100,
  29. RHASH_WHIRLPOOL = 0x200,
  30. RHASH_RIPEMD160 = 0x400,
  31. RHASH_GOST = 0x800,
  32. RHASH_GOST_CRYPTOPRO = 0x1000,
  33. RHASH_HAS160 = 0x2000,
  34. RHASH_SNEFRU128 = 0x4000,
  35. RHASH_SNEFRU256 = 0x8000,
  36. RHASH_SHA224 = 0x10000,
  37. RHASH_SHA256 = 0x20000,
  38. RHASH_SHA384 = 0x40000,
  39. RHASH_SHA512 = 0x80000,
  40. RHASH_EDONR256 = 0x0100000,
  41. RHASH_EDONR512 = 0x0200000,
  42. RHASH_SHA3_224 = 0x0400000,
  43. RHASH_SHA3_256 = 0x0800000,
  44. RHASH_SHA3_384 = 0x1000000,
  45. RHASH_SHA3_512 = 0x2000000,
  46. /** The bit-mask containing all supported hashe functions */
  47. RHASH_ALL_HASHES = RHASH_CRC32 | RHASH_MD4 | RHASH_MD5 | RHASH_ED2K | RHASH_SHA1 |
  48. RHASH_TIGER | RHASH_TTH | RHASH_GOST | RHASH_GOST_CRYPTOPRO |
  49. RHASH_BTIH | RHASH_AICH | RHASH_WHIRLPOOL | RHASH_RIPEMD160 |
  50. RHASH_HAS160 | RHASH_SNEFRU128 | RHASH_SNEFRU256 |
  51. RHASH_SHA224 | RHASH_SHA256 | RHASH_SHA384 | RHASH_SHA512 |
  52. RHASH_SHA3_224 | RHASH_SHA3_256 | RHASH_SHA3_384 | RHASH_SHA3_512 |
  53. RHASH_EDONR256 | RHASH_EDONR512,
  54. /** The number of supported hash functions */
  55. RHASH_HASH_COUNT = 26
  56. #else
  57. RHASH_MD5 = 0x01,
  58. RHASH_SHA1 = 0x02,
  59. RHASH_SHA224 = 0x04,
  60. RHASH_SHA256 = 0x08,
  61. RHASH_SHA384 = 0x10,
  62. RHASH_SHA512 = 0x20,
  63. RHASH_SHA3_224 = 0x40,
  64. RHASH_SHA3_256 = 0x80,
  65. RHASH_SHA3_384 = 0x100,
  66. RHASH_SHA3_512 = 0x200,
  67. RHASH_ALL_HASHES =
  68. RHASH_MD5 |
  69. RHASH_SHA1 |
  70. RHASH_SHA224 |
  71. RHASH_SHA256 |
  72. RHASH_SHA384 |
  73. RHASH_SHA512 |
  74. RHASH_SHA3_224 |
  75. RHASH_SHA3_256 |
  76. RHASH_SHA3_384 |
  77. RHASH_SHA3_512,
  78. RHASH_HASH_COUNT = 10
  79. #endif
  80. };
  81. /**
  82. * The rhash context structure contains contexts for several hash functions
  83. */
  84. typedef struct rhash_context
  85. {
  86. /** The size of the hashed message */
  87. unsigned long long msg_size;
  88. /**
  89. * The bit-mask containing identifiers of the hashes being calculated
  90. */
  91. unsigned hash_id;
  92. } rhash_context;
  93. #ifndef LIBRHASH_RHASH_CTX_DEFINED
  94. #define LIBRHASH_RHASH_CTX_DEFINED
  95. /**
  96. * Hashing context.
  97. */
  98. typedef struct rhash_context* rhash;
  99. #endif /* LIBRHASH_RHASH_CTX_DEFINED */
  100. /** type of a callback to be called periodically while hashing a file */
  101. typedef void (*rhash_callback_t)(void* data, unsigned long long offset);
  102. RHASH_API void rhash_library_init(void); /* initialize static data */
  103. /* hi-level hashing functions */
  104. RHASH_API int rhash_msg(unsigned hash_id, const void* message, size_t length, unsigned char* result);
  105. RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char* result);
  106. RHASH_API int rhash_file_update(rhash ctx, FILE* fd);
  107. #ifdef _WIN32 /* windows only function */
  108. RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned char* result);
  109. #endif
  110. /* lo-level interface */
  111. RHASH_API rhash rhash_init(unsigned hash_id);
  112. /*RHASH_API rhash rhash_init_by_ids(unsigned hash_ids[], unsigned count);*/
  113. RHASH_API int rhash_update(rhash ctx, const void* message, size_t length);
  114. RHASH_API int rhash_final(rhash ctx, unsigned char* first_result);
  115. RHASH_API void rhash_reset(rhash ctx); /* reinitialize the context */
  116. RHASH_API void rhash_free(rhash ctx);
  117. /* additional lo-level functions */
  118. RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data);
  119. /** bit-flag: default hash output format is base32 */
  120. #define RHASH_INFO_BASE32 1
  121. /**
  122. * Information about a hash function.
  123. */
  124. typedef struct rhash_info
  125. {
  126. /** hash function indentifier */
  127. unsigned hash_id;
  128. /** flags bit-mask, including RHASH_INFO_BASE32 bit */
  129. unsigned flags;
  130. /** size of binary message digest in bytes */
  131. size_t digest_size;
  132. const char* name;
  133. const char* magnet_name;
  134. } rhash_info;
  135. /* information functions */
  136. RHASH_API int rhash_count(void); /* number of supported hashes */
  137. RHASH_API int rhash_get_digest_size(unsigned hash_id); /* size of binary message digest */
  138. RHASH_API int rhash_get_hash_length(unsigned hash_id); /* length of formatted hash string */
  139. RHASH_API int rhash_is_base32(unsigned hash_id); /* default digest output format */
  140. RHASH_API const char* rhash_get_name(unsigned hash_id); /* get hash function name */
  141. RHASH_API const char* rhash_get_magnet_name(unsigned hash_id); /* get name part of magnet urn */
  142. /* note, that rhash_info_by_id() is not exported to a shared library or DLL */
  143. const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */
  144. #if 0
  145. /**
  146. * Flags for printing a hash sum
  147. */
  148. enum rhash_print_sum_flags
  149. {
  150. /** print in a default format */
  151. RHPR_DEFAULT = 0x0,
  152. /** output as binary message digest */
  153. RHPR_RAW = 0x1,
  154. /** print as a hexadecimal string */
  155. RHPR_HEX = 0x2,
  156. /** print as a base32-encoded string */
  157. RHPR_BASE32 = 0x3,
  158. /** print as a base64-encoded string */
  159. RHPR_BASE64 = 0x4,
  160. /**
  161. * Print as an uppercase string. Can be used
  162. * for base32 or hexadecimal format only.
  163. */
  164. RHPR_UPPERCASE = 0x8,
  165. /**
  166. * Reverse hash bytes. Can be used for GOST hash.
  167. */
  168. RHPR_REVERSE = 0x10,
  169. /** don't print 'magnet:?' prefix in rhash_print_magnet */
  170. RHPR_NO_MAGNET = 0x20,
  171. /** print file size in rhash_print_magnet */
  172. RHPR_FILESIZE = 0x40,
  173. };
  174. #endif
  175. /* output hash into the given buffer */
  176. RHASH_API size_t rhash_print_bytes(char* output,
  177. const unsigned char* bytes, size_t size, int flags);
  178. RHASH_API size_t rhash_print(char* output, rhash ctx, unsigned hash_id,
  179. int flags);
  180. /* output magnet URL into the given buffer */
  181. RHASH_API size_t rhash_print_magnet(char* output, const char* filepath,
  182. rhash context, unsigned hash_mask, int flags);
  183. /* macros for message API */
  184. /** The type of an unsigned integer large enough to hold a pointer */
  185. #if defined(UINTPTR_MAX)
  186. typedef uintptr_t rhash_uptr_t;
  187. #elif defined(_LP64) || defined(__LP64__) || defined(__x86_64) || \
  188. defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
  189. typedef unsigned long long rhash_uptr_t;
  190. #else
  191. typedef unsigned long rhash_uptr_t;
  192. #endif
  193. /** The value returned by rhash_transmit on error */
  194. #define RHASH_ERROR ((rhash_uptr_t)-1)
  195. /** Convert a pointer to rhash_uptr_t */
  196. #define RHASH_STR2UPTR(str) ((rhash_uptr_t)(char*)(str))
  197. /** Convert a rhash_uptr_t to a void* pointer */
  198. #define RHASH_UPTR2PVOID(u) ((void*)((char*)0 + (u)))
  199. /* rhash API to set/get data via messages */
  200. RHASH_API rhash_uptr_t rhash_transmit(
  201. unsigned msg_id, void* dst, rhash_uptr_t ldata, rhash_uptr_t rdata);
  202. /* rhash message constants */
  203. #define RMSG_GET_CONTEXT 1
  204. #define RMSG_CANCEL 2
  205. #define RMSG_IS_CANCELED 3
  206. #define RMSG_GET_FINALIZED 4
  207. #define RMSG_SET_AUTOFINAL 5
  208. #define RMSG_SET_OPENSSL_MASK 10
  209. #define RMSG_GET_OPENSSL_MASK 11
  210. /* helper macros */
  211. /** Get a pointer to context of the specified hash function */
  212. #define rhash_get_context_ptr(ctx, hash_id) RHASH_UPTR2PVOID(rhash_transmit(RMSG_GET_CONTEXT, ctx, hash_id, 0))
  213. /** Cancel hash calculation of a file */
  214. #define rhash_cancel(ctx) rhash_transmit(RMSG_CANCEL, ctx, 0, 0)
  215. /** Return non-zero if hash calculation was canceled, zero otherwise */
  216. #define rhash_is_canceled(ctx) rhash_transmit(RMSG_IS_CANCELED, ctx, 0, 0)
  217. /** Return non-zero if rhash_final was called for rhash_context */
  218. #define rhash_get_finalized(ctx) rhash_transmit(RMSG_GET_FINALIZED, ctx, 0, 0)
  219. /**
  220. * Turn on/off the auto-final flag for the given rhash_context. By default
  221. * auto-final is on, which means rhash_final is called automatically, if
  222. * needed when a hash value is retrived by rhash_print call.
  223. */
  224. #define rhash_set_autofinal(ctx, on) rhash_transmit(RMSG_SET_AUTOFINAL, ctx, on, 0)
  225. /**
  226. * Set the bit-mask of hash algorithms to be calculated by OpenSSL library.
  227. * The call rhash_set_openssl_mask(0) made before rhash_library_init(),
  228. * turns off loading of the OpenSSL dynamic library.
  229. * This call works if the LibRHash was compiled with OpenSSL support.
  230. */
  231. #define rhash_set_openssl_mask(mask) rhash_transmit(RMSG_SET_OPENSSL_MASK, NULL, mask, 0)
  232. /**
  233. * Return current bit-mask of hash algorithms selected to be calculated
  234. * by OpenSSL library.
  235. */
  236. #define rhash_get_openssl_mask() rhash_transmit(RMSG_GET_OPENSSL_MASK, NULL, 0, 0)
  237. /** The bit mask of hash algorithms implemented by OpenSSL */
  238. #if defined(USE_OPENSSL) || defined(OPENSSL_RUNTIME)
  239. # define RHASH_OPENSSL_SUPPORTED_HASHES (RHASH_MD4 | RHASH_MD5 | \
  240. RHASH_SHA1 | RHASH_SHA224 | RHASH_SHA256 | RHASH_SHA384 | \
  241. RHASH_SHA512 | RHASH_RIPEMD160 | RHASH_WHIRLPOOL)
  242. #else
  243. # define RHASH_OPENSSL_SUPPORTED_HASHES 0
  244. #endif
  245. #ifdef __cplusplus
  246. } /* extern "C" */
  247. #endif /* __cplusplus */
  248. #endif /* RHASH_H */