1 #ifndef HASH_METHOD_H
2 #define HASH_METHOD_H
3 
4 #include "buffer.h"
5 
6 struct hash_method {
7 	const char *name;
8 	/* Block size for the algorithm */
9 	unsigned int block_size;
10 	/* Number of bytes that must be allocated for context */
11 	unsigned int context_size;
12 	/* Number of bytes that must be allocated for result()'s digest */
13 	unsigned int digest_size;
14 
15 	void (*init)(void *context);
16 	void (*loop)(void *context, const void *data, size_t size);
17 	void (*result)(void *context, unsigned char *digest_r);
18 };
19 
20 const struct hash_method *hash_method_lookup(const char *name);
21 
22 /* NULL-terminated list of all hash methods */
23 extern const struct hash_method *hash_methods[];
24 
25 void hash_method_get_digest(const struct hash_method *meth,
26 			    const void *data, size_t data_len,
27 			    unsigned char *result_r);
28 
29 /** Simple datastack helpers for digesting (hashing)
30 
31  * USAGE:
32 
33  buffer_t *result = t_hash_str(hash_method_lookup("sha256"), "hello world");
34  const char *hex = binary_to_hex(result->data, result->used);
35 
36 */
37 
38 buffer_t *t_hash_data(const struct hash_method *meth,
39 		      const void *data, size_t data_len);
40 
41 static inline
t_hash_buffer(const struct hash_method * meth,const buffer_t * data)42 buffer_t *t_hash_buffer(const struct hash_method *meth,
43 			const buffer_t *data)
44 {
45 	return t_hash_data(meth, data->data, data->used);
46 }
47 
48 static inline
t_hash_str(const struct hash_method * meth,const char * data)49 buffer_t *t_hash_str(const struct hash_method *meth,
50 		     const char *data)
51 {
52 	return t_hash_data(meth, data, strlen(data));
53 }
54 
55 #endif
56