1 /*********************************************************************
2 
3 	hash.h
4 
5 	Function to handle hash functions (checksums)
6 
7 *********************************************************************/
8 
9 #ifndef HASH_H
10 #define HASH_H
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define HASH_INFO_NO_DUMP	0
17 #define HASH_INFO_BAD_DUMP	1
18 
19 #define HASH_CRC    (1 << 0)
20 #define HASH_SHA1   (1 << 1)
21 #define HASH_MD5    (1 << 2)
22 
23 #define HASH_NUM_FUNCTIONS  3
24 #define md5byte unsigned char
25 
26 /* Standard size of a hash data buffer, all the manipulated buffers
27  * must respect this size
28  */
29 #define HASH_BUF_SIZE       256
30 
31 /* Get function name of the specified function */
32 const char* hash_function_name(unsigned int function);
33 
34 /* Check if const char* contains the checksum for a specific function */
35 int hash_data_has_checksum(const char* d, unsigned int function);
36 
37 /* Extract the binary or printable checksum of a specific function from a hash data. If the checksum information
38  * is not available, the functions return 0. If the pointer to the output buffer is NULL, the function will
39  * return the minimum size of the output buffer required to store the informations. Otherwise, the buffer
40  * will be filled and the function will return 1 as success code.
41  */
42 int hash_data_extract_binary_checksum(const char* d, unsigned int function, unsigned char* checksum);
43 int hash_data_extract_printable_checksum(const char* d, unsigned int function, char* checksum);
44 
45 /* Insert an already computed binary checksum inside a hash data. This is useful when we already have
46  * checksum informations (e.g, from archive headers) and we want to prepare a hash data to compare
47  * with another const char* (e.g. the expected checksums). Returns 0 in case of error, 1 if the checksum
48  * was added correctly, 2 if the checksum was added overwriting a previously existing checksum for the
49  * the same function
50  */
51 int hash_data_insert_binary_checksum(char* d, unsigned int function, unsigned char* checksum);
52 
53 /* Check if the hash data contains the requested info */
54 int hash_data_has_info(const char* d, unsigned int info);
55 
56 /* Compare two hash data to check if they are the same. 'functions' can be either a combination of the
57  * hash function bits (HASH_CRC, etc) or zero to ask to check for all the available checksums
58  */
59 int hash_data_is_equal(const char* d1, const char* d2, unsigned int functions);
60 
61 /* Print hash data informations in a standard format. 'functions' can be either a combination of the
62  * hash function bits (HASH_CRC, etc) or zero to ask to print all the available checksums
63  */
64 void hash_data_print(const char* d, unsigned int functions, char* buffer);
65 
66 /* Copy hash data informations */
67 void hash_data_copy(char* dst, const char* src);
68 
69 /* Clear hash data informations */
70 void hash_data_clear(char* dst);
71 
72 /* Check which functions we have a checksum of inside the data */
73 unsigned int hash_data_used_functions(const char* d);
74 
75 /* Compute hash of a data chunk in memory. Parameter 'functions' specifies which hashing functions
76  * we want the checksum of.
77  */
78 void hash_compute(char* dst, const unsigned char* data, unsigned long length, unsigned int functions);
79 
80 /* Verifies that a hash string is valid */
81 int hash_verify_string(const char *hash);
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif
88