]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/hash.txt
ucw docs: more hash functions
[libucw.git] / ucw / doc / hash.txt
1 Hashing routines
2 ================
3
4 Libucw contains two cryptographic hash algorithms: MD5 (RFC 1321) and SHA1 (RFC
5 3174). A SHA1-HMAC (RFC 2104) message authentication is available.
6
7 There are non-cryptographic hashes as well.
8
9 Cryptographic ones
10 ------------------
11
12 - <<md5,MD5>>
13 - <<sha1,SHA1>>
14 - <<sha1:sha1_hmac(),SHA1_HMAC>>
15 - <<usage,Common usage>>
16
17 Non-cryptographic ones
18 ----------------------
19
20 - <<strhash,String & block hashes>>
21 - <<inthash,Integer hashes>>
22
23 [[md5]]
24 MD5
25 ---
26 !!ucw/md5.h
27
28 [[sha1]]
29 SHA1
30 ----
31 !!ucw/sha1.h
32
33 [[usage]]
34 Common usage
35 ------------
36
37 There are two ways you can use the hashing routines.
38
39 - Single-shot interface. If you have an in-memory buffer of the whole
40   message you want to hash, you can use this.
41
42   char *message = "Hello world";
43   byte output[MD5_SIZE];
44   md5_hash_buffer(output, message, strlen(message));
45
46 - Multi-shot interface. If you have the message scattered in many
47   buffers or you get it by parts, you do not need to concatenate the
48   parts together.
49
50   byte buffer[MAX_BUFFER];
51   uns buffer_len;
52   md5_context c;
53   md5_init(&c);
54   while(buffer_len = get_chunk(buffer, MAX_BUFFER)) {
55     md5_update(&c, buffer, buffer_len);
56   }
57   byte output[MD5_SIZE];
58   memcpy(output, md5_final(&c), MD5_SIZE);
59
60 SHA1 has the same interface, so the same two ways apply.
61
62 See also <<string:mem_to_hex()>>.
63
64 !!ucw/hashfunc.h