MemCacheClient

sha1.h

00001 /*
00002  * SOURCE:  http://www.aarongifford.com/computers/sha.html
00003  * FILE:    http://www.aarongifford.com/computers/hmac_sha1.tar.gz
00004  * DATE:    27 June 2008
00005  * LICENCE: Public Domain (sha.h)
00006  * LICENCE: BSD (hmac_sha1.h)
00007  *
00008  * Modified to add const specifiers and the SHA1() and HMAC_SHA1()
00009  * convenience functions.
00010  */
00011 
00012 /*
00013  * sha.h
00014  *
00015  * Originally taken from the public domain SHA1 implementation
00016  * written by by Steve Reid <steve@edmweb.com>
00017  * 
00018  * Modified by Aaron D. Gifford <agifford@infowest.com>
00019  *
00020  * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN
00021  *
00022  * The original unmodified version is available at:
00023  *    ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00031  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00032  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00034  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035  * SUCH DAMAGE.
00036  */
00037 
00038 #ifndef __SHA1_H__
00039 #define __SHA1_H__
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 /* Define this if your machine is LITTLE_ENDIAN, otherwise #undef it: */
00046 #ifdef _WIN32
00047 # define LITTLE_ENDIAN
00048 #else
00049 # include <endian.h>
00050 #endif
00051 
00052 /* Make sure you define these types for your architecture: */
00053 typedef unsigned int sha1_quadbyte; /* 4 byte type */
00054 typedef unsigned char sha1_byte;    /* single byte type */
00055 
00056 /*
00057  * Be sure to get the above definitions right.  For instance, on my
00058  * x86 based FreeBSD box, I define LITTLE_ENDIAN and use the type
00059  * "unsigned long" for the quadbyte.  On FreeBSD on the Alpha, however,
00060  * while I still use LITTLE_ENDIAN, I must define the quadbyte type
00061  * as "unsigned int" instead.
00062  */
00063 
00064 #define SHA1_BLOCK_LENGTH   64
00065 #define SHA1_DIGEST_LENGTH  20
00066 
00067 /* The SHA1 structure: */
00068 typedef struct _SHA_CTX {
00069     sha1_quadbyte   state[5];
00070     sha1_quadbyte   count[2];
00071     sha1_byte   buffer[SHA1_BLOCK_LENGTH];
00072 } SHA_CTX;
00073 
00074 #ifndef NOPROTO
00075 void SHA1_Init(SHA_CTX *context);
00076 void SHA1_Update(SHA_CTX *context, const sha1_byte *data, unsigned int len);
00077 void SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX* context);
00078 void SHA1(sha1_byte digest[SHA1_DIGEST_LENGTH], const sha1_byte *data, unsigned int len);
00079 #else
00080 void SHA1_Init();
00081 void SHA1_Update();
00082 void SHA1_Final();
00083 void SHA1();
00084 #endif
00085 
00086 #ifdef  __cplusplus
00087 }
00088 #endif
00089 
00090 #endif
00091 
00092 
00093 
00094 
00095 /*
00096  * hmac_sha1.h
00097  *
00098  * Version 1.0.0
00099  *
00100  * Written by Aaron D. Gifford <me@aarongifford.com>
00101  *
00102  * Copyright 1998, 2000 Aaron D. Gifford.  All rights reserved.
00103  *
00104  * Redistribution and use in source and binary forms, with or without
00105  * modification, are permitted provided that the following conditions
00106  * are met:
00107  * 1. Redistributions of source code must retain the above copyright
00108  *    notice, this list of conditions and the following disclaimer.
00109  * 2. Redistributions in binary form must reproduce the above copyright
00110  *    notice, this list of conditions and the following disclaimer in the
00111  *    documentation and/or other materials provided with the distribution.
00112  * 3. Neither the name of the copyright holder nor the names of contributors
00113  *    may be used to endorse or promote products derived from this software
00114  *    without specific prior written permission.
00115  * 
00116  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
00117  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00118  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00119  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
00120  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00121  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00122  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00123  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00124  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00125  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00126  * SUCH DAMAGE.
00127  */
00128 
00129 #ifndef HEADER_HMAC_SHA1_H
00130 #define HEADER_HMAC_SHA1_H
00131 
00132 /*
00133  * Include SHA-1 stuff - CHOOSE WHICH SOURCE to use for the SHA1 functions
00134  *
00135  * Use the below include if your system has a library with SHA1 and be sure
00136  * to link to the library:
00137  */
00138 
00139 /* #include <sha.h> */
00140 
00141 /*
00142  * Or you can use Steve Reid's public domain SHA1 implementation:
00143  */
00144 
00145 /* #include "sha.h" */
00146 
00147 #ifdef  __cplusplus
00148 extern "C" {
00149 #endif
00150 
00151 #define HMAC_SHA1_DIGEST_LENGTH 20
00152 #define HMAC_SHA1_BLOCK_LENGTH  64
00153 
00154 /* The HMAC_SHA1 structure: */
00155 typedef struct _HMAC_SHA1_CTX {
00156     unsigned char   ipad[HMAC_SHA1_BLOCK_LENGTH];
00157     unsigned char   opad[HMAC_SHA1_BLOCK_LENGTH];
00158     SHA_CTX     shactx;
00159     unsigned char   key[HMAC_SHA1_BLOCK_LENGTH];
00160     unsigned int    keylen;
00161     unsigned int    hashkey;
00162 } HMAC_SHA1_CTX;
00163 
00164 #ifndef NOPROTO
00165 void HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx);
00166 void HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const unsigned char *key, unsigned int keylen);
00167 void HMAC_SHA1_EndKey(HMAC_SHA1_CTX *ctx);
00168 void HMAC_SHA1_StartMessage(HMAC_SHA1_CTX *ctx);
00169 void HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, const unsigned char *data, unsigned int datalen);
00170 void HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx);
00171 void HMAC_SHA1_Done(HMAC_SHA1_CTX *ctx);
00172 void HMAC_SHA1(unsigned char *out, const unsigned char *key, unsigned int keylen, const unsigned char *data, unsigned int datalen);
00173 #else
00174 void HMAC_SHA1_Init();
00175 void HMAC_SHA1_UpdateKey();
00176 void HMAC_SHA1_EndKey();
00177 void HMAC_SHA1_StartMessage();
00178 void HMAC_SHA1_UpdateMessage();
00179 void HMAC_SHA1_EndMessage();
00180 void HMAC_SHA1_Done();
00181 void HMAC_SHA1();
00182 #endif
00183 
00184 #ifdef  __cplusplus
00185 }
00186 #endif
00187 
00188 #endif