|
|
|
@ -35,32 +35,39 @@ using namespace NLMISC;
|
|
|
|
|
#define new DEBUG_NEW
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace NLMISC {
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Types
|
|
|
|
|
// Constantes
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If you do not have the ISO standard stdint.h header file, then you
|
|
|
|
|
* must typedef the following:
|
|
|
|
|
* name meaning
|
|
|
|
|
* uint32_t unsigned 32 bit integer
|
|
|
|
|
* uint8_t unsigned 8 bit integer (i.e., unsigned char)
|
|
|
|
|
* int_least16_t integer of >= 16 bits
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef unsigned int uint32_t;
|
|
|
|
|
typedef unsigned char uint8_t;
|
|
|
|
|
typedef short int_least16_t;
|
|
|
|
|
static const int bufferSize = 100000;
|
|
|
|
|
static uint8 buffer[bufferSize];
|
|
|
|
|
|
|
|
|
|
namespace NLMISC {
|
|
|
|
|
#define SHA1HashSize 20
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Constantes
|
|
|
|
|
// Types
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
static const int bufferSize = 100000;
|
|
|
|
|
static uint8_t buffer[bufferSize];
|
|
|
|
|
/*
|
|
|
|
|
* This structure will hold context information for the SHA-1
|
|
|
|
|
* hashing operation
|
|
|
|
|
*/
|
|
|
|
|
typedef struct SHA1Context
|
|
|
|
|
{
|
|
|
|
|
uint32 Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
|
|
|
|
|
|
|
|
|
|
uint32 Length_Low; /* Message length in bits */
|
|
|
|
|
uint32 Length_High; /* Message length in bits */
|
|
|
|
|
|
|
|
|
|
/* Index into message block array */
|
|
|
|
|
sint16 Message_Block_Index;
|
|
|
|
|
uint8 Message_Block[64]; /* 512-bit message blocks */
|
|
|
|
|
|
|
|
|
|
int Computed; /* Is the digest computed? */
|
|
|
|
|
int Corrupted; /* Is the message digest corrupted? */
|
|
|
|
|
} SHA1Context;
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
@ -75,8 +82,8 @@ enum
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
int SHA1Reset (SHA1Context *);
|
|
|
|
|
int SHA1Input (SHA1Context *, const uint8_t *, unsigned int);
|
|
|
|
|
int SHA1Result (SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
|
|
|
|
|
int SHA1Input (SHA1Context *, const uint8 *, unsigned int);
|
|
|
|
|
int SHA1Result (SHA1Context *, uint8 Message_Digest[SHA1HashSize]);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Functions
|
|
|
|
@ -86,7 +93,7 @@ CHashKey getSHA1(const uint8 *buffer, uint32 size)
|
|
|
|
|
{
|
|
|
|
|
SHA1Context sha;
|
|
|
|
|
int err;
|
|
|
|
|
uint8_t Message_Digest[20];
|
|
|
|
|
uint8 Message_Digest[20];
|
|
|
|
|
|
|
|
|
|
err = SHA1Reset(&sha);
|
|
|
|
|
if (err)
|
|
|
|
@ -95,7 +102,7 @@ CHashKey getSHA1(const uint8 *buffer, uint32 size)
|
|
|
|
|
return CHashKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = SHA1Input(&sha, (const uint8_t*)buffer, size);
|
|
|
|
|
err = SHA1Input(&sha, (const uint8*)buffer, size);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
nlwarning ("SHA: SHA1Input Error %d.\n", err);
|
|
|
|
@ -117,7 +124,7 @@ CHashKey getSHA1(const string &filename, bool forcePath)
|
|
|
|
|
{
|
|
|
|
|
SHA1Context sha;
|
|
|
|
|
int err;
|
|
|
|
|
uint8_t Message_Digest[20];
|
|
|
|
|
uint8 Message_Digest[20];
|
|
|
|
|
|
|
|
|
|
//printf("reading '%s'\n", findData.cFileName);
|
|
|
|
|
CIFile ifile;
|
|
|
|
@ -168,50 +175,7 @@ CHashKey getSHA1(const string &filename, bool forcePath)
|
|
|
|
|
return CHashKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHashKey hk(Message_Digest);
|
|
|
|
|
return hk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Begin calculating a SHA1 hash
|
|
|
|
|
bool resetSHA1(SHA1Context *context)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
err = SHA1Reset(context);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
nlwarning("SHA: SHA1Reset Error %d.\n", err );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add data to a SHA1 hash calculation
|
|
|
|
|
bool inputSHA1(SHA1Context *context, const uint8 *buffer, uint32 size)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
err = SHA1Input(context, buffer, size);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
nlwarning ("SHA: SHA1Input Error %d.\n", err);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the SHA1 result
|
|
|
|
|
CHashKey getSHA1(SHA1Context *context)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
uint8_t messageDigest[20];
|
|
|
|
|
|
|
|
|
|
err = SHA1Result(context, messageDigest);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
nlwarning("SHA: SHA1Result Error %d, could not compute message digest.\n", err );
|
|
|
|
|
return CHashKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHashKey hk(messageDigest);
|
|
|
|
|
CHashKey hk (Message_Digest);
|
|
|
|
|
return hk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -225,9 +189,9 @@ CHashKey getHMacSHA1(const uint8 *text, uint32 text_len, const uint8 *key, uint3
|
|
|
|
|
{
|
|
|
|
|
SHA1Context sha;
|
|
|
|
|
|
|
|
|
|
uint8_t SHA1_Key[64];
|
|
|
|
|
uint8_t SHA1_Key1[20];
|
|
|
|
|
uint8_t SHA1_Key2[20];
|
|
|
|
|
uint8 SHA1_Key[64];
|
|
|
|
|
uint8 SHA1_Key1[20];
|
|
|
|
|
uint8 SHA1_Key2[20];
|
|
|
|
|
|
|
|
|
|
string buffer1;
|
|
|
|
|
string buffer2;
|
|
|
|
@ -238,9 +202,9 @@ CHashKey getHMacSHA1(const uint8 *text, uint32 text_len, const uint8 *key, uint3
|
|
|
|
|
|
|
|
|
|
// If lenght of key > 64 use sha1 hash
|
|
|
|
|
if (key_len > 64) {
|
|
|
|
|
uint8_t SHA1_Key0[20];
|
|
|
|
|
uint8 SHA1_Key0[20];
|
|
|
|
|
SHA1Reset(&sha);
|
|
|
|
|
SHA1Input(&sha, (const uint8_t*)key, key_len);
|
|
|
|
|
SHA1Input(&sha, (const uint8*)key, key_len);
|
|
|
|
|
SHA1Result(&sha, SHA1_Key0);
|
|
|
|
|
CHashKey hk0 (SHA1_Key0);
|
|
|
|
|
for (uint i = 0; i < 20; i++)
|
|
|
|
@ -260,7 +224,7 @@ CHashKey getHMacSHA1(const uint8 *text, uint32 text_len, const uint8 *key, uint3
|
|
|
|
|
|
|
|
|
|
// Get hash
|
|
|
|
|
SHA1Reset(&sha);
|
|
|
|
|
SHA1Input(&sha, (const uint8_t*)buffer1.c_str(), (uint)buffer1.size());
|
|
|
|
|
SHA1Input(&sha, (const uint8*)buffer1.c_str(), (uint)buffer1.size());
|
|
|
|
|
SHA1Result(&sha, SHA1_Key1);
|
|
|
|
|
CHashKey hk1 (SHA1_Key1);
|
|
|
|
|
|
|
|
|
@ -274,7 +238,7 @@ CHashKey getHMacSHA1(const uint8 *text, uint32 text_len, const uint8 *key, uint3
|
|
|
|
|
|
|
|
|
|
// Get new hash
|
|
|
|
|
SHA1Reset(&sha);
|
|
|
|
|
SHA1Input(&sha, (const uint8_t*)buffer2.c_str(), (uint)buffer2.size());
|
|
|
|
|
SHA1Input(&sha, (const uint8*)buffer2.c_str(), (uint)buffer2.size());
|
|
|
|
|
SHA1Result(&sha, SHA1_Key2);
|
|
|
|
|
CHashKey hk (SHA1_Key2);
|
|
|
|
|
|
|
|
|
@ -354,7 +318,7 @@ int SHA1Reset(SHA1Context *context)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
int SHA1Result( SHA1Context *context,
|
|
|
|
|
uint8_t Message_Digest[SHA1HashSize])
|
|
|
|
|
uint8 Message_Digest[SHA1HashSize])
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
@ -384,7 +348,7 @@ int SHA1Result( SHA1Context *context,
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < SHA1HashSize; ++i)
|
|
|
|
|
{
|
|
|
|
|
Message_Digest[i] = uint8_t((context->Intermediate_Hash[i>>2]
|
|
|
|
|
Message_Digest[i] = uint8((context->Intermediate_Hash[i>>2]
|
|
|
|
|
>> 8 * ( 3 - ( i & 0x03 ) ) ) & 0xff );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -412,7 +376,7 @@ int SHA1Result( SHA1Context *context,
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
int SHA1Input( SHA1Context *context,
|
|
|
|
|
const uint8_t *message_array,
|
|
|
|
|
const uint8 *message_array,
|
|
|
|
|
unsigned length)
|
|
|
|
|
{
|
|
|
|
|
if (!length)
|
|
|
|
@ -486,16 +450,16 @@ int SHA1Input( SHA1Context *context,
|
|
|
|
|
*/
|
|
|
|
|
void SHA1ProcessMessageBlock(SHA1Context *context)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t K[] = { /* Constants defined in SHA-1 */
|
|
|
|
|
const uint32 K[] = { /* Constants defined in SHA-1 */
|
|
|
|
|
0x5A827999,
|
|
|
|
|
0x6ED9EBA1,
|
|
|
|
|
0x8F1BBCDC,
|
|
|
|
|
0xCA62C1D6
|
|
|
|
|
};
|
|
|
|
|
int t; /* Loop counter */
|
|
|
|
|
uint32_t temp; /* Temporary word value */
|
|
|
|
|
uint32_t W[80]; /* Word sequence */
|
|
|
|
|
uint32_t A, B, C, D, E; /* Word buffers */
|
|
|
|
|
uint32 temp; /* Temporary word value */
|
|
|
|
|
uint32 W[80]; /* Word sequence */
|
|
|
|
|
uint32 A, B, C, D, E; /* Word buffers */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Initialize the first 16 words in the array W
|
|
|
|
@ -631,14 +595,14 @@ void SHA1PadMessage(SHA1Context *context)
|
|
|
|
|
/*
|
|
|
|
|
* Store the message length as the last 8 octets
|
|
|
|
|
*/
|
|
|
|
|
context->Message_Block[56] = uint8_t((context->Length_High >> 24)&0xff);
|
|
|
|
|
context->Message_Block[57] = uint8_t((context->Length_High >> 16)&0xff);
|
|
|
|
|
context->Message_Block[58] = uint8_t((context->Length_High >> 8)&0xff);
|
|
|
|
|
context->Message_Block[59] = uint8_t((context->Length_High)&0xff);
|
|
|
|
|
context->Message_Block[60] = uint8_t((context->Length_Low >> 24)&0xff);
|
|
|
|
|
context->Message_Block[61] = uint8_t((context->Length_Low >> 16)&0xff);
|
|
|
|
|
context->Message_Block[62] = uint8_t((context->Length_Low >> 8)&0xff);
|
|
|
|
|
context->Message_Block[63] = uint8_t((context->Length_Low)&0xff);
|
|
|
|
|
context->Message_Block[56] = uint8((context->Length_High >> 24)&0xff);
|
|
|
|
|
context->Message_Block[57] = uint8((context->Length_High >> 16)&0xff);
|
|
|
|
|
context->Message_Block[58] = uint8((context->Length_High >> 8)&0xff);
|
|
|
|
|
context->Message_Block[59] = uint8((context->Length_High)&0xff);
|
|
|
|
|
context->Message_Block[60] = uint8((context->Length_Low >> 24)&0xff);
|
|
|
|
|
context->Message_Block[61] = uint8((context->Length_Low >> 16)&0xff);
|
|
|
|
|
context->Message_Block[62] = uint8((context->Length_Low >> 8)&0xff);
|
|
|
|
|
context->Message_Block[63] = uint8((context->Length_Low)&0xff);
|
|
|
|
|
|
|
|
|
|
SHA1ProcessMessageBlock(context);
|
|
|
|
|
}
|
|
|
|
|