]> mj.ucw.cz Git - libucw.git/blob - ucw/sha1.h
ucw docs: Hash routines
[libucw.git] / ucw / sha1.h
1 /*
2  *      SHA-1 Hash Function (FIPS 180-1, RFC 3174)
3  *
4  *      (c) 2008 Martin Mares <mj@ucw.cz>
5  *
6  *      Based on the code from libgcrypt-1.2.3, which was:
7  *
8  *      Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
9  *
10  *      This software may be freely distributed and used according to the terms
11  *      of the GNU Lesser General Public License.
12  */
13
14 #ifndef _UCW_SHA1_H
15 #define _UCW_SHA1_H
16
17 /**
18  * Internal SHA1 state.
19  * You can consider it an opaque handle, if you want just hash
20  * functions.
21  */
22 typedef struct {
23   u32 h0,h1,h2,h3,h4;
24   u32 nblocks;
25   byte buf[64];
26   int count;
27 } sha1_context;
28
29 void sha1_init(sha1_context *hd); /** Initialize new algorithm run in the @hd context. **/
30 /**
31  * Push another @inlen bytes of data pointed to by @inbuf onto the
32  * SHA1 hash currently in @hd. You can call this any times you want on
33  * the same hash (and you do not need to reinitialize it by
34  * sha1_init()). It has the same effect as concatenating all the data
35  * together and passing them at once.
36  */
37 void sha1_update(sha1_context *hd, const byte *inbuf, uns inlen);
38 /**
39  * No more sha1_update() calls will be done. This terminates the hash
40  * and returns pointer to it.
41  *
42  * Note the pointer points into data in the @hd context. If it ceases
43  * to exist, the pointer becomes invalid.
44  */
45 byte *sha1_final(sha1_context *hd);
46
47 /**
48  * Convenience one-shot function for SHA1 hash.
49  * It is equivalent to this snippet of code:
50  *
51  *  sha1_context hd;
52  *  sha1_init(&hd);
53  *  sha1_update(&hd, buffer, length);
54  *  memcpy(outbuf, sha1_final(&hd), 20);
55  */
56 void sha1_hash_buffer(byte *outbuf, const byte *buffer, uns length);
57
58 /**
59  * SHA1 HMAC message authentication. If you provide @key and @data,
60  * the result will be stored in @outbuf.
61  */
62 void sha1_hmac(byte *outbuf, const byte *key, uns keylen, const byte *data, uns datalen);
63
64 #define SHA1_SIZE 20 /** Size of the SHA1 hash in its binary representation **/
65 #define SHA1_HEX_SIZE 41 /** Buffer length for a string containing SHA1 in hexadecimal format. **/
66 #define SHA1_BLOCK_SIZE 64 /** SHA1 splits input to blocks of this size. **/
67
68 #endif