Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / 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
10 static const char base64tab[]=
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
13 const char *sha1_hash(const char *passw)
14 {
15 SHA1_DIGEST sha1buf;
16 static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
17 int a=0,b=0,c=0;
18 int i, j;
19 int d, e, f, g;
20
21 sha1_digest(passw, strlen(passw), sha1buf);
22
23 j=0;
24
25 for (i=0; i<sizeof(sha1buf); i += 3)
26 {
27 a=sha1buf[i];
28 b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
29 c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
30
31 d=base64tab[ a >> 2 ];
32 e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
33 f=base64tab[ ((b & 15) << 2) | (c >> 6)];
34 g=base64tab[ c & 63 ];
35 if (i + 1 >= sizeof(sha1buf)) f='=';
36 if (i + 2 >= sizeof(sha1buf)) g='=';
37 hash_buffer[j++]=d;
38 hash_buffer[j++]=e;
39 hash_buffer[j++]=f;
40 hash_buffer[j++]=g;
41 }
42
43 hash_buffer[j]=0;
44 return (hash_buffer);
45 }
46
47 const char *ssha_hash(const char *passw, SSHA_RAND seed)
48 {
49 unsigned char sha1buf[sizeof(SHA1_DIGEST)+sizeof(SSHA_RAND)];
50
51 static char hash_buffer[1+(sizeof(sha1buf)+2)/3*4];
52 int a=0,b=0,c=0;
53 int i, j;
54 int d, e, f, g;
55 struct SHA1_CONTEXT ctx;
56
57 sha1_context_init( &ctx );
58 sha1_context_hashstream(&ctx, passw, strlen(passw));
59 sha1_context_hashstream(&ctx, seed, sizeof(SSHA_RAND));
60 sha1_context_endstream(&ctx, strlen(passw)+sizeof(SSHA_RAND));
61 sha1_context_digest( &ctx, sha1buf );
62
63 for(i=0; i<sizeof(SSHA_RAND); i++)
64 {
65 sha1buf[sizeof(SHA1_DIGEST)+i] = seed[i];
66 }
67
68 j=0;
69
70 for (i=0; i<sizeof(sha1buf); i += 3)
71 {
72 a=sha1buf[i];
73 b= i+1 < sizeof(sha1buf) ? sha1buf[i+1]:0;
74 c= i+2 < sizeof(sha1buf) ? sha1buf[i+2]:0;
75
76 d=base64tab[ a >> 2 ];
77 e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
78 f=base64tab[ ((b & 15) << 2) | (c >> 6)];
79 g=base64tab[ c & 63 ];
80 if (i + 1 >= sizeof(sha1buf)) f='=';
81 if (i + 2 >= sizeof(sha1buf)) g='=';
82 hash_buffer[j++]=d;
83 hash_buffer[j++]=e;
84 hash_buffer[j++]=f;
85 hash_buffer[j++]=g;
86 }
87
88 hash_buffer[j]=0;
89 return (hash_buffer);
90 }