Back to index

courier  0.68.2
md5_hash.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2007 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "md5.h"
00007 #include      <string.h>
00008 #include      <stdio.h>
00009 
00010 
00011 static const char base64tab[]=
00012 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00013 
00014 const char *md5_hash_courier(const char *passw)
00015 {
00016 MD5_DIGEST md5buf;
00017 static char hash_buffer[1+(sizeof(md5buf)+2)/3*4];
00018 int    a=0,b=0,c=0;
00019 int    i, j;
00020 int    d, e, f, g;
00021 
00022        md5_digest(passw, strlen(passw), md5buf);
00023 
00024        j=0;
00025 
00026        for (i=0; i<sizeof(md5buf); i += 3)
00027        {
00028               a=md5buf[i];
00029               b= i+1 < sizeof(md5buf) ? md5buf[i+1]:0;
00030               c= i+2 < sizeof(md5buf) ? md5buf[i+2]:0;
00031 
00032               d=base64tab[ a >> 2 ];
00033               e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
00034               f=base64tab[ ((b & 15) << 2) | (c >> 6)];
00035               g=base64tab[ c & 63 ];
00036               if (i + 1 >= sizeof(md5buf))       f='=';
00037               if (i + 2 >= sizeof(md5buf)) g='=';
00038               hash_buffer[j++]=d;
00039               hash_buffer[j++]=e;
00040               hash_buffer[j++]=f;
00041               hash_buffer[j++]=g;
00042        }
00043 
00044        hash_buffer[j]=0;
00045        return (hash_buffer);
00046 }
00047 
00048 const char *md5_hash_raw(const char *passw)
00049 {
00050        MD5_DIGEST digest;
00051        static char hash_buffer[sizeof(digest)*2+1];
00052        size_t j=0,i=0;
00053 
00054        char
00055               tmp_buf[3];
00056 
00057        md5_digest(passw, strlen(passw), digest);
00058        for (j=0; j<sizeof(digest); j++)
00059        {
00060               sprintf(tmp_buf,"%02x",digest[j]);
00061               hash_buffer[i++]=tmp_buf[0];
00062               hash_buffer[i++]=tmp_buf[1];
00063        }
00064        hash_buffer[i]=0;
00065 
00066        return(hash_buffer);
00067 }