aboutsummaryrefslogtreecommitdiff
path: root/src/md5.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-08-04 05:24:13 +0000
committerMike Buland <eichlan@xagasoft.com>2009-08-04 05:24:13 +0000
commit9e48c6f7d602364eb1c18de7e1e4c00e4852839c (patch)
tree0f4ba529a0d58453227b7cddc429ed97494a14ef /src/md5.cpp
parent6e6402825bcd6021d62fd2eb8a0669641fe9c266 (diff)
downloadlibbu++-9e48c6f7d602364eb1c18de7e1e4c00e4852839c.tar.gz
libbu++-9e48c6f7d602364eb1c18de7e1e4c00e4852839c.tar.bz2
libbu++-9e48c6f7d602364eb1c18de7e1e4c00e4852839c.tar.xz
libbu++-9e48c6f7d602364eb1c18de7e1e4c00e4852839c.zip
***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.
Diffstat (limited to 'src/md5.cpp')
-rw-r--r--src/md5.cpp47
1 files changed, 34 insertions, 13 deletions
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 @@
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h> 10#include <string.h>
11#include "md5.h" 11#include "md5.h"
12#include "bu/stream.h"
13
12 14
13// This performs a wrapping bitwise shift, kinda' fun! 15// This performs a wrapping bitwise shift, kinda' fun!
14 16
@@ -84,11 +86,39 @@ void Bu::Md5::addData( const void *sVData, int iSize )
84 86
85Bu::FString Bu::Md5::getResult() 87Bu::FString Bu::Md5::getResult()
86{ 88{
89 long lsum[4];
90 compCap( lsum );
91 return Bu::FString( (const char *)lsum, 4*4 );
92}
93
94void Bu::Md5::writeResult( Bu::Stream &sOut )
95{
96 long lsum[4];
97 compCap( lsum );
98 sOut.write( lsum, 4*4 );
99}
100
101Bu::FString Bu::Md5::getHexResult()
102{
87 static const char hex_tab[] = {"0123456789abcdef"}; 103 static const char hex_tab[] = {"0123456789abcdef"};
88 char str[33]; 104 char str[33];
89 105
90 long lsum[4]; 106 long lsum[4];
91 memcpy( lsum, sum, 4*4 ); 107 compCap( lsum );
108
109 int k = 0;
110 for( int i = 0; i < 16; i++ )
111 {
112 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8+4)) & 0xF];
113 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8 )) & 0xF];
114 }
115
116 return Bu::FString( str, 32 );
117}
118
119void Bu::Md5::compCap( long *sumout )
120{
121 memcpy( sumout, sum, 4*4 );
92 long lbuf[16]; 122 long lbuf[16];
93 memcpy( lbuf, inbuf, 4*16 ); 123 memcpy( lbuf, inbuf, 4*16 );
94 124
@@ -96,25 +126,16 @@ Bu::FString Bu::Md5::getResult()
96 uint64_t iBits = iBytes*8; 126 uint64_t iBits = iBytes*8;
97 if( iBytes > 0 && iFill>>2 >= 14 ) 127 if( iBytes > 0 && iFill>>2 >= 14 )
98 { 128 {
99 compBlock( lbuf, lsum ); 129 compBlock( lbuf, sumout );
100 memset( lbuf, 0, 4*16 ); 130 memset( lbuf, 0, 4*16 );
101 memcpy( lbuf+14, &iBits, 8 ); 131 memcpy( lbuf+14, &iBits, 8 );
102 compBlock( lbuf, lsum ); 132 compBlock( lbuf, sumout );
103 } 133 }
104 else 134 else
105 { 135 {
106 memcpy( lbuf+14, &iBits, 8 ); 136 memcpy( lbuf+14, &iBits, 8 );
107 compBlock( lbuf, lsum ); 137 compBlock( lbuf, sumout );
108 } 138 }
109
110 int k = 0;
111 for( int i = 0; i < 16; i++ )
112 {
113 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8+4)) & 0xF];
114 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8 )) & 0xF];
115 }
116
117 return Bu::FString( str, 32 );
118} 139}
119 140
120void Bu::Md5::compBlock( long *x, long *lsum ) 141void Bu::Md5::compBlock( long *x, long *lsum )