8b4699a3e318f465603a1bfe18372b2e7eb1b15a
[strongswan.git] / Source / charon / transforms / hashers / hasher.h
1 /**
2 * @file hasher.h
3 *
4 * @brief Generic interface for hash functions
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef HASHER_H_
24 #define HASHER_H_
25
26
27 #include <types.h>
28
29 typedef enum hash_algorithm_t hash_algorithm_t;
30
31 /**
32 * algorithms to use for hashing
33 */
34 enum hash_algorithm_t {
35 HASH_SHA1,
36 HASH_MD5
37 };
38
39
40 typedef struct hasher_t hasher_t;
41
42 /**
43 * Object representing a hasher
44 */
45 struct hasher_t {
46 /**
47 * @brief hash data and write it in the buffer
48 *
49 * If the parameter hash is NULL, no result is written back
50 * an more data can be appended to already hashed data.
51 * If not, the result is written back and the hasher is reset.
52 *
53 * @warning: the hash output parameter must hold at least
54 * #hash_t.get_block_size bytes.
55 *
56 * @param this calling hasher
57 * @param data data to hash
58 * @param [out]buffer pointer where the hash will be written
59 * @return
60 * - SUCCESS in any case
61 */
62 status_t (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
63
64 /**
65 * @brief hash data and allocate space for the hash
66 *
67 * If the parameter hash is NULL, no result is written back
68 * an more data can be appended to already hashed data.
69 * If not, the result is written back and the hasher is reset.
70 *
71 * @param this calling hasher
72 * @param data chunk with data to hash
73 * @param [out]hash chunk which will hold allocated hash
74 * @return
75 * - SUCCESS in any case
76 * - OUT_OF_RES if space could not be allocated
77 */
78 status_t (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
79
80 /**
81 * @brief get the block size of this hashing function
82 *
83 * @param this calling hasher
84 * @return block size in bytes
85 */
86 size_t (*get_block_size) (hasher_t *this);
87
88 /**
89 * @brief reset the hashers state, which allows
90 * computation of a completly new hash.
91 *
92 * @param this calling hasher
93 * @return - SUCCESS in any case
94 */
95 status_t (*reset) (hasher_t *this);
96
97 /**
98 * @brief Destroys a hasher object.
99 *
100 * @param this hasher_t object to destroy
101 * @return
102 * SUCCESS in any case
103 */
104 status_t (*destroy) (hasher_t *this);
105 };
106
107 /**
108 * Creates a new hasher_t object
109 *
110 * @param hash_algorithm Algorithm to use for hashing
111 * @return
112 * - hasher_t if successfully
113 * - NULL if out of ressources
114 */
115 hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
116
117 #endif /*HASHER_H_*/