d9898ee8 |
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 | static const char rcsid[]="$Id: sha256_hash.c,v 1.1 2005/02/21 03:18:30 mrsam Exp $"; |
10 | |
11 | static const char base64tab[]= |
12 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
13 | |
14 | const char *sha256_hash(const char *passw) |
15 | { |
16 | SHA256_DIGEST sha256buf; |
17 | static char hash_buffer[1+(sizeof(sha256buf)+2)/3*4]; |
18 | int a=0,b=0,c=0; |
19 | int i, j; |
20 | int d, e, f, g; |
21 | |
22 | sha256_digest(passw, strlen(passw), sha256buf); |
23 | |
24 | j=0; |
25 | |
26 | for (i=0; i<sizeof(sha256buf); i += 3) |
27 | { |
28 | a=sha256buf[i]; |
29 | b= i+1 < sizeof(sha256buf) ? sha256buf[i+1]:0; |
30 | c= i+2 < sizeof(sha256buf) ? sha256buf[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(sha256buf)) f='='; |
37 | if (i + 2 >= sizeof(sha256buf)) 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 | } |