Imported upstream version 0.60.1
[hcoop/debian/courier-authlib.git] / md5 / md5_hash.c
1 /*
2 ** Copyright 2007 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #include "md5.h"
7 #include <string.h>
8 #include <stdio.h>
9
10 static const char rcsid[]="$Id: md5_hash.c,v 1.6 2007/10/07 02:50:45 mrsam Exp $";
11
12 static const char base64tab[]=
13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14
15 const char *md5_hash_courier(const char *passw)
16 {
17 MD5_DIGEST md5buf;
18 static char hash_buffer[1+(sizeof(md5buf)+2)/3*4];
19 int a=0,b=0,c=0;
20 int i, j;
21 int d, e, f, g;
22
23 md5_digest(passw, strlen(passw), md5buf);
24
25 j=0;
26
27 for (i=0; i<sizeof(md5buf); i += 3)
28 {
29 a=md5buf[i];
30 b= i+1 < sizeof(md5buf) ? md5buf[i+1]:0;
31 c= i+2 < sizeof(md5buf) ? md5buf[i+2]:0;
32
33 d=base64tab[ a >> 2 ];
34 e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
35 f=base64tab[ ((b & 15) << 2) | (c >> 6)];
36 g=base64tab[ c & 63 ];
37 if (i + 1 >= sizeof(md5buf)) f='=';
38 if (i + 2 >= sizeof(md5buf)) g='=';
39 hash_buffer[j++]=d;
40 hash_buffer[j++]=e;
41 hash_buffer[j++]=f;
42 hash_buffer[j++]=g;
43 }
44
45 hash_buffer[j]=0;
46 return (hash_buffer);
47 }
48
49 const char *md5_hash_raw(const char *passw)
50 {
51 MD5_DIGEST digest;
52 static char hash_buffer[sizeof(digest)*2+1];
53 size_t j=0,i=0;
54
55 char
56 tmp_buf[3];
57
58 md5_digest(passw, strlen(passw), digest);
59 for (j=0; j<sizeof(digest); j++)
60 {
61 sprintf(tmp_buf,"%02x",digest[j]);
62 hash_buffer[i++]=tmp_buf[0];
63 hash_buffer[i++]=tmp_buf[1];
64 }
65 hash_buffer[i]=0;
66
67 return(hash_buffer);
68 }