From 9c246fecb523aea9eb05f5f3628f10aa9468e5fe Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 23 Feb 2016 13:39:38 +0100 Subject: [PATCH] Add some rough estimation --- src/Components/Modules/Auth.cpp | 21 ++++++++++++++++++--- src/Components/Modules/Auth.hpp | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index eccb28fd..1ffcc6f7 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -52,7 +52,20 @@ namespace Components if (Auth::TokenContainer.generating) { - Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::VA("Increasing security level from %d to %d"/* (approx. 1 min)"*/, Auth::GetSecurityLevel(), Auth::TokenContainer.targetLevel)); + static int lastCalc = 0; + static double mseconds = 0; + + if (!lastCalc || (Game::Com_Milliseconds() - lastCalc) > 1000) + { + int diff = Game::Com_Milliseconds() - Auth::TokenContainer.startTime; + double hashPMS = (Auth::TokenContainer.hashes * 1.0) / diff; + double requiredHashes = std::pow(2, Auth::TokenContainer.targetLevel) - Auth::TokenContainer.hashes; + mseconds = requiredHashes / hashPMS; + mseconds *= 2; // Times 2, cause well, we might not hit it the first time :P + if (mseconds < 0) mseconds = 0; + } + + Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::VA("Increasing security level from %d to %d (est. %s)", Auth::GetSecurityLevel(), Auth::TokenContainer.targetLevel, Utils::FormatTimeSpan(static_cast(mseconds)).data())); } else if(Auth::TokenContainer.thread) { @@ -177,8 +190,9 @@ namespace Components Auth::TokenContainer.thread = new std::thread([&level] () { Auth::TokenContainer.generating = true; + Auth::TokenContainer.hashes = 0; Auth::TokenContainer.startTime = Game::Com_Milliseconds(); - Auth::IncrementToken(Auth::GuidToken, Auth::GuidKey.GetPublicKey(), Auth::TokenContainer.targetLevel, &Auth::TokenContainer.cancel); + Auth::IncrementToken(Auth::GuidToken, Auth::GuidKey.GetPublicKey(), Auth::TokenContainer.targetLevel, &Auth::TokenContainer.cancel, &Auth::TokenContainer.hashes); Auth::TokenContainer.generating = false; if (Auth::TokenContainer.cancel) @@ -219,7 +233,7 @@ namespace Components return bits; } - void Auth::IncrementToken(Utils::Cryptography::Token& token, std::string publicKey, uint32_t zeroBits, bool* cancel) + void Auth::IncrementToken(Utils::Cryptography::Token& token, std::string publicKey, uint32_t zeroBits, bool* cancel, uint64_t* count) { if (zeroBits > 512) return; // Not possible, due to SHA512 @@ -233,6 +247,7 @@ namespace Components do { ++tempToken; + if (count) ++(*count); level = Auth::GetZeroBits(tempToken, publicKey); // Store level if higher than the last one diff --git a/src/Components/Modules/Auth.hpp b/src/Components/Modules/Auth.hpp index 64de02dd..967f4ad9 100644 --- a/src/Components/Modules/Auth.hpp +++ b/src/Components/Modules/Auth.hpp @@ -16,7 +16,7 @@ namespace Components static void IncreaseSecurityLevel(uint32_t level, std::string command = ""); static uint32_t GetZeroBits(Utils::Cryptography::Token token, std::string publicKey); - static void IncrementToken(Utils::Cryptography::Token& token, std::string publicKey, uint32_t zeroBits, bool* cancel = nullptr); + static void IncrementToken(Utils::Cryptography::Token& token, std::string publicKey, uint32_t zeroBits, bool* cancel = nullptr, uint64_t* count = nullptr); private: @@ -44,6 +44,7 @@ namespace Components uint32_t targetLevel; int startTime; std::string command; + uint64_t hashes; }; static AuthInfo ClientAuthInfo[18];