Browse Source
Refactor hash digest code --HG-- rename : Sources/Plasma/PubUtilLib/plMath/plRandom.h => Sources/Plasma/NucleusLib/pnEncryption/plRandom.h
Adam Johnson
13 years ago
55 changed files with 976 additions and 726 deletions
@ -0,0 +1,19 @@
|
||||
include_directories("../../Apps") |
||||
include_directories("../../CoreLib") |
||||
include_directories("../../FeatureLib/inc") |
||||
include_directories("../../FeatureLib") |
||||
include_directories("../../NucleusLib/inc") |
||||
include_directories("../../NucleusLib") |
||||
include_directories("../../PubUtilLib/inc") |
||||
include_directories("../../PubUtilLib") |
||||
include_directories(${OPENSSL_INCLUDE_DIR}) |
||||
|
||||
set(plMD5_SOURCES |
||||
Main.cpp |
||||
) |
||||
|
||||
add_executable(plMD5 ${plMD5_SOURCES}) |
||||
target_link_libraries(plMD5 CoreLib pnUtils pnProduct pnEncryption) |
||||
target_link_libraries(plMD5 ${OPENSSL_LIBRARIES}) |
||||
|
||||
source_group("Source Files" FILES ${plMD5_SOURCES}) |
@ -0,0 +1,19 @@
|
||||
include_directories("../../Apps") |
||||
include_directories("../../CoreLib") |
||||
include_directories("../../FeatureLib/inc") |
||||
include_directories("../../FeatureLib") |
||||
include_directories("../../NucleusLib/inc") |
||||
include_directories("../../NucleusLib") |
||||
include_directories("../../PubUtilLib/inc") |
||||
include_directories("../../PubUtilLib") |
||||
include_directories(${OPENSSL_INCLUDE_DIR}) |
||||
|
||||
set(plSHA_SOURCES |
||||
Main.cpp |
||||
) |
||||
|
||||
add_executable(plSHA ${plSHA_SOURCES}) |
||||
target_link_libraries(plSHA CoreLib pnUtils pnProduct pnEncryption) |
||||
target_link_libraries(plSHA ${OPENSSL_LIBRARIES}) |
||||
|
||||
source_group("Source Files" FILES ${plSHA_SOURCES}) |
@ -0,0 +1,20 @@
|
||||
include_directories("../../CoreLib") |
||||
include_directories("../../NucleusLib") |
||||
include_directories(${OPENSSL_INCLUDE_DIR}) |
||||
|
||||
set(pnEncryption_SOURCES |
||||
plChallengeHash.cpp |
||||
plChecksum.cpp |
||||
) |
||||
|
||||
set(pnEncryption_HEADERS |
||||
plChallengeHash.h |
||||
plChecksum.h |
||||
plRandom.h |
||||
) |
||||
|
||||
add_library(pnEncryption STATIC ${pnEncryption_SOURCES} ${pnEncryption_HEADERS}) |
||||
target_link_libraries(pnEncryption ${OPENSSL_LIBRARIES}) |
||||
|
||||
source_group("Source Files" FILES ${pnEncryption_SOURCES}) |
||||
source_group("Header Files" FILES ${pnEncryption_HEADERS}) |
@ -0,0 +1,119 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7 |
||||
|
||||
If you modify this Program, or any covered work, by linking or |
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
||||
(or a modified version of those libraries), |
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
||||
licensors of this Program grant you additional |
||||
permission to convey the resulting work. Corresponding Source for a |
||||
non-source form of such a combination shall include the source code for |
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
||||
work. |
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
|
||||
#include "plChallengeHash.h" |
||||
#include "pnUtils/pnUtils.h" |
||||
|
||||
ShaDigest fSeed; |
||||
|
||||
void CryptCreateRandomSeed(size_t length, uint8_t* data) |
||||
{ |
||||
uint32_t seedIdx = 0; |
||||
uint32_t dataIdx = 0; |
||||
uint32_t cur = 0; |
||||
uint32_t end = max(length, sizeof(ShaDigest)); |
||||
|
||||
// Combine seed with input data
|
||||
for (; cur < end; cur++) { |
||||
fSeed[seedIdx] ^= data[dataIdx]; |
||||
|
||||
if (++seedIdx >= sizeof(ShaDigest)) |
||||
seedIdx = 0; |
||||
if (++dataIdx >= length) |
||||
dataIdx = 0; |
||||
} |
||||
|
||||
((uint32_t*)fSeed)[2] ^= (uint32_t)((uintptr_t)&length); |
||||
((uint32_t*)fSeed)[3] ^= (uint32_t)length; |
||||
((uint32_t*)fSeed)[4] ^= (uint32_t)((uintptr_t)data); |
||||
|
||||
// Hash seed
|
||||
plSHAChecksum sum(sizeof(ShaDigest), (uint8_t*)fSeed); |
||||
ShaDigest digest; |
||||
memcpy(digest, sum.GetValue(), sizeof(ShaDigest)); |
||||
|
||||
seedIdx = 0; |
||||
dataIdx = 0; |
||||
cur = 0; |
||||
|
||||
// Update output with contents of digest
|
||||
for (; cur < end; cur++) { |
||||
data[dataIdx] ^= digest[seedIdx]; |
||||
|
||||
if (++seedIdx >= sizeof(ShaDigest)) |
||||
seedIdx = 0; |
||||
if (++dataIdx >= length) |
||||
dataIdx = 0; |
||||
} |
||||
|
||||
// Combine seed with digest
|
||||
for (size_t i = 0; i < sizeof(ShaDigest); i++) { |
||||
fSeed[i] ^= digest[i]; |
||||
} |
||||
} |
||||
|
||||
void CryptHashPassword(const plString& username, const plString& password, ShaDigest dest) |
||||
{ |
||||
/* This should be unnecessary once plString has ToLower() */ |
||||
wchar_t* w_name = (wchar_t*)_TEMP_CONVERT_TO_WCHAR_T(username); |
||||
StrLower(w_name); |
||||
|
||||
plString buf = password; |
||||
buf += _TEMP_CONVERT_FROM_WCHAR_T(w_name); |
||||
|
||||
plSHAChecksum sum(buf.GetSize() * sizeof(wchar_t), (uint8_t*)_TEMP_CONVERT_TO_WCHAR_T(buf)); |
||||
|
||||
memcpy(dest, sum.GetValue(), sizeof(ShaDigest)); |
||||
} |
||||
|
||||
void CryptHashPasswordChallenge(uint32_t clientChallenge, uint32_t serverChallenge, ShaDigest namePassHash, ShaDigest challengeHash) |
||||
{ |
||||
plSHAChecksum sum; |
||||
|
||||
sum.Start(); |
||||
sum.AddTo(sizeof(uint32_t), (uint8_t*)&clientChallenge); |
||||
sum.AddTo(sizeof(uint32_t), (uint8_t*)&serverChallenge); |
||||
sum.AddTo(sizeof(ShaDigest), namePassHash); |
||||
sum.Finish(); |
||||
|
||||
memcpy(challengeHash, sum.GetValue(), sizeof(ShaDigest)); |
||||
} |
@ -0,0 +1,482 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7 |
||||
|
||||
If you modify this Program, or any covered work, by linking or |
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
||||
(or a modified version of those libraries), |
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
||||
licensors of this Program grant you additional |
||||
permission to convey the resulting work. Corresponding Source for a |
||||
non-source form of such a combination shall include the source code for |
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
||||
work. |
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
#include "plChecksum.h" |
||||
#include "hsStream.h" |
||||
|
||||
#include <cstring> |
||||
|
||||
|
||||
static uint8_t IHexCharToInt(char c) |
||||
{ |
||||
switch( c ) |
||||
{ |
||||
// yes, it's ugly, but it'll be fast :)
|
||||
case '0': return 0; |
||||
case '1': return 1; |
||||
case '2': return 2; |
||||
case '3': return 3; |
||||
case '4': return 4; |
||||
case '5': return 5; |
||||
case '6': return 6; |
||||
case '7': return 7; |
||||
case '8': return 8; |
||||
case '9': return 9; |
||||
|
||||
case 'a': return 10; |
||||
case 'b': return 11; |
||||
case 'c': return 12; |
||||
case 'd': return 13; |
||||
case 'e': return 14; |
||||
case 'f': return 15; |
||||
|
||||
case 'A': return 10; |
||||
case 'B': return 11; |
||||
case 'C': return 12; |
||||
case 'D': return 13; |
||||
case 'E': return 14; |
||||
case 'F': return 15; |
||||
} |
||||
|
||||
return 0xff; |
||||
} |
||||
|
||||
plChecksum::plChecksum(unsigned int bufsize, const char* buffer) |
||||
{ |
||||
unsigned int wndsz = GetWindowSize(),i = 0; |
||||
fSum = 0; |
||||
|
||||
const char* bufferAbsEnd = buffer + bufsize; |
||||
const char* bufferEnvenEnd = buffer + bufsize - (bufsize % wndsz); |
||||
|
||||
while (buffer < bufferEnvenEnd) |
||||
{ |
||||
fSum += hsToLE32(*((SumStorage*)buffer)); |
||||
buffer += wndsz; |
||||
} |
||||
|
||||
SumStorage last = 0; |
||||
while (buffer < bufferAbsEnd) |
||||
{ |
||||
((char*)&last)[i % wndsz] = *buffer; |
||||
buffer++; |
||||
} |
||||
fSum+= hsToLE32(last); |
||||
} |
||||
|
||||
//============================================================================
|
||||
|
||||
plMD5Checksum::plMD5Checksum(size_t size, uint8_t *buffer ) |
||||
{ |
||||
fValid = false; |
||||
Start(); |
||||
AddTo( size, buffer ); |
||||
Finish(); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum() |
||||
{ |
||||
Clear(); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( const plMD5Checksum &rhs ) |
||||
{ |
||||
memcpy( fChecksum, rhs.fChecksum, sizeof( fChecksum ) ); |
||||
fValid = rhs.fValid; |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( const char *fileName ) |
||||
{ |
||||
CalcFromFile( fileName ); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( hsStream* stream ) |
||||
{ |
||||
CalcFromStream(stream); |
||||
} |
||||
|
||||
void plMD5Checksum::Clear() |
||||
{ |
||||
memset( fChecksum, 0, sizeof( fChecksum ) ); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plMD5Checksum::CalcFromFile( const char *fileName ) |
||||
{ |
||||
hsUNIXStream s; |
||||
fValid = false; |
||||
|
||||
if( s.Open(fileName) ) |
||||
{ |
||||
CalcFromStream(&s); |
||||
s.Close(); |
||||
} |
||||
} |
||||
|
||||
void plMD5Checksum::CalcFromStream( hsStream* stream ) |
||||
{ |
||||
uint32_t sPos = stream->GetPosition(); |
||||
unsigned loadLen = 1024 * 1024; |
||||
Start(); |
||||
|
||||
uint8_t *buf = new uint8_t[loadLen]; |
||||
|
||||
while(int read = stream->Read(loadLen, buf)) |
||||
AddTo( read, buf ); |
||||
delete[] buf; |
||||
|
||||
Finish(); |
||||
stream->SetPosition(sPos); |
||||
} |
||||
|
||||
void plMD5Checksum::Start() |
||||
{ |
||||
MD5_Init( &fContext ); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plMD5Checksum::AddTo(size_t size, const uint8_t *buffer ) |
||||
{ |
||||
MD5_Update( &fContext, buffer, size ); |
||||
} |
||||
|
||||
void plMD5Checksum::Finish() |
||||
{ |
||||
MD5_Final( fChecksum, &fContext ); |
||||
fValid = true; |
||||
} |
||||
|
||||
const char *plMD5Checksum::GetAsHexString() const |
||||
{ |
||||
const int kHexStringSize = ( 2 * MD5_DIGEST_LENGTH ) + 1; |
||||
static char tempString[ kHexStringSize ]; |
||||
|
||||
int i; |
||||
char *ptr; |
||||
|
||||
|
||||
hsAssert( fValid, "Trying to get string version of invalid checksum" ); |
||||
|
||||
for( i = 0, ptr = tempString; i < sizeof( fChecksum ); i++, ptr += 2 ) |
||||
sprintf( ptr, "%02x", fChecksum[ i ] ); |
||||
|
||||
*ptr = 0; |
||||
|
||||
return tempString; |
||||
} |
||||
|
||||
void plMD5Checksum::SetFromHexString( const char *string ) |
||||
{ |
||||
const char *ptr; |
||||
int i; |
||||
|
||||
|
||||
hsAssert( strlen( string ) == 2 * MD5_DIGEST_LENGTH, "Invalid string in MD5Checksum Set()" ); |
||||
|
||||
for( i = 0, ptr = string; i < sizeof( fChecksum ); i++, ptr += 2 ) |
||||
fChecksum[ i ] = ( IHexCharToInt( ptr[ 0 ] ) << 4 ) | IHexCharToInt( ptr[ 1 ] ); |
||||
|
||||
fValid = true; |
||||
} |
||||
|
||||
void plMD5Checksum::SetValue(uint8_t* checksum) |
||||
{ |
||||
fValid = true; |
||||
memcpy(fChecksum, checksum, sizeof(fChecksum)); |
||||
} |
||||
|
||||
bool plMD5Checksum::operator==( const plMD5Checksum &rhs ) const |
||||
{ |
||||
return (fValid && rhs.fValid && memcmp(fChecksum, rhs.fChecksum, sizeof(fChecksum)) == 0); |
||||
} |
||||
|
||||
//============================================================================
|
||||
|
||||
plSHAChecksum::plSHAChecksum(size_t size, uint8_t *buffer ) |
||||
{ |
||||
fValid = false; |
||||
Start(); |
||||
AddTo(size, buffer); |
||||
Finish(); |
||||
} |
||||
|
||||
plSHAChecksum::plSHAChecksum() |
||||
{ |
||||
Clear(); |
||||
} |
||||
|
||||
plSHAChecksum::plSHAChecksum(const plSHAChecksum& rhs) |
||||
{ |
||||
memcpy(fChecksum, rhs.fChecksum, sizeof(fChecksum)); |
||||
fValid = rhs.fValid; |
||||
} |
||||
|
||||
plSHAChecksum::plSHAChecksum(const char* fileName) |
||||
{ |
||||
CalcFromFile(fileName); |
||||
} |
||||
|
||||
plSHAChecksum::plSHAChecksum(hsStream* stream) |
||||
{ |
||||
CalcFromStream(stream); |
||||
} |
||||
|
||||
void plSHAChecksum::Clear() |
||||
{ |
||||
memset(fChecksum, 0, sizeof(fChecksum)); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plSHAChecksum::CalcFromFile(const char* fileName) |
||||
{ |
||||
hsUNIXStream s; |
||||
fValid = false; |
||||
|
||||
if (s.Open(fileName)) |
||||
{ |
||||
CalcFromStream(&s); |
||||
s.Close(); |
||||
} |
||||
} |
||||
|
||||
void plSHAChecksum::CalcFromStream(hsStream* stream) |
||||
{ |
||||
uint32_t sPos = stream->GetPosition(); |
||||
unsigned loadLen = 1024 * 1024; |
||||
Start(); |
||||
|
||||
uint8_t* buf = new uint8_t[loadLen]; |
||||
|
||||
while (int read = stream->Read(loadLen, buf)) |
||||
{ |
||||
AddTo( read, buf ); |
||||
} |
||||
delete[] buf; |
||||
|
||||
Finish(); |
||||
stream->SetPosition(sPos); |
||||
} |
||||
|
||||
void plSHAChecksum::Start() |
||||
{ |
||||
SHA_Init(&fContext); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plSHAChecksum::AddTo(size_t size, const uint8_t* buffer) |
||||
{ |
||||
SHA_Update(&fContext, buffer, size); |
||||
} |
||||
|
||||
void plSHAChecksum::Finish() |
||||
{ |
||||
SHA_Final(fChecksum, &fContext); |
||||
fValid = true; |
||||
} |
||||
|
||||
const char *plSHAChecksum::GetAsHexString() const |
||||
{ |
||||
const int kHexStringSize = (2 * SHA_DIGEST_LENGTH) + 1; |
||||
static char tempString[kHexStringSize]; |
||||
|
||||
int i; |
||||
char* ptr; |
||||
|
||||
hsAssert(fValid, "Trying to get string version of invalid checksum"); |
||||
|
||||
for (i = 0, ptr = tempString; i < sizeof(fChecksum); i++, ptr += 2) |
||||
sprintf(ptr, "%02x", fChecksum[i]); |
||||
|
||||
*ptr = 0; |
||||
|
||||
return tempString; |
||||
} |
||||
|
||||
void plSHAChecksum::SetFromHexString(const char* string) |
||||
{ |
||||
const char* ptr; |
||||
int i; |
||||
|
||||
hsAssert(strlen(string) == (2 * SHA_DIGEST_LENGTH), "Invalid string in SHAChecksum Set()"); |
||||
|
||||
for (i = 0, ptr = string; i < sizeof(fChecksum); i++, ptr += 2) |
||||
fChecksum[i] = (IHexCharToInt(ptr[0]) << 4) | IHexCharToInt(ptr[1]); |
||||
|
||||
fValid = true; |
||||
} |
||||
|
||||
void plSHAChecksum::SetValue(uint8_t* checksum) |
||||
{ |
||||
fValid = true; |
||||
memcpy(fChecksum, checksum, sizeof(fChecksum)); |
||||
} |
||||
|
||||
bool plSHAChecksum::operator==(const plSHAChecksum& rhs) const |
||||
{ |
||||
return (fValid && rhs.fValid && memcmp(fChecksum, rhs.fChecksum, sizeof(fChecksum)) == 0); |
||||
} |
||||
|
||||
//============================================================================
|
||||
|
||||
plSHA1Checksum::plSHA1Checksum(size_t size, uint8_t *buffer ) |
||||
{ |
||||
fValid = false; |
||||
Start(); |
||||
AddTo(size, buffer); |
||||
Finish(); |
||||
} |
||||
|
||||
plSHA1Checksum::plSHA1Checksum() |
||||
{ |
||||
Clear(); |
||||
} |
||||
|
||||
plSHA1Checksum::plSHA1Checksum(const plSHA1Checksum& rhs) |
||||
{ |
||||
memcpy(fChecksum, rhs.fChecksum, sizeof(fChecksum)); |
||||
fValid = rhs.fValid; |
||||
} |
||||
|
||||
plSHA1Checksum::plSHA1Checksum(const char* fileName) |
||||
{ |
||||
CalcFromFile(fileName); |
||||
} |
||||
|
||||
plSHA1Checksum::plSHA1Checksum(hsStream* stream) |
||||
{ |
||||
CalcFromStream(stream); |
||||
} |
||||
|
||||
void plSHA1Checksum::Clear() |
||||
{ |
||||
memset(fChecksum, 0, sizeof(fChecksum)); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plSHA1Checksum::CalcFromFile(const char* fileName) |
||||
{ |
||||
hsUNIXStream s; |
||||
fValid = false; |
||||
|
||||
if (s.Open(fileName)) |
||||
{ |
||||
CalcFromStream(&s); |
||||
s.Close(); |
||||
} |
||||
} |
||||
|
||||
void plSHA1Checksum::CalcFromStream(hsStream* stream) |
||||
{ |
||||
uint32_t sPos = stream->GetPosition(); |
||||
unsigned loadLen = 1024 * 1024; |
||||
Start(); |
||||
|
||||
uint8_t* buf = new uint8_t[loadLen]; |
||||
|
||||
while (int read = stream->Read(loadLen, buf)) |
||||
{ |
||||
AddTo( read, buf ); |
||||
} |
||||
delete[] buf; |
||||
|
||||
Finish(); |
||||
stream->SetPosition(sPos); |
||||
} |
||||
|
||||
void plSHA1Checksum::Start() |
||||
{ |
||||
SHA1_Init(&fContext); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plSHA1Checksum::AddTo(size_t size, const uint8_t* buffer) |
||||
{ |
||||
SHA1_Update(&fContext, buffer, size); |
||||
} |
||||
|
||||
void plSHA1Checksum::Finish() |
||||
{ |
||||
SHA1_Final(fChecksum, &fContext); |
||||
fValid = true; |
||||
} |
||||
|
||||
const char *plSHA1Checksum::GetAsHexString() const |
||||
{ |
||||
const int kHexStringSize = (2 * SHA_DIGEST_LENGTH) + 1; |
||||
static char tempString[kHexStringSize]; |
||||
|
||||
int i; |
||||
char* ptr; |
||||
|
||||
hsAssert(fValid, "Trying to get string version of invalid checksum"); |
||||
|
||||
for (i = 0, ptr = tempString; i < sizeof(fChecksum); i++, ptr += 2) |
||||
sprintf(ptr, "%02x", fChecksum[i]); |
||||
|
||||
*ptr = 0; |
||||
|
||||
return tempString; |
||||
} |
||||
|
||||
void plSHA1Checksum::SetFromHexString(const char* string) |
||||
{ |
||||
const char* ptr; |
||||
int i; |
||||
|
||||
hsAssert(strlen(string) == (2 * SHA_DIGEST_LENGTH), "Invalid string in SHA1Checksum Set()"); |
||||
|
||||
for (i = 0, ptr = string; i < sizeof(fChecksum); i++, ptr += 2) |
||||
fChecksum[i] = (IHexCharToInt(ptr[0]) << 4) | IHexCharToInt(ptr[1]); |
||||
|
||||
fValid = true; |
||||
} |
||||
|
||||
void plSHA1Checksum::SetValue(uint8_t* checksum) |
||||
{ |
||||
fValid = true; |
||||
memcpy(fChecksum, checksum, sizeof(fChecksum)); |
||||
} |
||||
|
||||
bool plSHA1Checksum::operator==(const plSHA1Checksum& rhs) const |
||||
{ |
||||
return (fValid && rhs.fValid && memcmp(fChecksum, rhs.fChecksum, sizeof(fChecksum)) == 0); |
||||
} |
||||
|
@ -0,0 +1,187 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7 |
||||
|
||||
If you modify this Program, or any covered work, by linking or |
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
||||
(or a modified version of those libraries), |
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
||||
licensors of this Program grant you additional |
||||
permission to convey the resulting work. Corresponding Source for a |
||||
non-source form of such a combination shall include the source code for |
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
||||
work. |
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
#ifndef PL_CHECKSUM_H |
||||
#define PL_CHECKSUM_H |
||||
|
||||
#include "HeadSpin.h" |
||||
#include <openssl/md5.h> |
||||
#include <openssl/sha.h> |
||||
|
||||
class plChecksum |
||||
{ |
||||
public: |
||||
typedef uint32_t SumStorage; |
||||
private: |
||||
SumStorage fSum; |
||||
public: |
||||
plChecksum(unsigned int bufsize, const char* buffer); |
||||
static int GetChecksumSize() { return sizeof(SumStorage); } |
||||
static int GetWindowSize() { return sizeof(SumStorage); } |
||||
SumStorage GetChecksum() { return fSum; } |
||||
}; |
||||
|
||||
class hsStream; |
||||
|
||||
class plMD5Checksum |
||||
{ |
||||
protected: |
||||
hsBool fValid; |
||||
MD5_CTX fContext; |
||||
uint8_t fChecksum[MD5_DIGEST_LENGTH]; |
||||
|
||||
public: |
||||
plMD5Checksum(size_t size, uint8_t *buffer); |
||||
plMD5Checksum(); |
||||
plMD5Checksum(const plMD5Checksum &rhs); |
||||
plMD5Checksum(const char *fileName); |
||||
plMD5Checksum(hsStream* stream); |
||||
|
||||
hsBool IsValid() const { return fValid; } |
||||
void Clear(); |
||||
|
||||
void CalcFromFile(const char *fileName); |
||||
void CalcFromStream(hsStream* stream); |
||||
|
||||
void Start(); |
||||
void AddTo(size_t size, const uint8_t *buffer); |
||||
void Finish(); |
||||
|
||||
const uint8_t *GetValue() const { return fChecksum; } |
||||
size_t GetSize() const { return sizeof(fChecksum); } |
||||
|
||||
// Backdoor for cached checksums (ie, if you loaded it off disk)
|
||||
void SetValue(uint8_t* checksum); |
||||
|
||||
// Note: GetAsHexString() returns a pointer to a static string;
|
||||
// do not rely on the contents of this string between calls!
|
||||
const char *GetAsHexString() const; |
||||
void SetFromHexString( const char *string ); |
||||
|
||||
bool operator==(const plMD5Checksum &rhs) const; |
||||
bool operator!=(const plMD5Checksum &rhs) const { return !operator==(rhs); } |
||||
}; |
||||
|
||||
/* A bunch of things might store either a SHA or a SHA1 checksum, this provides
|
||||
* them a way to store the checksum itself, rather than a union of the classes. |
||||
*/ |
||||
typedef uint8_t ShaDigest[SHA_DIGEST_LENGTH]; |
||||
|
||||
class plSHAChecksum |
||||
{ |
||||
protected: |
||||
hsBool fValid; |
||||
SHA_CTX fContext; |
||||
ShaDigest fChecksum; |
||||
|
||||
public: |
||||
plSHAChecksum(size_t size, uint8_t* buffer); |
||||
plSHAChecksum(); |
||||
plSHAChecksum(const plSHAChecksum& rhs); |
||||
plSHAChecksum(const char* fileName); |
||||
plSHAChecksum(hsStream* stream); |
||||
|
||||
hsBool IsValid() const { return fValid; } |
||||
void Clear(); |
||||
|
||||
void CalcFromFile(const char* fileName); |
||||
void CalcFromStream(hsStream* stream); |
||||
|
||||
void Start(); |
||||
void AddTo(size_t size, const uint8_t* buffer); |
||||
void Finish(); |
||||
|
||||
const uint8_t* GetValue() const { return fChecksum; } |
||||
size_t GetSize() const { return sizeof(fChecksum); } |
||||
|
||||
// Backdoor for cached checksums (ie, if you loaded it off disk)
|
||||
void SetValue(uint8_t* checksum); |
||||
|
||||
// Note: GetAsHexString() returns a pointer to a static string;
|
||||
// do not rely on the contents of this string between calls!
|
||||
const char* GetAsHexString() const; |
||||
void SetFromHexString(const char* string); |
||||
|
||||
bool operator==(const plSHAChecksum& rhs) const; |
||||
bool operator!=(const plSHAChecksum& rhs) const { return !operator==(rhs); } |
||||
}; |
||||
|
||||
class plSHA1Checksum |
||||
{ |
||||
protected: |
||||
hsBool fValid; |
||||
SHA_CTX fContext; |
||||
ShaDigest fChecksum; |
||||
|
||||
public: |
||||
plSHA1Checksum(size_t size, uint8_t* buffer); |
||||
plSHA1Checksum(); |
||||
plSHA1Checksum(const plSHA1Checksum& rhs); |
||||
plSHA1Checksum(const char* fileName); |
||||
plSHA1Checksum(hsStream* stream); |
||||
|
||||
hsBool IsValid() const { return fValid; } |
||||
void Clear(); |
||||
|
||||
void CalcFromFile(const char* fileName); |
||||
void CalcFromStream(hsStream* stream); |
||||
|
||||
void Start(); |
||||
void AddTo(size_t size, const uint8_t* buffer); |
||||
void Finish(); |
||||
|
||||
const uint8_t* GetValue() const { return fChecksum; } |
||||
size_t GetSize() const { return sizeof(fChecksum); } |
||||
|
||||
// Backdoor for cached checksums (ie, if you loaded it off disk)
|
||||
void SetValue(uint8_t* checksum); |
||||
|
||||
// Note: GetAsHexString() returns a pointer to a static string;
|
||||
// do not rely on the contents of this string between calls!
|
||||
const char* GetAsHexString() const; |
||||
void SetFromHexString(const char* string); |
||||
|
||||
bool operator==(const plSHA1Checksum& rhs) const; |
||||
bool operator!=(const plSHA1Checksum& rhs) const { return !operator==(rhs); } |
||||
}; |
||||
|
||||
#endif // PL_CHECKSUM_H
|
||||
|
@ -1,16 +0,0 @@
|
||||
include_directories("../../CoreLib") |
||||
include_directories("../../NucleusLib") |
||||
include_directories("../../PubUtilLib") |
||||
|
||||
set(plEncryption_SOURCES |
||||
plChecksum.cpp |
||||
) |
||||
|
||||
set(plEncryption_HEADERS |
||||
plChecksum.h |
||||
) |
||||
|
||||
add_library(plEncryption STATIC ${plEncryption_SOURCES} ${plEncryption_HEADERS}) |
||||
|
||||
source_group("Source Files" FILES ${plEncryption_SOURCES}) |
||||
source_group("Header Files" FILES ${plEncryption_HEADERS}) |
@ -1,226 +0,0 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7 |
||||
|
||||
If you modify this Program, or any covered work, by linking or |
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
||||
(or a modified version of those libraries), |
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
||||
licensors of this Program grant you additional |
||||
permission to convey the resulting work. Corresponding Source for a |
||||
non-source form of such a combination shall include the source code for |
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
||||
work. |
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
#include "plChecksum.h" |
||||
#include "hsStream.h" |
||||
|
||||
#include <cstring> |
||||
|
||||
plChecksum::plChecksum(unsigned int bufsize, const char* buffer) |
||||
{ |
||||
unsigned int wndsz = GetWindowSize(),i = 0; |
||||
fSum = 0; |
||||
|
||||
const char* bufferAbsEnd = buffer + bufsize; |
||||
const char* bufferEnvenEnd = buffer + bufsize - (bufsize % wndsz); |
||||
|
||||
while (buffer < bufferEnvenEnd) |
||||
{ |
||||
fSum += hsToLE32(*((SumStorage*)buffer)); |
||||
buffer += wndsz; |
||||
} |
||||
|
||||
SumStorage last = 0; |
||||
while (buffer < bufferAbsEnd) |
||||
{ |
||||
((char*)&last)[i % wndsz] = *buffer; |
||||
buffer++; |
||||
} |
||||
fSum+= hsToLE32(last); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( uint32_t size, uint8_t *buffer ) |
||||
{ |
||||
fValid = false; |
||||
Start(); |
||||
AddTo( size, buffer ); |
||||
Finish(); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum() |
||||
{ |
||||
Clear(); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( const plMD5Checksum &rhs ) |
||||
{ |
||||
memcpy( fChecksum, rhs.fChecksum, sizeof( fChecksum ) ); |
||||
fValid = rhs.fValid; |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( const char *fileName ) |
||||
{ |
||||
CalcFromFile( fileName ); |
||||
} |
||||
|
||||
plMD5Checksum::plMD5Checksum( hsStream* stream ) |
||||
{ |
||||
CalcFromStream(stream); |
||||
} |
||||
|
||||
void plMD5Checksum::Clear() |
||||
{ |
||||
memset( fChecksum, 0, sizeof( fChecksum ) ); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plMD5Checksum::CalcFromFile( const char *fileName ) |
||||
{ |
||||
hsUNIXStream s; |
||||
fValid = false; |
||||
|
||||
if( s.Open(fileName) ) |
||||
{ |
||||
CalcFromStream(&s); |
||||
s.Close(); |
||||
} |
||||
} |
||||
|
||||
void plMD5Checksum::CalcFromStream( hsStream* stream ) |
||||
{ |
||||
uint32_t sPos = stream->GetPosition(); |
||||
unsigned loadLen = 1024 * 1024; |
||||
Start(); |
||||
|
||||
uint8_t *buf = new uint8_t[loadLen]; |
||||
|
||||
while(int read = stream->Read(loadLen, buf)) |
||||
AddTo( read, buf ); |
||||
delete[] buf; |
||||
|
||||
Finish(); |
||||
stream->SetPosition(sPos); |
||||
} |
||||
|
||||
void plMD5Checksum::Start( void ) |
||||
{ |
||||
MD5_Init( &fContext ); |
||||
fValid = false; |
||||
} |
||||
|
||||
void plMD5Checksum::AddTo( uint32_t size, const uint8_t *buffer ) |
||||
{ |
||||
MD5_Update( &fContext, buffer, size ); |
||||
} |
||||
|
||||
void plMD5Checksum::Finish( void ) |
||||
{ |
||||
MD5_Final( fChecksum, &fContext ); |
||||
fValid = true; |
||||
} |
||||
|
||||
const char *plMD5Checksum::GetAsHexString( void ) const |
||||
{ |
||||
const int kHexStringSize = ( 2 * MD5_DIGEST_LENGTH ) + 1; |
||||
static char tempString[ kHexStringSize ]; |
||||
|
||||
int i; |
||||
char *ptr; |
||||
|
||||
|
||||
hsAssert( fValid, "Trying to get string version of invalid checksum" ); |
||||
|
||||
for( i = 0, ptr = tempString; i < sizeof( fChecksum ); i++, ptr += 2 ) |
||||
sprintf( ptr, "%02x", fChecksum[ i ] ); |
||||
|
||||
*ptr = 0; |
||||
|
||||
return tempString; |
||||
} |
||||
|
||||
uint8_t plMD5Checksum::IHexCharToInt( char c ) const |
||||
{ |
||||
switch( c ) |
||||
{ |
||||
// yes, it's ugly, but it'll be fast :)
|
||||
case '0': return 0; |
||||
case '1': return 1; |
||||
case '2': return 2; |
||||
case '3': return 3; |
||||
case '4': return 4; |
||||
case '5': return 5; |
||||
case '6': return 6; |
||||
case '7': return 7; |
||||
case '8': return 8; |
||||
case '9': return 9; |
||||
|
||||
case 'a': return 10; |
||||
case 'b': return 11; |
||||
case 'c': return 12; |
||||
case 'd': return 13; |
||||
case 'e': return 14; |
||||
case 'f': return 15; |
||||
|
||||
case 'A': return 10; |
||||
case 'B': return 11; |
||||
case 'C': return 12; |
||||
case 'D': return 13; |
||||
case 'E': return 14; |
||||
case 'F': return 15; |
||||
} |
||||
|
||||
return 0xff; |
||||
} |
||||
|
||||
void plMD5Checksum::SetFromHexString( const char *string ) |
||||
{ |
||||
const char *ptr; |
||||
int i; |
||||
|
||||
|
||||
hsAssert( strlen( string ) == 2 * MD5_DIGEST_LENGTH, "Invalid string in MD5Checksum Set()" ); |
||||
|
||||
for( i = 0, ptr = string; i < sizeof( fChecksum ); i++, ptr += 2 ) |
||||
fChecksum[ i ] = ( IHexCharToInt( ptr[ 0 ] ) << 4 ) | IHexCharToInt( ptr[ 1 ] ); |
||||
|
||||
fValid = true; |
||||
} |
||||
|
||||
void plMD5Checksum::SetValue(uint8_t* checksum) |
||||
{ |
||||
fValid = true; |
||||
memcpy(fChecksum, checksum, sizeof(fChecksum)); |
||||
} |
||||
|
||||
bool plMD5Checksum::operator==( const plMD5Checksum &rhs ) const |
||||
{ |
||||
return (fValid && rhs.fValid && memcmp(fChecksum, rhs.fChecksum, sizeof(fChecksum)) == 0); |
||||
} |
@ -1,105 +0,0 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7 |
||||
|
||||
If you modify this Program, or any covered work, by linking or |
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
||||
(or a modified version of those libraries), |
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
||||
licensors of this Program grant you additional |
||||
permission to convey the resulting work. Corresponding Source for a |
||||
non-source form of such a combination shall include the source code for |
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
||||
work. |
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
#ifndef PL_CHECKSUM_H |
||||
#define PL_CHECKSUM_H |
||||
|
||||
#include "HeadSpin.h" |
||||
#include <openssl/md5.h> |
||||
|
||||
class plChecksum |
||||
{ |
||||
public: |
||||
typedef uint32_t SumStorage; |
||||
private: |
||||
SumStorage fSum; |
||||
public: |
||||
plChecksum(unsigned int bufsize, const char* buffer); |
||||
static int GetChecksumSize() { return sizeof(SumStorage); } |
||||
static int GetWindowSize() { return sizeof(SumStorage); } |
||||
SumStorage GetChecksum() { return fSum; } |
||||
}; |
||||
|
||||
class hsStream; |
||||
|
||||
class plMD5Checksum
|
||||
{ |
||||
protected: |
||||
|
||||
hsBool fValid; |
||||
MD5_CTX fContext; |
||||
uint8_t fChecksum[ MD5_DIGEST_LENGTH ]; |
||||
|
||||
uint8_t IHexCharToInt( char c ) const; |
||||
|
||||
public: |
||||
|
||||
plMD5Checksum( uint32_t size, uint8_t *buffer ); |
||||
plMD5Checksum(); |
||||
plMD5Checksum( const plMD5Checksum &rhs ); |
||||
plMD5Checksum( const char *fileName ); |
||||
plMD5Checksum( hsStream* stream ); |
||||
|
||||
hsBool IsValid( void ) const { return fValid; } |
||||
void Clear(); |
||||
|
||||
void CalcFromFile( const char *fileName ); |
||||
void CalcFromStream( hsStream* stream ); |
||||
|
||||
void Start( void ); |
||||
void AddTo( uint32_t size, const uint8_t *buffer ); |
||||
void Finish( void ); |
||||
|
||||
const uint8_t *GetValue( void ) const { return fChecksum; } |
||||
uint32_t GetSize( void ) const { return sizeof( fChecksum ); } |
||||
|
||||
// Backdoor for cached checksums (ie, if you loaded it off disk)
|
||||
void SetValue(uint8_t* checksum); |
||||
|
||||
// Note: GetAsHexString() returns a pointer to a static string; do not rely on the contents of this string between calls!
|
||||
const char *GetAsHexString( void ) const; |
||||
void SetFromHexString( const char *string ); |
||||
|
||||
bool operator==( const plMD5Checksum &rhs ) const; |
||||
bool operator!=( const plMD5Checksum &rhs ) const { return !operator==( rhs ); } |
||||
}; |
||||
#endif // PL_CHECKSUM_H
|
||||
|
Loading…
Reference in new issue