maint(utils): clean up crypto utils
This commit is contained in:
parent
0da8167a0a
commit
e26eed4c11
@ -61,14 +61,14 @@ namespace Utils
|
||||
{
|
||||
if (!key.isValid()) return {};
|
||||
|
||||
std::uint8_t buffer[512];
|
||||
std::uint8_t buffer[512]{};
|
||||
unsigned long length = sizeof(buffer);
|
||||
|
||||
ltc_mp = ltm_desc;
|
||||
register_prng(&sprng_desc);
|
||||
ecc_sign_hash(reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), buffer, &length, nullptr, find_prng("sprng"), key.getKeyPtr());
|
||||
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
}
|
||||
|
||||
bool ECC::VerifyMessage(Key key, const std::string& message, const std::string& signature)
|
||||
@ -115,7 +115,7 @@ namespace Utils
|
||||
|
||||
rsa_sign_hash(reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), find_hash("sha1"), 0, key.getKeyPtr());
|
||||
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
}
|
||||
|
||||
bool RSA::VerifyMessage(Key key, const std::string& message, const std::string& signature)
|
||||
@ -145,9 +145,9 @@ namespace Utils
|
||||
encData.resize(text.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
int des3 = find_cipher("3des");
|
||||
const auto des3 = find_cipher("3des");
|
||||
|
||||
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc);
|
||||
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_encrypt(reinterpret_cast<const std::uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(encData.data()), text.size(), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
@ -160,9 +160,9 @@ namespace Utils
|
||||
decData.resize(data.size());
|
||||
|
||||
symmetric_CBC cbc;
|
||||
int des3 = find_cipher("3des");
|
||||
const auto des3 = find_cipher("3des");
|
||||
|
||||
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc);
|
||||
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), static_cast<int>(key.size()), 0, &cbc);
|
||||
cbc_decrypt(reinterpret_cast<const std::uint8_t*>(data.data()), reinterpret_cast<std::uint8_t*>(decData.data()), data.size(), &cbc);
|
||||
cbc_done(&cbc);
|
||||
|
||||
@ -269,20 +269,21 @@ namespace Utils
|
||||
|
||||
#pragma region JenkinsOneAtATime
|
||||
|
||||
unsigned int JenkinsOneAtATime::Compute(const std::string& data)
|
||||
std::size_t JenkinsOneAtATime::Compute(const std::string& data)
|
||||
{
|
||||
return Compute(data.data(), data.size());
|
||||
}
|
||||
|
||||
unsigned int JenkinsOneAtATime::Compute(const char* key, std::size_t len)
|
||||
std::size_t JenkinsOneAtATime::Compute(const char* key, const std::size_t len)
|
||||
{
|
||||
unsigned int hash, i;
|
||||
std::size_t 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);
|
||||
|
@ -9,16 +9,16 @@ namespace Utils
|
||||
class Token
|
||||
{
|
||||
public:
|
||||
Token() { this->tokenString.clear(); };
|
||||
Token(const Token& obj) : tokenString(obj.tokenString) { };
|
||||
Token(const std::string& token) : tokenString(token.begin(), token.end()) { };
|
||||
Token(const std::basic_string<uint8_t>& token) : tokenString(token.begin(), token.end()) { };
|
||||
Token() { this->tokenString.clear(); }
|
||||
Token(const Token& obj) : tokenString(obj.tokenString) { }
|
||||
Token(const std::string& token) : tokenString(token.begin(), token.end()) { }
|
||||
Token(const std::basic_string<std::uint8_t>& token) : tokenString(token.begin(), token.end()) { }
|
||||
|
||||
Token& operator++ ()
|
||||
{
|
||||
if (this->tokenString.empty())
|
||||
{
|
||||
this->tokenString.append(reinterpret_cast<uint8_t*>(const_cast<char *>("\0")), 1);
|
||||
this->tokenString.append(reinterpret_cast<std::uint8_t*>(const_cast<char *>("\0")), 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -30,7 +30,7 @@ namespace Utils
|
||||
|
||||
if (!i)
|
||||
{
|
||||
this->tokenString = std::basic_string<std::uint8_t>(reinterpret_cast<std::uint8_t*>(const_cast<char*>("\0")), 1) + this->tokenString;
|
||||
this->tokenString = std::basic_string{ reinterpret_cast<std::uint8_t*>(const_cast<char*>("\0")), 1 } + this->tokenString;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -68,25 +68,25 @@ namespace Utils
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (this->toString().size() < token.toString().size())
|
||||
|
||||
if (this->toString().size() < token.toString().size())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (this->toString().size() > token.toString().size())
|
||||
|
||||
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)
|
||||
auto lStr = this->toString();
|
||||
auto rStr = token.toString();
|
||||
|
||||
for (std::size_t i = 0; i < lStr.size(); ++i)
|
||||
{
|
||||
if (lStr[i] < rStr[i])
|
||||
{
|
||||
if (lStr[i] < rStr[i])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,17 +108,12 @@ namespace Utils
|
||||
return !(*this < token);
|
||||
}
|
||||
|
||||
std::string toString()
|
||||
[[nodiscard]] std::string toString() const
|
||||
{
|
||||
return std::string(this->tokenString.begin(), this->tokenString.end());
|
||||
return std::string{ this->tokenString.begin(), this->tokenString.end() };
|
||||
}
|
||||
|
||||
std::string toString() const
|
||||
{
|
||||
return std::string(this->tokenString.begin(), this->tokenString.end());
|
||||
}
|
||||
|
||||
std::basic_string<uint8_t> toUnsignedString()
|
||||
[[nodiscard]] std::basic_string<std::uint8_t> toUnsignedString() const
|
||||
{
|
||||
return this->tokenString;
|
||||
}
|
||||
@ -136,7 +131,7 @@ namespace Utils
|
||||
{
|
||||
public:
|
||||
static std::string GenerateChallenge();
|
||||
static uint32_t GenerateInt();
|
||||
static std::uint32_t GenerateInt();
|
||||
static void Initialize();
|
||||
|
||||
private:
|
||||
@ -152,45 +147,47 @@ namespace Utils
|
||||
Key() : keyStorage(new ecc_key)
|
||||
{
|
||||
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
|
||||
};
|
||||
Key(ecc_key* key) : Key() { if (key) std::memmove(this->getKeyPtr(), key, sizeof(*key)); };
|
||||
Key(ecc_key key) : Key(&key) {};
|
||||
}
|
||||
|
||||
Key(ecc_key* key) : Key() { if (key) std::memmove(this->getKeyPtr(), key, sizeof(*key)); }
|
||||
Key(ecc_key key) : Key(&key) {}
|
||||
|
||||
~Key()
|
||||
{
|
||||
if (this->keyStorage.use_count() <= 1)
|
||||
{
|
||||
this->free();
|
||||
}
|
||||
};
|
||||
|
||||
bool isValid()
|
||||
{
|
||||
return (!Utils::Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
|
||||
}
|
||||
|
||||
ecc_key* getKeyPtr()
|
||||
[[nodiscard]] bool isValid()
|
||||
{
|
||||
return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
|
||||
}
|
||||
|
||||
[[nodiscard]] ecc_key* getKeyPtr()
|
||||
{
|
||||
return this->keyStorage.get();
|
||||
}
|
||||
|
||||
std::string getPublicKey()
|
||||
[[nodiscard]] std::string getPublicKey()
|
||||
{
|
||||
uint8_t buffer[512] = { 0 };
|
||||
std::uint8_t buffer[512]{};
|
||||
DWORD length = sizeof(buffer);
|
||||
|
||||
if (ecc_ansi_x963_export(this->getKeyPtr(), buffer, &length) == CRYPT_OK)
|
||||
{
|
||||
return std::string(reinterpret_cast<char*>(buffer), length);
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
}
|
||||
|
||||
return "";
|
||||
return std::string{};
|
||||
}
|
||||
|
||||
void set(const std::string& pubKeyBuffer)
|
||||
{
|
||||
this->free();
|
||||
|
||||
if (ecc_ansi_x963_import(reinterpret_cast<const uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->getKeyPtr()) != CRYPT_OK)
|
||||
if (ecc_ansi_x963_import(reinterpret_cast<const std::uint8_t*>(pubKeyBuffer.data()), pubKeyBuffer.size(), this->getKeyPtr()) != CRYPT_OK)
|
||||
{
|
||||
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
|
||||
}
|
||||
@ -200,23 +197,23 @@ namespace Utils
|
||||
{
|
||||
this->free();
|
||||
|
||||
if (ecc_import(reinterpret_cast<const uint8_t*>(key.data()), key.size(), this->getKeyPtr()) != CRYPT_OK)
|
||||
if (ecc_import(reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), this->getKeyPtr()) != CRYPT_OK)
|
||||
{
|
||||
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
|
||||
}
|
||||
}
|
||||
|
||||
std::string serialize(int type = PK_PRIVATE)
|
||||
[[nodiscard]] std::string serialize(int type = PK_PRIVATE)
|
||||
{
|
||||
uint8_t buffer[4096] = { 0 };
|
||||
std::uint8_t buffer[4096]{};
|
||||
DWORD length = sizeof(buffer);
|
||||
|
||||
if (ecc_export(buffer, &length, type, this->getKeyPtr()) == CRYPT_OK)
|
||||
{
|
||||
return std::string(reinterpret_cast<char*>(buffer), length);
|
||||
return std::string{ reinterpret_cast<char*>(buffer), length };
|
||||
}
|
||||
|
||||
return "";
|
||||
return std::string{};
|
||||
}
|
||||
|
||||
void free()
|
||||
@ -265,16 +262,16 @@ namespace Utils
|
||||
}
|
||||
}
|
||||
|
||||
rsa_key* getKeyPtr()
|
||||
{
|
||||
return this->keyStorage.get();
|
||||
}
|
||||
|
||||
bool isValid()
|
||||
[[nodiscard]] bool isValid()
|
||||
{
|
||||
return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
|
||||
}
|
||||
|
||||
[[nodiscard]] rsa_key* getKeyPtr()
|
||||
{
|
||||
return this->keyStorage.get();
|
||||
}
|
||||
|
||||
void free()
|
||||
{
|
||||
if (this->isValid())
|
||||
@ -333,8 +330,8 @@ namespace Utils
|
||||
class JenkinsOneAtATime
|
||||
{
|
||||
public:
|
||||
static unsigned int Compute(const std::string& data);
|
||||
static unsigned int Compute(const char* key, std::size_t len);
|
||||
static std::size_t Compute(const std::string& data);
|
||||
static std::size_t Compute(const char* key, std::size_t len);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user