]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/hash.txt
b28c42a76eb5448ba0b403851f368ad294bf8630
[libucw.git] / ucw / doc / hash.txt
1 Hashing routines
2 ================
3
4 Libucw contains two hash algorithms, MD5 (RFC 1321) and SHA1 (RFC
5 3174).
6
7 - <<md5,MD5>>
8 - <<sha1,SHA1>>
9 - <<usage,Usage>>
10
11 [[md5]]
12 MD5
13 ---
14 !!ucw/md5.h
15
16 [[sha1]]
17 SHA1
18 ----
19 !!ucw/sha1.h
20
21 [[usage]]
22 Usage
23 -----
24
25 There are two ways you can use the hashing routines.
26
27 - Single-shot interface. If you have an in-memory buffer of the whole
28   message you want to hash, you can use this.
29
30   char *message = "Hello world";
31   byte output[MD5_SIZE];
32   md5_hash_buffer(output, message, strlen(message));
33
34 - Multi-shot interface. If you have the message scattered in many
35   buffers or you get it by parts, you do not need to concatenate the
36   parts together.
37
38   byte buffer[MAX_BUFFER];
39   uns buffer_len;
40   md5_context c;
41   md5_init(&c);
42   while(buffer_len = get_chunk(buffer, MAX_BUFFER)) {
43     md5_update(&c, buffer, buffer_len);
44   }
45   byte output[MD5_SIZE];
46   memcpy(output, md5_final(&c), MD5_SIZE);
47
48 SHA1 has the same interface, so both ways work to it as well.
49
50 See also <<string:mem_to_hex()>>.