From 9e48c6f7d602364eb1c18de7e1e4c00e4852839c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 4 Aug 2009 05:24:13 +0000 Subject: ***IMPORTANT*** The function Bu::Md5::getResult no longer returns a hex string, it returns the raw binary string that makes up the md5 sum, this matches the original goal of the API and makes the whole system more general and transportable. I have added a handy helper function named getHexResult that will return the same classic hex md5 string we're used to, change anything in your code that uses getResult to getHexResult now. I've also added a handy function to the CryptoHash to write the result to a stream, writeResult. I've fixed some more things in the formatter, and added a cryptPass function that works very much like the system crypt function, md5 and base64. If I knew more about the glibc implementation I could probably make them compatible. For now there are some subtle differences in the formatting and the salting algorithm, also the output mantains it's base64 trailer (==) wheras the system function chops those off. There's also another helper that will only work on linux for now, that only takes the password, and generates a salt for you using urandom. --- src/md5.cpp | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'src/md5.cpp') diff --git a/src/md5.cpp b/src/md5.cpp index 14b244a..8d7b7c9 100644 --- a/src/md5.cpp +++ b/src/md5.cpp @@ -9,6 +9,8 @@ #include #include #include "md5.h" +#include "bu/stream.h" + // This performs a wrapping bitwise shift, kinda' fun! @@ -83,12 +85,40 @@ void Bu::Md5::addData( const void *sVData, int iSize ) } Bu::FString Bu::Md5::getResult() +{ + long lsum[4]; + compCap( lsum ); + return Bu::FString( (const char *)lsum, 4*4 ); +} + +void Bu::Md5::writeResult( Bu::Stream &sOut ) +{ + long lsum[4]; + compCap( lsum ); + sOut.write( lsum, 4*4 ); +} + +Bu::FString Bu::Md5::getHexResult() { static const char hex_tab[] = {"0123456789abcdef"}; char str[33]; long lsum[4]; - memcpy( lsum, sum, 4*4 ); + compCap( lsum ); + + int k = 0; + for( int i = 0; i < 16; i++ ) + { + str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8+4)) & 0xF]; + str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8 )) & 0xF]; + } + + return Bu::FString( str, 32 ); +} + +void Bu::Md5::compCap( long *sumout ) +{ + memcpy( sumout, sum, 4*4 ); long lbuf[16]; memcpy( lbuf, inbuf, 4*16 ); @@ -96,25 +126,16 @@ Bu::FString Bu::Md5::getResult() uint64_t iBits = iBytes*8; if( iBytes > 0 && iFill>>2 >= 14 ) { - compBlock( lbuf, lsum ); + compBlock( lbuf, sumout ); memset( lbuf, 0, 4*16 ); memcpy( lbuf+14, &iBits, 8 ); - compBlock( lbuf, lsum ); + compBlock( lbuf, sumout ); } else { memcpy( lbuf+14, &iBits, 8 ); - compBlock( lbuf, lsum ); + compBlock( lbuf, sumout ); } - - int k = 0; - for( int i = 0; i < 16; i++ ) - { - str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8+4)) & 0xF]; - str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8 )) & 0xF]; - } - - return Bu::FString( str, 32 ); } void Bu::Md5::compBlock( long *x, long *lsum ) -- cgit v1.2.3