2 * Copyright (C) 2005 Jan Hutter
3 * Copyright (C) 2005-2006 Martin Willi
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * @defgroup hasher hasher
26 typedef enum hash_algorithm_t hash_algorithm_t
;
27 typedef struct hasher_t hasher_t
;
32 * Algorithms to use for hashing.
34 enum hash_algorithm_t
{
35 /** not specified hash function */
37 /** preferred hash function, general purpose */
49 #define HASH_SIZE_MD2 16
50 #define HASH_SIZE_MD4 16
51 #define HASH_SIZE_MD5 16
52 #define HASH_SIZE_SHA1 20
53 #define HASH_SIZE_SHA224 28
54 #define HASH_SIZE_SHA256 32
55 #define HASH_SIZE_SHA384 48
56 #define HASH_SIZE_SHA512 64
59 * enum names for hash_algorithm_t.
61 extern enum_name_t
*hash_algorithm_names
;
64 * Generic interface for all hash functions.
68 * Hash data and write it in the buffer.
70 * If the parameter hash is NULL, no result is written back
71 * and more data can be appended to already hashed data.
72 * If not, the result is written back and the hasher is reset.
74 * The hash output parameter must hold at least
75 * hash_t.get_block_size() bytes.
77 * @param data data to hash
78 * @param hash pointer where the hash will be written
80 void (*get_hash
) (hasher_t
*this, chunk_t data
, u_int8_t
*hash
);
83 * Hash data and allocate space for the hash.
85 * If the parameter hash is NULL, no result is written back
86 * and more data can be appended to already hashed data.
87 * If not, the result is written back and the hasher is reset.
89 * @param data chunk with data to hash
90 * @param hash chunk which will hold allocated hash
92 void (*allocate_hash
) (hasher_t
*this, chunk_t data
, chunk_t
*hash
);
95 * Get the size of the resulting hash.
97 * @return hash size in bytes
99 size_t (*get_hash_size
) (hasher_t
*this);
102 * Resets the hashers state.
104 void (*reset
) (hasher_t
*this);
107 * Destroys a hasher object.
109 void (*destroy
) (hasher_t
*this);
113 * Conversion of ASN.1 OID to hash algorithm.
115 * @param oid ASN.1 OID
116 * @return hash algorithm, HASH_UNKNOWN if OID unsuported
118 hash_algorithm_t
hasher_algorithm_from_oid(int oid
);
121 * Conversion of hash algorithm into ASN.1 OID.
123 * @param alg hash algorithm
124 * @return ASN.1 OID, or OID_UNKNOW
126 int hasher_algorithm_to_oid(hash_algorithm_t alg
);
129 * Conversion of hash signature algorithm into ASN.1 OID.
131 * @param alg hash algorithm
132 * @return ASN.1 OID if, or OID_UNKNOW
134 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg
);
136 #endif /** HASHER_H_ @}*/