1 /* 2 * checksum.h 3 * 4 * Copyright (c) 2008- Steve McIntyre <steve@einval.com> 5 * 6 * Definitions and prototypes for a generic checksum interface, used 7 * in JTE. Inspired heavily by the interface to the MD5 code we're 8 * using already. 9 * 10 * GNU GPL v2 11 */ 12 13 enum checksum_types 14 { 15 CHECK_MD5 = 0, 16 CHECK_SHA1, 17 CHECK_SHA256, 18 CHECK_SHA512, 19 NUM_CHECKSUMS 20 }; 21 22 #define CHECK_MD5_USED (1 << CHECK_MD5) 23 #define CHECK_SHA1_USED (1 << CHECK_SHA1) 24 #define CHECK_SHA256_USED (1 << CHECK_SHA256) 25 #define CHECK_SHA512_USED (1 << CHECK_SHA512) 26 #define CHECK_ALL_USED 0xFFFFFFFF 27 28 typedef void checksum_context_t; 29 30 struct checksum_info 31 { 32 char *name; 33 char *prog; 34 int digest_size; 35 }; 36 37 /* Ask the library for information about a particular checksum 38 * algorithm. Returns a pointer to internal memory - DO NOT 39 * MODIFY/FREE! */ 40 struct checksum_info *checksum_information(enum checksum_types which); 41 42 /* Allocate / initialise a context for the chosen checksums. OR 43 * together the desired checksums as the parameter */ 44 checksum_context_t *checksum_init_context(int checksums, const char *owner); 45 46 /* Cleanup and free a context when it's finished with */ 47 void checksum_free_context(checksum_context_t *context); 48 49 /* Pass a new buffer full of data through the checksum code */ 50 void checksum_update(checksum_context_t *context, 51 unsigned char const *buf, 52 unsigned int len); 53 54 /* Finish the current set of checksums */ 55 void checksum_final(checksum_context_t *context); 56 57 /* Extract a particular algorithm's checksum once checksum_final() has 58 * been called. Use the details in checksum_information() above first 59 * to see how big the digest will be. Only valid once checksum_final() 60 * has been called, otherwise the digest returned will be all 61 * zeroes. */ 62 void checksum_copy(checksum_context_t *context, 63 enum checksum_types which, 64 unsigned char *digest); 65 66 /* Helper function: return a pointer to a string containing the ASCII 67 * hexadecimal dump of a checksum. Only valid once checksum_final() 68 * has been called, otherwise will return NULL */ 69 const char * checksum_hex(checksum_context_t *context, 70 enum checksum_types which); 71 72 73 extern int parse_checksum_algo(char *arg, 74 int *algo); 75