diff options
Diffstat (limited to '')
-rw-r--r-- | src/crypt.cpp | 40 | ||||
-rw-r--r-- | src/crypt.h | 12 | ||||
-rw-r--r-- | src/cryptohash.h | 3 | ||||
-rw-r--r-- | src/formatter.h | 6 | ||||
-rw-r--r-- | src/md5.cpp | 47 | ||||
-rw-r--r-- | src/md5.h | 3 | ||||
-rw-r--r-- | src/tests/cryptpass.cpp | 18 | ||||
-rw-r--r-- | src/tests/md5.cpp | 2 | ||||
-rw-r--r-- | src/util.cpp | 1 |
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 | |||
7 | Bu::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 | |||
25 | Bu::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 | |||
6 | namespace 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 | ||
13 | namespace Bu | 13 | namespace 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 | ||
85 | Bu::FString Bu::Md5::getResult() | 87 | Bu::FString Bu::Md5::getResult() |
86 | { | 88 | { |
89 | long lsum[4]; | ||
90 | compCap( lsum ); | ||
91 | return Bu::FString( (const char *)lsum, 4*4 ); | ||
92 | } | ||
93 | |||
94 | void Bu::Md5::writeResult( Bu::Stream &sOut ) | ||
95 | { | ||
96 | long lsum[4]; | ||
97 | compCap( lsum ); | ||
98 | sOut.write( lsum, 4*4 ); | ||
99 | } | ||
100 | |||
101 | Bu::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 | |||
119 | void 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 | ||
120 | void Bu::Md5::compBlock( long *x, long *lsum ) | 141 | void Bu::Md5::compBlock( long *x, long *lsum ) |
@@ -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 | |||
4 | using namespace Bu; | ||
5 | |||
6 | int 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" | |||