Imported upstream version 0.59.3
[hcoop/debian/courier-authlib.git] / sha1 / sha1_hash.c
1 /*
2 ** Copyright 2001 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #include "sha1.h"
7 #include <string.h>
8
9 static const char rcsid[]="$Id: sha1_hash.c,v 1.1 2001/04/19 01:20:46 mrsam Exp $";
10
11 static const char base64tab[]=
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
14 const char *sha1_hash(const char *passw)
15 {
16 SHA1_DIGEST sha1buf;
17 static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
18 int a=0,b=0,c=0;
19 int i, j;
20 int d, e, f, g;
21
22 sha1_digest(passw, strlen(passw), sha1buf);
23
24 j=0;
25
26 for (i=0; i<sizeof(sha1buf); i += 3)
27 {
28 a=sha1buf[i];
29 b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
30 c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
31
32 d=base64tab[ a >> 2 ];
33 e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
34 f=base64tab[ ((b & 15) << 2) | (c >> 6)];
35 g=base64tab[ c & 63 ];
36 if (i + 1 >= sizeof(sha1buf)) f='=';
37 if (i + 2 >= sizeof(sha1buf)) g='=';
38 hash_buffer[j++]=d;
39 hash_buffer[j++]=e;
40 hash_buffer[j++]=f;
41 hash_buffer[j++]=g;
42 }
43
44 hash_buffer[j]=0;
45 return (hash_buffer);
46 }