From 156ab9b879fc61b5b753e3dbae808f230640d26e Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Sat, 4 Aug 2012 11:37:55 -0300 Subject: [PATCH] More SHA encoding functions and add missing copyright --- src/framework/sql/database.cpp | 22 ++++++++++++++++ src/framework/sql/database.h | 22 ++++++++++++++++ src/framework/sql/mysql.cpp | 25 ++++++++++++++++++ src/framework/sql/mysql.h | 25 +++++++++++++++--- src/framework/util/crypt.cpp | 46 ++++++++++++++++++++++++++++++++-- src/framework/util/crypt.h | 6 +++-- 6 files changed, 139 insertions(+), 7 deletions(-) diff --git a/src/framework/sql/database.cpp b/src/framework/sql/database.cpp index c137b88e..bfd14660 100644 --- a/src/framework/sql/database.cpp +++ b/src/framework/sql/database.cpp @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2010-2012 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() diff --git a/src/framework/sql/database.h b/src/framework/sql/database.h index bf26e55a..5f5bd60d 100644 --- a/src/framework/sql/database.h +++ b/src/framework/sql/database.h @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2010-2012 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 diff --git a/src/framework/sql/mysql.cpp b/src/framework/sql/mysql.cpp index acec629a..7648a045 100644 --- a/src/framework/sql/mysql.cpp +++ b/src/framework/sql/mysql.cpp @@ -1,3 +1,28 @@ +/* + * Copyright (c) 2010-2012 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 +#endif #include "mysql.h" #include #include diff --git a/src/framework/sql/mysql.h b/src/framework/sql/mysql.h index 444955a3..d9605679 100644 --- a/src/framework/sql/mysql.h +++ b/src/framework/sql/mysql.h @@ -1,11 +1,30 @@ +/* + * Copyright (c) 2010-2012 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 -#ifdef WIN32 -#include -#endif #include class DBResult; diff --git a/src/framework/util/crypt.cpp b/src/framework/util/crypt.cpp index cc01e3fc..a880b7e4 100644 --- a/src/framework/util/crypt.cpp +++ b/src/framework/util/crypt.cpp @@ -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()); diff --git a/src/framework/util/crypt.h b/src/framework/util/crypt.h index cfa72647..673621dc 100644 --- a/src/framework/util/crypt.h +++ b/src/framework/util/crypt.h @@ -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);