Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pdumais/DumaisLib
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamTambellini committed Oct 15, 2015
2 parents 8d9e839 + 122b8f5 commit c8a54e2
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 597 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ outdir:
mkdir -p $(INCLUDEDIR)

libs: outdir $(LIBS)
$(AR) rcs $(LIBDIR)/dumaislib.a obj/*.o
echo $(LIBS)

.PHONY: test
test: libs
$(AR) rcs $(LIBDIR)/dumaislib.a obj/*.o
cd tests && make $(TLS)
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>
#include "utils/sha256.h"
#include "utils/sha1.h"
#include "utils/md5.h"

#define SIZE 1000

Expand Down Expand Up @@ -30,6 +31,13 @@ int main(int argc, char** argv)
st = s4.toHex();
if (st!="3ae3644d6777a1f56a1defeabc74af9c4b313e49") printf("ERROR 4:%s\r\n",st.c_str()); else printf("OK 4\r\n");

MD5 s5(test,strlen(test));
st = s5.toHex();
if (st!="098f6bcd4621d373cade4e832627b4f6") printf("ERROR 5:%s\r\n",st.c_str()); else printf("OK 5\r\n");
MD5 s6(test2,SIZE);
st = s6.toHex();
if (st!="7644672d049290f0390d9c993c7d343d") printf("ERROR 6:%s\r\n",st.c_str()); else printf("OK 6\r\n");

delete test2;

}
2 changes: 1 addition & 1 deletion tests/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ int main(int argc, char** argv)
send(sock,ss.str().c_str(),ss.str().size(),0);
recv(sock,(char*)&rcv[0],12,0); // just receive 1 byte, we will ignore the response
rcv[12]=0;
ASSERT(!strcmp((char*)&rcv[9],"406"));
ASSERT(!strcmp((char*)&rcv[9],"501"));
}

// test 100 connections at the same time
Expand Down
46 changes: 46 additions & 0 deletions utils/Base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Base64.h"
#include <stdio.h>

#if __GNUC__ >= 4 and __GNUC_MINOR__ >= 3
#define MOVEBE(x) __builtin_bswap32(x);
#else
#define MOVEBE(x) ((x&0xff000000)>>24)|((x&0xff)<<24)|((x&0xff0000)>>8)|((x&0xff00)<<8)
#endif

char* Base64::digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string Base64::encode(const char* data, unsigned int size)
{
std::string str;
unsigned int tmp;
unsigned int i = 0;
while (i<size)
{
tmp = MOVEBE(*((unsigned int*)&data[i]));
if (size-i == 1)
{
str += digits[(tmp>>26)&0x3F];
str += digits[(tmp>>20)&0x3F];
str += "==";
i++;
}
else if (size-i == 2)
{
str += digits[(tmp>>26)&0x3F];
str += digits[(tmp>>20)&0x3F];
str += digits[(tmp>>14)&0x3F];
str += "=";
i+=2;
}
else
{
str += digits[(tmp>>26)&0x3F];
str += digits[(tmp>>20)&0x3F];
str += digits[(tmp>>14)&0x3F];
str += digits[(tmp>>8)&0x3F];
i+=3;
}
}

return str;
}
10 changes: 10 additions & 0 deletions utils/Base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <string>

class Base64
{
private:
static char* digits;
public:
static std::string encode(const char* data, unsigned int size);
};
125 changes: 125 additions & 0 deletions utils/md5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright (c) 2015 Patrick Dumais
http://www.dumaisnet.ca
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "md5.h"
#include <string.h>
#include <stdio.h>

#define ROL(x,shift) ((x<<shift)|(x>>(32-shift))) // could be faster with real rol instruction

using namespace Dumais::Utils;

MD5::MD5(const char* input, unsigned int size): Hash(16,input,size)
{
H[0] = 0x67452301;
H[1] = 0xefcdab89;
H[2] = 0x98badcfe;
H[3] = 0x10325476;

std::string st;
st.assign(input,size);
printf("$$$$$$$$$$$$$$$$ [%s]\r\n",st.c_str());

unsigned long i;
unsigned int a;
unsigned int b;
unsigned int c;
unsigned int d;
unsigned int f;
unsigned int g;
unsigned int tmp;

unsigned int size2 = size;
unsigned int size3 = 0;
unsigned int words[16];

unsigned long numChunks = (size+1+8+63) >> 6;// div by 512bit chunks (div by 64bytes)
unsigned int frameIndexForSize = numChunks-1;

unsigned char trail = 0x80;
for (unsigned long chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) // for each 512bit chunk
{
if (size2>=64)
{
size3=64;
memcpy((void*)words,(void*)input,64);
}
else
{
size3=size2;
char* cbuf = (char*)words;
if (size3) memcpy((void*)words,(void*)input,size3);
memset((char*)&cbuf[size3],0,64-size3);
((unsigned char*)words)[size3] = trail;
trail = 0;
}

if (chunkIndex == frameIndexForSize) // do this only a the end of the last frame
{
((unsigned long*)words)[7] = (((unsigned long)size)<<3);
}
input+=64;
size2-=size3;
unsigned int* chunkWords = words;

a=H[0];b=H[1];c=H[2];d=H[3];

for (i=0;i<64;i++)
{
if (i < 16)
{
f = (b&c)|(~b&d);
g = i;
}
else if (i < 32)
{
f = (d&b)|(~d&c);
g = (5*i +1)&0x0F;
}
else if (i < 48)
{
f = b^c^d;
g = (3*i +5)&0x0F;
}
else if (i < 64)
{
f = c^(b|~d);
g = (7*i)&0x0F;
}
tmp = d;
d = c;
c = b;
b = b+ROL(a+f+K[i]+chunkWords[g],s[i]);
a = tmp;

}

H[0]+=a;H[1]+=b;H[2]+=c;H[3]+=d;
}

std::string st1 = toHex();
printf("$$$$$$$$$$$$$$$$ MD5 [%s]\r\n",st1.c_str());
}



50 changes: 50 additions & 0 deletions utils/md5.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (c) 2015 Patrick Dumais
http://www.dumaisnet.ca
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "Hash.h"

namespace Dumais
{
namespace Utils
{
class MD5: public Hash
{
private:
unsigned int s[64]={7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21};
unsigned int K[64]={0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,
0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391};

public:
MD5(const char* input, unsigned int inputSize);
};
}
}
5 changes: 3 additions & 2 deletions webserver/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <sstream>
#include <time.h>
#include <fstream>
#include "md5.h"
#include "utils/md5.h"

using namespace Dumais::WebServer;

Expand Down Expand Up @@ -180,7 +180,8 @@ bool WebServer::validateAuthentication(HTTPRequest* req, HTTPAuth auth)

std::string WebServer::computemd5(const std::string& st)
{
return md5(st);
Dumais::Utils::MD5 md5(st.c_str(),st.size());
return md5.toHex();
}

void WebServer::clearExpiredSessions()
Expand Down
Loading

0 comments on commit c8a54e2

Please sign in to comment.