More SHA encoding functions and add missing copyright

This commit is contained in:
Henrique Santiago 2012-08-04 11:37:55 -03:00
parent fb7ab21e71
commit 156ab9b879
6 changed files with 139 additions and 7 deletions

View File

@ -1,3 +1,25 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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 "database.h"
Database::Database()

View File

@ -1,3 +1,25 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef DATABASE_H
#define DATABASE_H

View File

@ -1,3 +1,28 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifdef WIN32
#include <winsock2.h>
#endif
#include "mysql.h"
#include <mysql/errmsg.h>
#include <framework/core/clock.h>

View File

@ -1,11 +1,30 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef DATABASEMYSQL_H
#define DATABASEMYSQL_H
#include "database.h"
#include <framework/global.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#include <mysql/mysql.h>
class DBResult;

View File

@ -178,7 +178,7 @@ std::string Crypt::decrypt(const std::string& encrypted_string)
return std::string();
}
std::string Crypt::md5Encode(std::string decoded_string, bool upperCase)
std::string Crypt::md5Encode(const std::string& decoded_string, bool upperCase)
{
MD5_CTX c;
MD5_Init(&c);
@ -199,7 +199,7 @@ std::string Crypt::md5Encode(std::string decoded_string, bool upperCase)
return result;
}
std::string Crypt::sha1Encode(std::string decoded_string, bool upperCase)
std::string Crypt::sha1Encode(const std::string& decoded_string, bool upperCase)
{
SHA_CTX c;
SHA1_Init(&c);
@ -220,6 +220,48 @@ std::string Crypt::sha1Encode(std::string decoded_string, bool upperCase)
return result;
}
std::string Crypt::sha256Encode(const std::string& decoded_string, bool upperCase)
{
SHA256_CTX c;
SHA256_Init(&c);
SHA256_Update(&c, decoded_string.c_str(), decoded_string.length());
uint8_t md[SHA256_DIGEST_LENGTH];
SHA256_Final(md, &c);
char output[(SHA256_DIGEST_LENGTH << 1) + 1];
for(int32_t i = 0; i < (int32_t)sizeof(md); ++i)
sprintf(output + (i << 1), "%.2X", md[i]);
std::string result = output;
if(upperCase)
return result;
std::transform(result.begin(), result.end(), result.begin(), tolower);
return result;
}
std::string Crypt::sha512Encode(const std::string& decoded_string, bool upperCase)
{
SHA512_CTX c;
SHA512_Init(&c);
SHA512_Update(&c, decoded_string.c_str(), decoded_string.length());
uint8_t md[SHA512_DIGEST_LENGTH];
SHA512_Final(md, &c);
char output[(SHA512_DIGEST_LENGTH << 1) + 1];
for(int32_t i = 0; i < (int32_t)sizeof(md); ++i)
sprintf(output + (i << 1), "%.2X", md[i]);
std::string result = output;
if(upperCase)
return result;
std::transform(result.begin(), result.end(), result.begin(), tolower);
return result;
}
void Crypt::rsaSetPublicKey(const std::string& n, const std::string& e)
{
BN_dec2bn(&m_rsa->n, n.c_str());

View File

@ -40,8 +40,10 @@ public:
std::string genUUIDKey();
std::string encrypt(const std::string& decrypted_string);
std::string decrypt(const std::string& encrypted_string);
std::string md5Encode(std::string decoded_string, bool upperCase);
std::string sha1Encode(std::string decoded_string, bool upperCase);
std::string md5Encode(const std::string& decoded_string, bool upperCase);
std::string sha1Encode(const std::string& decoded_string, bool upperCase);
std::string sha256Encode(const std::string& decoded_string, bool upperCase);
std::string sha512Encode(const std::string& decoded_string, bool upperCase);
void rsaSetPublicKey(const std::string& n, const std::string& e);
void rsaSetPrivateKey(const std::string &p, const std::string &q, const std::string &d);