74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen#ifndef HASH_METHOD_H
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen#define HASH_METHOD_H
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi#include "buffer.h"
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenstruct hash_method {
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen const char *name;
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen /* Number of bytes that must be allocated for context */
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen unsigned int context_size;
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen /* Number of bytes that must be allocated for result()'s digest */
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen unsigned int digest_size;
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen void (*init)(void *context);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen void (*loop)(void *context, const void *data, size_t size);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen void (*result)(void *context, unsigned char *digest_r);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen};
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenconst struct hash_method *hash_method_lookup(const char *name);
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen/* NULL-terminated list of all hash methods */
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainenextern const struct hash_method *hash_methods[];
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi/** Simple helpers for digesting (hashing)
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi * USAGE:
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi buffer_t *result = t_hash_str(hash_method_lookup("sha256"), "hello world");
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi const char *hex = binary_to_hex(result->data, result->used);
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi*/
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomibuffer_t *t_hash_data(const struct hash_method *meth,
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi const void *data, size_t data_len);
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomistatic inline
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomibuffer_t *t_hash_buffer(const struct hash_method *meth,
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi const buffer_t *data)
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi{
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi return t_hash_data(meth, data->data, data->used);
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi}
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomistatic inline
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomibuffer_t *t_hash_str(const struct hash_method *meth,
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi const char *data)
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi{
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi return t_hash_data(meth, data, strlen(data));
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi}
ba706bd508fdbe3e6c971769d0c913b32bf458ebAki Tuomi
74ae32512357bdd4872bf160dc697ff7b54b54c5Timo Sirainen#endif