Minor LLMD5 extension and cleanup.
* Add LLMD5::clone(unsigned char const*), the inverse of LLMD5::raw_digest. * Add LLMD5::clone(std::string const&), the inverse of LLMD5::hex_digest. * Add LLMD5::isFinalized(), returns true if the object is finalized. * Turn all binary operators into inline friends in the class. * Fix operator<< to take a LLMD5 const& instead of a LLMD5&. These changes are needed for / used by AIMultiGrid.
This commit is contained in:
@@ -281,7 +281,12 @@ void LLMD5::raw_digest(unsigned char *s) const
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Singu extension: the inverse of LLMD5::raw_digest.
|
||||
void LLMD5::clone(unsigned char const* s)
|
||||
{
|
||||
memcpy(digest, s, 16);
|
||||
finalized = 1;
|
||||
}
|
||||
|
||||
void LLMD5::hex_digest(char *s) const
|
||||
{
|
||||
@@ -305,12 +310,26 @@ void LLMD5::hex_digest(char *s) const
|
||||
return;
|
||||
}
|
||||
|
||||
//Singu extension: the inverse of LLMD5::hex_digest.
|
||||
void LLMD5::clone(std::string const& hash_str)
|
||||
{
|
||||
for (int i = 0; i < 16; ++i)
|
||||
{
|
||||
unsigned char byte = 0;
|
||||
for (int j = 0; j < 2; ++j)
|
||||
{
|
||||
char c = hash_str[i * 2 + j];
|
||||
unsigned char nibble = (c >= '0' && c <= '9') ? c - '0' : c - 'a' + 10;
|
||||
byte += nibble << ((1 - j) << 2);
|
||||
}
|
||||
digest[i] = byte;
|
||||
}
|
||||
finalized = 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream &stream, LLMD5 context)
|
||||
std::ostream& operator<<(std::ostream &stream, LLMD5 const& context)
|
||||
{
|
||||
char s[33]; /* Flawfinder: ignore */
|
||||
context.hex_digest(s);
|
||||
@@ -318,23 +337,6 @@ std::ostream& operator<<(std::ostream &stream, LLMD5 context)
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool operator==(const LLMD5& a, const LLMD5& b)
|
||||
{
|
||||
unsigned char a_guts[16];
|
||||
unsigned char b_guts[16];
|
||||
a.raw_digest(a_guts);
|
||||
b.raw_digest(b_guts);
|
||||
if (memcmp(a_guts,b_guts,16)==0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator!=(const LLMD5& a, const LLMD5& b)
|
||||
{
|
||||
return !(a==b);
|
||||
}
|
||||
|
||||
// PRIVATE METHODS:
|
||||
|
||||
void LLMD5::init(){
|
||||
|
||||
@@ -102,18 +102,27 @@ public:
|
||||
void update (const std::string& str);
|
||||
void finalize ();
|
||||
|
||||
bool isFinalized() const { return finalized; }
|
||||
|
||||
// constructors for special circumstances. All these constructors finalize
|
||||
// the MD5 context.
|
||||
LLMD5 (const unsigned char *string); // digest string, finalize
|
||||
LLMD5 (std::istream& stream); // digest stream, finalize
|
||||
LLMD5 (FILE *file); // digest file, close, finalize
|
||||
LLMD5 (const unsigned char *string, const unsigned int number);
|
||||
|
||||
// Singu extension: set digest directly, finalize.
|
||||
void clone(unsigned char const* digest); // Inverse of raw_digest.
|
||||
void clone(std::string const& hash_str); // Inverse of hex_digest.
|
||||
|
||||
// methods to acquire finalized result
|
||||
void raw_digest(unsigned char *array) const; // provide 16-byte array for binary data
|
||||
void hex_digest(char *string) const; // provide 33-byte array for ascii-hex string
|
||||
|
||||
friend LL_COMMON_API std::ostream& operator<< (std::ostream&, LLMD5 context);
|
||||
friend LL_COMMON_API std::ostream& operator<< (std::ostream&, LLMD5 const& context);
|
||||
friend LL_COMMON_API bool operator==(const LLMD5& a, const LLMD5& b) { return std::memcmp(a.digest ,b.digest, 16) == 0; }
|
||||
friend LL_COMMON_API bool operator!=(const LLMD5& a, const LLMD5& b) { return std::memcmp(a.digest ,b.digest, 16) != 0; }
|
||||
friend LL_COMMON_API bool operator<(const LLMD5& a, const LLMD5& b) { return std::memcmp(a.digest ,b.digest, 16) < 0; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -135,7 +144,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
LL_COMMON_API bool operator==(const LLMD5& a, const LLMD5& b);
|
||||
LL_COMMON_API bool operator!=(const LLMD5& a, const LLMD5& b);
|
||||
|
||||
#endif // LL_LLMD5_H
|
||||
|
||||
Reference in New Issue
Block a user