Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / sha1 / sha1_hash.c
1 /*
2 ** Copyright 2001-2008 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5 #define SHA1_INTERNAL
6 #include "sha1.h"
7 #include <string.h>
8
9 static const char rcsid[]="$Id: sha1_hash.c,v 1.3 2009/06/27 16:32:38 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 }
47
48 const char *ssha_hash(const char *passw, SSHA_RAND seed)
49 {
50 unsigned char sha1buf[sizeof(SHA1_DIGEST)+sizeof(SSHA_RAND)];
51
52 static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
53 int a=0,b=0,c=0;
54 int i, j;
55 int d, e, f, g;
56 struct SHA1_CONTEXT ctx;
57
58 sha1_context_init( &ctx );
59 sha1_context_hashstream(&ctx, passw, strlen(passw));
60 sha1_context_hashstream(&ctx, seed, sizeof(SSHA_RAND));
61 sha1_context_endstream(&ctx, strlen(passw)+sizeof(SSHA_RAND));
62 sha1_context_digest( &ctx, sha1buf );
63
64 for(i=0; i<sizeof(SSHA_RAND); i++)
65 {
66 sha1buf[sizeof(SHA1_DIGEST)+i] = seed[i];
67 }
68
69 j=0;
70
71 for (i=0; i<sizeof(sha1buf); i += 3)
72 {
73 a=sha1buf[i];
74 b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
75 c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
76
77 d=base64tab[ a >> 2 ];
78 e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
79 f=base64tab[ ((b & 15) << 2) | (c >> 6)];
80 g=base64tab[ c & 63 ];
81 if (i + 1 >= sizeof(sha1buf)) f='=';
82 if (i + 2 >= sizeof(sha1buf)) g='=';
83 hash_buffer[j++]=d;
84 hash_buffer[j++]=e;
85 hash_buffer[j++]=f;
86 hash_buffer[j++]=g;
87 }
88
89 hash_buffer[j]=0;
90 return (hash_buffer);
91 }