From e14521b5b3f8d0dbd3b3e78585eac0746f6d3559 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Tue, 4 Jan 2011 09:31:55 +0100 Subject: [PATCH] ENH: add SHA1 comparison to text representations from a string - eases management/control of previously calculated sums - add empty() member function to SHA1Digest - adjust Copyright dates --- .../primitives/hashes/Hasher/Hasher.C | 4 +- .../primitives/hashes/Hasher/Hasher.H | 2 +- .../primitives/hashes/Hasher/HasherInt.H | 2 +- src/OpenFOAM/primitives/hashes/SHA1/SHA1.C | 9 +--- src/OpenFOAM/primitives/hashes/SHA1/SHA1.H | 41 ++++++++++++----- .../primitives/hashes/SHA1/SHA1Digest.C | 4 +- .../primitives/hashes/SHA1/SHA1Digest.H | 25 +++++++++- src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H | 46 +++++++++++-------- 8 files changed, 89 insertions(+), 44 deletions(-) diff --git a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C index dddd523d36c..9c6582fa9c3 100644 --- a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C +++ b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -490,7 +490,7 @@ unsigned Foam::Hasher const short endianTest = 0x0100; // yields 0x01 for big endian - if (*(reinterpret_cast<const char *>(&endianTest))) + if (*(reinterpret_cast<const char*>(&endianTest))) { return jenkins_hashbig(key, length, initval); } diff --git a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.H b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.H index 1fa68d49193..85a7ea202c0 100644 --- a/src/OpenFOAM/primitives/hashes/Hasher/Hasher.H +++ b/src/OpenFOAM/primitives/hashes/Hasher/Hasher.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License diff --git a/src/OpenFOAM/primitives/hashes/Hasher/HasherInt.H b/src/OpenFOAM/primitives/hashes/Hasher/HasherInt.H index 96be6188a98..f5a1e8b3ff1 100644 --- a/src/OpenFOAM/primitives/hashes/Hasher/HasherInt.H +++ b/src/OpenFOAM/primitives/hashes/Hasher/HasherInt.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C index d6c99831854..4a9444b6a6b 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -74,7 +74,7 @@ inline uint32_t Foam::SHA1::swapBytes(uint32_t n) const short x = 0x0100; // yields 0x01 for big endian - if (*(reinterpret_cast<const char *>(&x))) + if (*(reinterpret_cast<const char*>(&x))) { return n; } @@ -459,10 +459,5 @@ Foam::SHA1Digest Foam::SHA1::digest() const // } // } -// * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * * // - - -// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * // - // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H index 4b7080e3df0..da19da2d182 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -115,10 +115,10 @@ public: //- Construct null inline SHA1(); - //- Construct and append initial std::string + //- Construct null and append initial std::string explicit inline SHA1(const std::string&); - //- Construct and append initial string + //- Construct null and append initial string explicit inline SHA1(const char*); // Member Functions @@ -145,25 +145,44 @@ public: // Member Operators - //- Equality operator + //- Equality operator, compares %digests + inline bool operator==(const SHA1&) const; + + //- Compare %digest inline bool operator==(const SHA1Digest&) const; - //- Inequality operator - inline bool operator!=(const SHA1Digest&) const; + //- Compare %digest to (40-byte) text representation (eg, from sha1sum) + // An %empty string is equivalent to + // "0000000000000000000000000000000000000000" + inline bool operator==(const std::string& hexdigits) const; + + //- Compare %digest to (40-byte) text representation (eg, from sha1sum) + // A %null or %empty string is equivalent to + // "0000000000000000000000000000000000000000" + inline bool operator==(const char* hexdigits) const; - //- Equality operator - inline bool operator==(const SHA1&) const; - //- Inequality operator + //- Inequality operator, compares %digests inline bool operator!=(const SHA1&) const; - //- Convert to a digest, calculate current digest from appended data. + //- Inequality operator, compare %digest + inline bool operator!=(const SHA1Digest&) const; + + //- Inequality operator, compares %digests + inline bool operator!=(const std::string& hexdigits) const; + + //- Inequality operator, compare %digest + inline bool operator!=(const char* hexdigits) const; + + + //- Convert to a SHA1Digest, + // calculate current %digest from appended data inline operator SHA1Digest() const; - // Friend Functions // Friend Operators + //- Output the %digest inline friend Ostream& operator<<(Ostream&, const SHA1&); }; diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C index a270dd1d774..7b5d16d722a 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -105,7 +105,7 @@ bool Foam::SHA1Digest::operator==(const SHA1Digest& rhs) const bool Foam::SHA1Digest::operator!=(const SHA1Digest& rhs) const { - return !operator==(rhs); + return !this->operator==(rhs); } diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H index 0402dc9884c..19c9698d13d 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -38,6 +38,8 @@ SourceFiles #ifndef SHA1Digest_H #define SHA1Digest_H +#include <string> + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -75,12 +77,33 @@ public: //- Reset the digest to zero void clear(); + //- Return true if the digest is empty (ie, all zero). + bool empty() const; + //- Equality operator bool operator==(const SHA1Digest&) const; + //- Compare to (40-byte) text representation (eg, from sha1sum) + // An %empty string is equivalent to + // "0000000000000000000000000000000000000000" + bool operator==(const std::string& hexdigits) const; + + //- Compare to (40-byte) text representation (eg, from sha1sum) + // A %null or %empty string is equivalent to + // "0000000000000000000000000000000000000000" + bool operator==(const char* hexdigits) const; + + //- Inequality operator bool operator!=(const SHA1Digest&) const; + //- Inequality operator + bool operator!=(const std::string& hexdigits) const; + + //- Inequality operator + bool operator!=(const char* hexdigits) const; + + friend Ostream& operator<<(Ostream&, const SHA1Digest&); friend Istream& operator>>(Istream&, SHA1Digest&); diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H index e1715bc4c13..aa05de558c1 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2009-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -48,9 +48,6 @@ inline Foam::SHA1::SHA1(const char* str) } -// * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * // - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // inline Foam::SHA1& Foam::SHA1::append(const char* data, size_t len) @@ -79,55 +76,66 @@ inline Foam::SHA1& Foam::SHA1::append(const char* str) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +inline bool Foam::SHA1::operator==(const SHA1& rhs) const +{ + return this->digest() == rhs.digest(); +} + + inline bool Foam::SHA1::operator==(const SHA1Digest& rhs) const { return this->digest() == rhs; } -inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const +inline bool Foam::SHA1::operator==(const std::string& hexdigits) const { - return this->digest() != rhs; + return this->digest() == hexdigits; } -inline bool Foam::SHA1::operator==(const SHA1& rhs) const +inline bool Foam::SHA1::operator==(const char* hexdigits) const { - return digest() == rhs.digest(); + return this->digest() == hexdigits; } inline bool Foam::SHA1::operator!=(const SHA1& rhs) const { - return digest() != rhs.digest(); + return !this->operator==(rhs); } -inline Foam::SHA1::operator -Foam::SHA1Digest () const +inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const { - return digest(); + return !this->operator==(rhs); } -// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * // +inline bool Foam::SHA1::operator!=(const std::string& rhs) const +{ + return !this->operator==(rhs); +} -// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * // +inline bool Foam::SHA1::operator!=(const char* rhs) const +{ + return !this->operator==(rhs); +} -// * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * * // +inline Foam::SHA1::operator Foam::SHA1Digest() const +{ + return digest(); +} // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * // inline Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1& sha) { - return os << sha.digest(); + return os << sha.digest(); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - // ************************************************************************* // -- GitLab