Back to index

courier  0.68.2
sha1_hash.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2008 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 #define SHA1_INTERNAL
00006 #include      "sha1.h"
00007 #include      <string.h>
00008 
00009 
00010 static const char base64tab[]=
00011 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00012 
00013 const char *sha1_hash(const char *passw)
00014 {
00015 SHA1_DIGEST sha1buf;
00016 static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
00017 int    a=0,b=0,c=0;
00018 int    i, j;
00019 int    d, e, f, g;
00020 
00021        sha1_digest(passw, strlen(passw), sha1buf);
00022 
00023        j=0;
00024 
00025        for (i=0; i<sizeof(sha1buf); i += 3)
00026        {
00027               a=sha1buf[i];
00028               b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
00029               c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
00030 
00031               d=base64tab[ a >> 2 ];
00032               e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
00033               f=base64tab[ ((b & 15) << 2) | (c >> 6)];
00034               g=base64tab[ c & 63 ];
00035               if (i + 1 >= sizeof(sha1buf))      f='=';
00036               if (i + 2 >= sizeof(sha1buf)) g='=';
00037               hash_buffer[j++]=d;
00038               hash_buffer[j++]=e;
00039               hash_buffer[j++]=f;
00040               hash_buffer[j++]=g;
00041        }
00042 
00043        hash_buffer[j]=0;
00044        return (hash_buffer);
00045 }
00046 
00047 const char *ssha_hash(const char *passw, SSHA_RAND seed)
00048 {
00049        unsigned char sha1buf[sizeof(SHA1_DIGEST)+sizeof(SSHA_RAND)];
00050 
00051        static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
00052        int    a=0,b=0,c=0;
00053        int    i, j;
00054        int    d, e, f, g;
00055        struct SHA1_CONTEXT ctx;
00056 
00057        sha1_context_init( &ctx );
00058        sha1_context_hashstream(&ctx, passw, strlen(passw));
00059        sha1_context_hashstream(&ctx, seed, sizeof(SSHA_RAND));
00060        sha1_context_endstream(&ctx, strlen(passw)+sizeof(SSHA_RAND));
00061        sha1_context_digest( &ctx, sha1buf );
00062 
00063        for(i=0; i<sizeof(SSHA_RAND); i++)
00064        {
00065               sha1buf[sizeof(SHA1_DIGEST)+i] = seed[i];
00066        }
00067 
00068        j=0;
00069 
00070        for (i=0; i<sizeof(sha1buf); i += 3)
00071        {
00072               a=sha1buf[i];
00073               b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
00074               c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
00075 
00076               d=base64tab[ a >> 2 ];
00077               e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
00078               f=base64tab[ ((b & 15) << 2) | (c >> 6)];
00079               g=base64tab[ c & 63 ];
00080               if (i + 1 >= sizeof(sha1buf))      f='=';
00081               if (i + 2 >= sizeof(sha1buf)) g='=';
00082               hash_buffer[j++]=d;
00083               hash_buffer[j++]=e;
00084               hash_buffer[j++]=f;
00085               hash_buffer[j++]=g;
00086        }
00087 
00088        hash_buffer[j]=0;
00089        return (hash_buffer);
00090 }