POW optimization and general cleanup

This commit is contained in:
momo5502
2016-04-11 13:32:11 +02:00
parent 4cb3e04c88
commit 3c703e6db4
14 changed files with 208 additions and 66 deletions

View File

@ -27,11 +27,11 @@ namespace Utils
#pragma endregion
#pragma region ECDSA
#pragma region ECC
ECDSA::Key ECDSA::GenerateKey(int bits)
ECC::Key ECC::GenerateKey(int bits)
{
ECDSA::Key key;
ECC::Key key;
register_prng(&sprng_desc);
@ -42,7 +42,7 @@ namespace Utils
return key;
}
std::string ECDSA::SignMessage(Key key, std::string message)
std::string ECC::SignMessage(Key key, std::string message)
{
if (!key.IsValid()) return "";
@ -58,7 +58,7 @@ namespace Utils
return std::string(reinterpret_cast<char*>(buffer), length);
}
bool ECDSA::VerifyMessage(Key key, std::string message, std::string signature)
bool ECC::VerifyMessage(Key key, std::string message, std::string signature)
{
if (!key.IsValid()) return false;
@ -163,6 +163,25 @@ namespace Utils
return Utils::DumpHex(hash, "");
}
#pragma endregion
#pragma region JenkinsOneAtATime
unsigned int JenkinsOneAtATime::Compute(const char *key, size_t len)
{
unsigned int hash, i;
for (hash = i = 0; i < len; ++i)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
#pragma endregion
}

View File

@ -49,11 +49,72 @@ namespace Utils
return result;
}
bool operator==(const Token& token) const
{
return (this->ToString() == token.ToString());
}
bool operator!=(const Token& token) const
{
return !(*this == token);
}
bool operator< (const Token& token) const
{
if (*this == token)
{
return false;
}
else if (this->ToString().size() < token.ToString().size())
{
return true;
}
else if (this->ToString().size() > token.ToString().size())
{
return false;
}
else
{
auto lStr = this->ToString();
auto rStr = token.ToString();
for (unsigned int i = 0; i < lStr.size(); ++i)
{
if (lStr[i] < rStr[i])
{
return true;
}
}
}
return false;
}
bool operator> (const Token& token) const
{
return (token < *this && *this != token);
}
bool operator<= (const Token& token) const
{
return !(*this > token);
}
bool operator>= (const Token& token) const
{
return !(*this < token);
}
std::string ToString()
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
const std::string ToString() const
{
return std::string(this->TokenString.begin(), this->TokenString.end());
}
std::basic_string<uint8_t> ToUnsignedString()
{
return this->TokenString;
@ -78,7 +139,7 @@ namespace Utils
static prng_state State;
};
class ECDSA
class ECC
{
public:
class Key
@ -235,5 +296,11 @@ namespace Utils
static std::string Compute(std::string data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
};
class JenkinsOneAtATime
{
public:
static unsigned int Compute(const char *key, size_t len);
};
}
}

View File

@ -100,21 +100,6 @@ namespace Utils
return (haystack.size() >= needle.size() && !strncmp(needle.data(), haystack.data(), needle.size()));
}
unsigned int OneAtATime(const char *key, size_t len)
{
unsigned int hash, i;
for (hash = i = 0; i < len; ++i)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
// trim from start
std::string &LTrim(std::string &s)
{

View File

@ -8,7 +8,6 @@ namespace Utils
std::vector<std::string> Explode(const std::string& str, char delim);
void Replace(std::string &string, std::string find, std::string replace);
bool StartsWith(std::string haystack, std::string needle);
unsigned int OneAtATime(const char *key, size_t len);
std::string &LTrim(std::string &s);
std::string &RTrim(std::string &s);
std::string &Trim(std::string &s);