Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / sha1 / sha256_hash.c
1 /*
2 ** Copyright 2005 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #include "sha1.h"
7 #include <string.h>
8
9
10 static const char base64tab[]=
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
13 const char *sha256_hash(const char *passw)
14 {
15 SHA256_DIGEST sha256buf;
16 static char hash_buffer[1+(sizeof(sha256buf)+2)/3*4];
17 int a=0,b=0,c=0;
18 int i, j;
19 int d, e, f, g;
20
21 sha256_digest(passw, strlen(passw), sha256buf);
22
23 j=0;
24
25 for (i=0; i<sizeof(sha256buf); i += 3)
26 {
27 a=sha256buf[i];
28 b= i+1 < sizeof(sha256buf) ? sha256buf[i+1]:0;
29 c= i+2 < sizeof(sha256buf) ? sha256buf[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(sha256buf)) f='=';
36 if (i + 2 >= sizeof(sha256buf)) 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 }