Back to index

courier  0.68.2
sha512_hash.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2008 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "sha1.h"
00007 #include      <string.h>
00008 
00009 
00010 static const char base64tab[]=
00011 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00012 
00013 const char *sha512_hash(const char *passw)
00014 {
00015 SHA512_DIGEST sha512buf;
00016 static char hash_buffer[1+(sizeof(sha512buf)+2)/3*4];
00017 int    a=0,b=0,c=0;
00018 int    i, j;
00019 int    d, e, f, g;
00020 
00021        sha512_digest(passw, strlen(passw), sha512buf);
00022 
00023        j=0;
00024 
00025        for (i=0; i<sizeof(sha512buf); i += 3)
00026        {
00027               a=sha512buf[i];
00028               b= i+1 < sizeof(sha512buf) ? sha512buf[i+1]:0;
00029               c= i+2 < sizeof(sha512buf) ? sha512buf[i+2]:0;
00030 
00031               d=base64tab[ a >> 2 ];
00032               e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
00033               f=base64tab[ ((b & 15) << 2) | (c >> 6)];
00034               g=base64tab[ c & 63 ];
00035               if (i + 1 >= sizeof(sha512buf))    f='=';
00036               if (i + 2 >= sizeof(sha512buf)) g='=';
00037               hash_buffer[j++]=d;
00038               hash_buffer[j++]=e;
00039               hash_buffer[j++]=f;
00040               hash_buffer[j++]=g;
00041        }
00042 
00043        hash_buffer[j]=0;
00044        return (hash_buffer);
00045 }