summaryrefslogtreecommitdiff
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
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 '')
-rw-r--r--src/crypt.cpp40
-rw-r--r--src/crypt.h12
-rw-r--r--src/cryptohash.h3
-rw-r--r--src/formatter.h6
-rw-r--r--src/md5.cpp47
-rw-r--r--src/md5.h3
-rw-r--r--src/tests/cryptpass.cpp18
-rw-r--r--src/tests/md5.cpp2
-rw-r--r--src/util.cpp1
9 files changed, 116 insertions, 16 deletions
diff --git a/src/crypt.cpp b/src/crypt.cpp
new file mode 100644
index 0000000..9111cda
--- /dev/null
+++ b/src/crypt.cpp
@@ -0,0 +1,40 @@
1#include "bu/crypt.h"
2#include "bu/md5.h"
3#include "bu/base64.h"
4#include "bu/membuf.h"
5#include "bu/file.h"
6
7Bu::FString Bu::cryptPass( const Bu::FString &sPass, const Bu::FString &sSalt )
8{
9 Bu::Md5 md5;
10 Bu::MemBuf mbOut;
11 Bu::Base64 b64Out( mbOut );
12
13 Bu::FString::const_iterator i = sSalt.find('$');
14 Bu::FString sSaltSml = sSalt.getSubStr( sSalt.begin(), i );
15
16 md5.addData( sPass );
17 md5.addData( sSaltSml );
18 md5.writeResult( b64Out );
19
20 b64Out.stop();
21
22 return sSaltSml + "$" + mbOut.getString();
23}
24
25Bu::FString Bu::cryptPass( const Bu::FString &sPass )
26{
27 Bu::MemBuf mbSalt;
28 Bu::Base64 b64Salt( mbSalt );
29 Bu::File fRand("/dev/urandom", Bu::File::Read );
30
31#define STR 6
32 char buf[STR];
33 fRand.read( buf, STR );
34 b64Salt.write( buf, STR );
35
36 b64Salt.stop();
37
38 return cryptPass( sPass, mbSalt.getString() );
39}
40
diff --git a/src/crypt.h b/src/crypt.h
new file mode 100644
index 0000000..1ea1b85
--- /dev/null
+++ b/src/crypt.h
@@ -0,0 +1,12 @@
1#ifndef BU_CRYPT_H
2#define BU_CRYPT_H
3
4#include "bu/fstring.h"
5
6namespace Bu
7{
8 FString cryptPass( const FString &sPass, const FString &sSalt );
9 FString cryptPass( const FString &sPass );
10};
11
12#endif
diff --git a/src/cryptohash.h b/src/cryptohash.h
index 87aaf09..91c9511 100644
--- a/src/cryptohash.h
+++ b/src/cryptohash.h
@@ -12,6 +12,8 @@
12 12
13namespace Bu 13namespace Bu
14{ 14{
15 class Stream;
16
15 class CryptoHash 17 class CryptoHash
16 { 18 {
17 public: 19 public:
@@ -23,6 +25,7 @@ namespace Bu
23 virtual void addData( const void *sData, int iSize ) = 0; 25 virtual void addData( const void *sData, int iSize ) = 0;
24 virtual void addData( const Bu::FString &sData ); 26 virtual void addData( const Bu::FString &sData );
25 virtual FString getResult() = 0; 27 virtual FString getResult() = 0;
28 virtual void writeResult( Stream &sOut ) = 0;
26 }; 29 };
27}; 30};
28 31
diff --git a/src/formatter.h b/src/formatter.h
index 8bb8e08..9f407a4 100644
--- a/src/formatter.h
+++ b/src/formatter.h
@@ -134,6 +134,7 @@ namespace Bu
134 { 134 {
135 // This code is taken from Nango, hopefully we can make it better. 135 // This code is taken from Nango, hopefully we can make it better.
136 bool bNeg = i<0; 136 bool bNeg = i<0;
137 char cBase = fLast.bCaps?'A':'a';
137 char buf[sizeof(type)*8+1]; 138 char buf[sizeof(type)*8+1];
138 if( bNeg ) i = -i; 139 if( bNeg ) i = -i;
139 if( fLast.uRadix < 2 || fLast.uRadix > 36 ) 140 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
@@ -146,7 +147,7 @@ namespace Bu
146 { 147 {
147 int c = i%fLast.uRadix; 148 int c = i%fLast.uRadix;
148 i /= fLast.uRadix; 149 i /= fLast.uRadix;
149 buf[j] = (char)((c<10)?('0'+c):('A'+c-10)); 150 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
150 if( i == 0 ) 151 if( i == 0 )
151 { 152 {
152 if( bNeg ) buf[--j] = '-'; 153 if( bNeg ) buf[--j] = '-';
@@ -164,6 +165,7 @@ namespace Bu
164 { 165 {
165 // This code is taken from Nango, hopefully we can make it better. 166 // This code is taken from Nango, hopefully we can make it better.
166 char buf[sizeof(type)*8+1]; 167 char buf[sizeof(type)*8+1];
168 char cBase = fLast.bCaps?'A':'a';
167 if( fLast.uRadix < 2 || fLast.uRadix > 36 ) 169 if( fLast.uRadix < 2 || fLast.uRadix > 36 )
168 { 170 {
169 usedFormat(); 171 usedFormat();
@@ -174,7 +176,7 @@ namespace Bu
174 { 176 {
175 int c = i%fLast.uRadix; 177 int c = i%fLast.uRadix;
176 i /= fLast.uRadix; 178 i /= fLast.uRadix;
177 buf[j] = (char)((c<10)?('0'+c):('A'+c-10)); 179 buf[j] = (char)((c<10)?('0'+c):(cBase+c-10));
178 if( i == 0 ) 180 if( i == 0 )
179 { 181 {
180 if( fLast.bPlus ) buf[--j] = '+'; 182 if( fLast.bPlus ) buf[--j] = '+';
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 )
diff --git a/src/md5.h b/src/md5.h
index 96ba2ce..76bdd88 100644
--- a/src/md5.h
+++ b/src/md5.h
@@ -30,12 +30,15 @@ namespace Bu
30 virtual void addData( const void *sData, int iSize ); 30 virtual void addData( const void *sData, int iSize );
31 using Bu::CryptoHash::addData; 31 using Bu::CryptoHash::addData;
32 virtual FString getResult(); 32 virtual FString getResult();
33 virtual void writeResult( Bu::Stream &sOut );
34 virtual FString getHexResult();
33 35
34 private: 36 private:
35 /** 37 /**
36 * Compute one block of input data. 38 * Compute one block of input data.
37 */ 39 */
38 void compBlock( long *x, long *lsum ); 40 void compBlock( long *x, long *lsum );
41 void compCap( long *sumout );
39 42
40 long inbuf[16]; 43 long inbuf[16];
41 long iFill; 44 long iFill;
diff --git a/src/tests/cryptpass.cpp b/src/tests/cryptpass.cpp
new file mode 100644
index 0000000..4b090fe
--- /dev/null
+++ b/src/tests/cryptpass.cpp
@@ -0,0 +1,18 @@
1#include "bu/crypt.h"
2#include "bu/sio.h"
3
4using namespace Bu;
5
6int main( int argc, char *argv[] )
7{
8 if( argc == 1 )
9 sio << "Syntax: " << argv[0] << " <password> [salt]" << sio.nl
10 << sio.nl;
11 else if( argc == 2 )
12 sio << "Crypt1: >> " << cryptPass( argv[1] ) << " <<" << sio.nl;
13 else
14 sio << "Crypt2: >> " << cryptPass( argv[1], argv[2] ) << " <<" << sio.nl;
15
16 return 0;
17}
18
diff --git a/src/tests/md5.cpp b/src/tests/md5.cpp
index 3b3d5e7..69ddd1d 100644
--- a/src/tests/md5.cpp
+++ b/src/tests/md5.cpp
@@ -28,7 +28,7 @@ int main( int argc, char *argv[] )
28 break; 28 break;
29 } 29 }
30 30
31 sio << m.getResult() << " *" << *argv << sio.nl; 31 sio << m.getHexResult() << " *" << *argv << sio.nl;
32 } 32 }
33} 33}
34 34
diff --git a/src/util.cpp b/src/util.cpp
new file mode 100644
index 0000000..111574c
--- /dev/null
+++ b/src/util.cpp
@@ -0,0 +1 @@
#include "bu/util.h"