]> mj.ucw.cz Git - libucw.git/blob - ucw/sha1.h
Logging: Introduce LS_NUM_TYPES and use it.
[libucw.git] / ucw / sha1.h
1 /*
2  *      SHA-1 Hash Function (FIPS 180-1, RFC 3174)
3  *
4  *      (c) 2008--2009 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 should use it just as an opaque handle only.
20  */
21 typedef struct {
22   u32 h0,h1,h2,h3,h4;
23   u32 nblocks;
24   byte buf[64];
25   int count;
26 } sha1_context;
27
28 void sha1_init(sha1_context *hd); /** Initialize new algorithm run in the @hd context. **/
29 /**
30  * Push another @inlen bytes of data pointed to by @inbuf onto the
31  * SHA1 hash currently in @hd. You can call this any times you want on
32  * the same hash (and you do not need to reinitialize it by
33  * @sha1_init()). It has the same effect as concatenating all the data
34  * together and passing them at once.
35  */
36 void sha1_update(sha1_context *hd, const byte *inbuf, uns inlen);
37 /**
38  * No more @sha1_update() calls will be done. This terminates the hash
39  * and returns a pointer to it.
40  *
41  * Note that the pointer points into data in the @hd context. If it ceases
42  * to exist, the pointer becomes invalid.
43  *
44  * To convert the hash to its usual hexadecimal representation, see
45  * <<string:mem_to_hex()>>.
46  */
47 byte *sha1_final(sha1_context *hd);
48
49 /**
50  * A convenience one-shot function for SHA1 hash.
51  * It is equivalent to this snippet of code:
52  *
53  *  sha1_context hd;
54  *  sha1_init(&hd);
55  *  sha1_update(&hd, buffer, length);
56  *  memcpy(outbuf, sha1_final(&hd), SHA1_SIZE);
57  */
58 void sha1_hash_buffer(byte *outbuf, const byte *buffer, uns length);
59
60 /**
61  * SHA1 HMAC message authentication. If you provide @key and @data,
62  * the result will be stored in @outbuf.
63  */
64 void sha1_hmac(byte *outbuf, const byte *key, uns keylen, const byte *data, uns datalen);
65
66 /**
67  * The HMAC also exists in a stream version in a way analogous to the
68  * plain SHA1. Pass this as a context.
69  */
70 typedef struct {
71   sha1_context ictx;
72   sha1_context octx;
73 } sha1_hmac_context;
74
75 void sha1_hmac_init(sha1_hmac_context *hd, const byte *key, uns keylen);        /** Initialize HMAC with context @hd and the given key. See sha1_init(). */
76 void sha1_hmac_update(sha1_hmac_context *hd, const byte *data, uns datalen);    /** Hash another @datalen bytes of data. See sha1_update(). */
77 byte *sha1_hmac_final(sha1_hmac_context *hd);                                   /** Terminate the HMAC and return a pointer to the allocated hash. See sha1_final(). */
78
79 #define SHA1_SIZE 20 /** Size of the SHA1 hash in its binary representation **/
80 #define SHA1_HEX_SIZE 41 /** Buffer length for a string containing SHA1 in hexadecimal format. **/
81 #define SHA1_BLOCK_SIZE 64 /** SHA1 splits input to blocks of this size. **/
82
83 #endif