| 1 | /* |
| 2 | ** Copyright 2008 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 *sha512_hash(const char *passw) |
| 14 | { |
| 15 | SHA512_DIGEST sha512buf; |
| 16 | static char hash_buffer[1+(sizeof(sha512buf)+2)/3*4]; |
| 17 | int a=0,b=0,c=0; |
| 18 | int i, j; |
| 19 | int d, e, f, g; |
| 20 | |
| 21 | sha512_digest(passw, strlen(passw), sha512buf); |
| 22 | |
| 23 | j=0; |
| 24 | |
| 25 | for (i=0; i<sizeof(sha512buf); i += 3) |
| 26 | { |
| 27 | a=sha512buf[i]; |
| 28 | b= i+1 < sizeof(sha512buf) ? sha512buf[i+1]:0; |
| 29 | c= i+2 < sizeof(sha512buf) ? sha512buf[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(sha512buf)) f='='; |
| 36 | if (i + 2 >= sizeof(sha512buf)) 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 | } |