00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <openssl/sha.h>
00019 #include <openssl/evp.h>
00020
00021 #include "SHA1HashFunction.h"
00022
00023 using namespace CODEX_Ciphers;
00024
00025 HashFunction::ustring*
00026 SHA1HashFunction::operator()( const ustring& buff ) const
00027 {
00028 EVP_MD_CTX ctx;
00029 EVP_DigestInit( &ctx, EVP_sha1() );
00030 EVP_DigestUpdate( &ctx, buff.data(), buff.length() );
00031 unsigned char output[ SHA_DIGEST_LENGTH ];
00032 unsigned int s;
00033 EVP_DigestFinal( &ctx, output, &s );
00034 return new ustring( output, SHA_DIGEST_LENGTH );
00035 }
00036
00037 HashFunction::ustring*
00038 SHA1HashFunction::operator()( const ustring& buff, unsigned int len ) const
00039 {
00040 unsigned int bytesNeeded = (len+7)/8;
00041 EVP_MD_CTX ctx;
00042 EVP_MD_CTX_init( &ctx );
00043 ustring* retVal = new ustring;
00044 ustring junkBuff;
00045
00046
00047 unsigned char i = 0;
00048 while ( bytesNeeded > 0 )
00049 {
00050 junkBuff.push_back(i);
00051 i = ( i < 255 ) ? i+1 : 0;
00052 EVP_DigestInit_ex( &ctx, EVP_sha1(), 0 );
00053 EVP_DigestUpdate( &ctx, buff.data(), buff.length() );
00054 EVP_DigestUpdate( &ctx, junkBuff.data(), junkBuff.length() );
00055 unsigned char output[ SHA_DIGEST_LENGTH ];
00056 unsigned int s;
00057 EVP_DigestFinal_ex( &ctx, output, &s );
00058 unsigned int write_length = ( s < bytesNeeded ) ? s : bytesNeeded;
00059 retVal->reserve( retVal->capacity() + write_length );
00060 for ( unsigned int j = 0 ; j < write_length ; ++j )
00061 {
00062 retVal->push_back(output[j]);
00063 }
00064 bytesNeeded -= write_length;
00065 }
00066
00067 EVP_MD_CTX_cleanup( &ctx );
00068 return retVal;
00069 }