]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/hash.txt
short_opts[] should be chars, not bytes
[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 <<crypto,Cryptographic ones>>:
10
11 - <<md5,MD5>>
12 - <<sha1,SHA1>>
13 - <<sha1:sha1_hmac(),SHA1_HMAC>>
14 - <<usage,Common usage>>
15
16 <<nocrypto,Non-cryptographic ones>>:
17
18 - <<strhash,String & block hashes>>
19 - <<inthash,Integer hashes>>
20
21 [[crypto]]
22 Cryptographic hashes
23 --------------------
24
25 [[md5]]
26 MD5
27 ~~~
28 !!ucw/md5.h
29
30 [[sha1]]
31 SHA1
32 ~~~~
33 !!ucw/sha1.h
34
35 [[usage]]
36 Common usage
37 ~~~~~~~~~~~~
38
39 There are two ways you can use the hashing routines.
40
41 - Single-shot interface. If you have an in-memory buffer of the whole
42   message you want to hash, you can use this.
43
44   char *message = "Hello world";
45   byte output[MD5_SIZE];
46   md5_hash_buffer(output, message, strlen(message));
47
48 - Multi-shot interface. If you have the message scattered in many
49   buffers or you get it by parts, you do not need to concatenate the
50   parts together.
51
52   byte buffer[MAX_BUFFER];
53   uns buffer_len;
54   md5_context c;
55   md5_init(&c);
56   while(buffer_len = get_chunk(buffer, MAX_BUFFER)) {
57     md5_update(&c, buffer, buffer_len);
58   }
59   byte output[MD5_SIZE];
60   memcpy(output, md5_final(&c), MD5_SIZE);
61
62 SHA1 has the same interface, so the same two ways apply.
63
64 See also <<string:mem_to_hex()>>.
65
66 [[nocrypto]]
67 Non-cryptographic hashes
68 ------------------------
69
70 All these functions expect to be moduled by the size of a hash table.
71 The size should be a prime number (it gives better distribution).
72
73 !!ucw/hashfunc.h