]> mj.ucw.cz Git - libucw.git/blob - ucw/md5.h
ucw docs: Hash routines
[libucw.git] / ucw / md5.h
1 /*
2  *      UCW Library -- MD5 Message Digest
3  *
4  *      This file is in public domain (see ucw/md5.c).
5  */
6
7 #ifndef _UCW_MD5_H
8 #define _UCW_MD5_H
9
10 /**
11  * Internal MD5 hash state.
12  * You can use it just as a opaque handle.
13  */
14 typedef struct {
15         u32 buf[4];
16         u32 bits[2];
17         byte in[64];
18 } md5_context;
19
20 void md5_init(md5_context *context); /** Initialize the MD5 hashing algorithm in @context. **/
21 /**
22  * Push another @len bytes of data from @buf to the MD5 hash
23  * represented by @context. You can call it multiple time on the same
24  * @context without reinitializing it and the result will be the same
25  * as you concatenated all the data together and fed them here all at
26  * once.
27  */
28 void md5_update(md5_context *context, const byte *buf, uns len);
29 /**
30  * Call this after the last md5_update(). It will terminate the
31  * algorithm and return pointer to the result.
32  *
33  * Note that the data it points to are stored inside the @context, so
34  * if you use it to compute another hash or it ceases to exist, the
35  * pointer becomes invalid.
36  */
37 byte *md5_final(md5_context *context);
38
39 /**
40  * This is the core routine of MD5 algorithm. It takes 16 longwords of
41  * data in @in and transforms the hash in @buf according to them.
42  *
43  * You probably do not want to call this one directly.
44  */
45 void md5_transform(u32 buf[4], const u32 in[16]);
46
47 /**
48  * MD5 one-shot convenience method. It takes @length bytes from
49  * @buffer, creates the hash from them and returns it in @output.
50  *
51  * It is equivalent to this code:
52  *  md5_context c;
53  *  md5_init(&c);
54  *  md5_update(&c, buffer, length);
55  *  memcpy(outbuf, md5_final(&c), MD5_SIZE);
56  */
57 void md5_hash_buffer(byte *outbuf, const byte *buffer, uns length);
58
59 #define MD5_HEX_SIZE 33 /** How many bytes a string buffer for MD5 in hexadecimal format should have. **/
60 #define MD5_SIZE 16 /** Number of bytes the MD5 hash takes in the binary form. **/
61
62 #endif /* !_UCW_MD5_H */