2 * HMAC-SHA1 Message Authentication Code (RFC 2202)
4 * (c) 2008 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
16 sha1_hmac(byte *outbuf, const byte *key, uns keylen, const byte *data, uns datalen)
18 sha1_context ictx, octx;
19 byte keybuf[SHA1_BLOCK_SIZE], buf[SHA1_BLOCK_SIZE];
21 // Hash the key if necessary
22 if (keylen <= SHA1_BLOCK_SIZE)
24 memcpy(keybuf, key, keylen);
25 bzero(keybuf + keylen, SHA1_BLOCK_SIZE - keylen);
29 sha1_hash_buffer(keybuf, key, keylen);
30 bzero(keybuf + SHA1_SIZE, SHA1_BLOCK_SIZE - SHA1_SIZE);
35 for (int i=0; i < SHA1_BLOCK_SIZE; i++)
36 buf[i] = keybuf[i] ^ 0x36;
37 sha1_update(&ictx, buf, SHA1_BLOCK_SIZE);
38 sha1_update(&ictx, data, datalen);
39 byte *isha = sha1_final(&ictx);
43 for (int i=0; i < SHA1_BLOCK_SIZE; i++)
44 buf[i] = keybuf[i] ^ 0x5c;
45 sha1_update(&octx, buf, SHA1_BLOCK_SIZE);
46 sha1_update(&octx, isha, SHA1_SIZE);
47 byte *osha = sha1_final(&octx);
50 memcpy(outbuf, osha, SHA1_SIZE);
56 #include "ucw/string.h"
58 static uns rd(char *dest)
61 fgets(buf, sizeof(buf), stdin);
62 *strchr(buf, '\n') = 0;
63 if (buf[0] == '0' && buf[1] == 'x')
65 const char *e = hex_to_mem(dest, buf+2, 1024, 0);
78 char key[1024], data[1024];
82 sha1_hmac(hmac, key, kl, data, dl);
83 mem_to_hex(data, hmac, SHA1_SIZE, 0);