mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-02 09:49:48 +02:00
added DigestEngine::digestFromHex(const std::string& digest) method
This commit is contained in:
parent
bd59670069
commit
a4931bb9af
@ -85,6 +85,9 @@ public:
|
||||
static std::string digestToHex(const Digest& bytes);
|
||||
/// Converts a message digest into a string of hexadecimal numbers.
|
||||
|
||||
static Digest digestFromHex(const std::string& digest);
|
||||
/// Converts a string created by digestToHex back to its Digest presentation
|
||||
|
||||
protected:
|
||||
virtual void updateImpl(const void* data, unsigned length) = 0;
|
||||
/// Updates the digest with the given data. Must be implemented
|
||||
|
@ -35,6 +35,7 @@
|
||||
|
||||
|
||||
#include "Poco/DigestEngine.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -65,4 +66,39 @@ std::string DigestEngine::digestToHex(const Digest& bytes)
|
||||
}
|
||||
|
||||
|
||||
DigestEngine::Digest DigestEngine::digestFromHex(const std::string& digest)
|
||||
{
|
||||
if (digest.size() % 2 != 0)
|
||||
throw DataFormatException();
|
||||
Digest result;
|
||||
result.reserve(digest.size()/2);
|
||||
for (std::size_t i = 0; i < digest.size(); ++i)
|
||||
{
|
||||
int c = 0;
|
||||
// first upper 4 bits
|
||||
if (digest[i] >= '0' && digest[i] <= '9')
|
||||
c = digest[i] - '0';
|
||||
else if (digest[i] >= 'a' && digest[i] <= 'f')
|
||||
c = digest[i] - 'a'+10;
|
||||
else if (digest[i] >= 'A' && digest[i] <= 'F')
|
||||
c = digest[i] - 'A'+10;
|
||||
else
|
||||
throw DataFormatException();
|
||||
c <<= 4;
|
||||
++i;
|
||||
if (digest[i] >= '0' && digest[i] <= '9')
|
||||
c += digest[i] - '0';
|
||||
else if (digest[i] >= 'a' && digest[i] <= 'f')
|
||||
c += digest[i] - 'a'+10;
|
||||
else if (digest[i] >= 'A' && digest[i] <= 'F')
|
||||
c += digest[i] - 'A'+10;
|
||||
else
|
||||
throw DataFormatException();
|
||||
|
||||
result.push_back(static_cast<unsigned char>(c));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
Loading…
x
Reference in New Issue
Block a user