-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/pdumais/DumaisLib
- Loading branch information
Showing
15 changed files
with
247 additions
and
597 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.