diff --git a/applications/test/sha1/testSHA1.C b/applications/test/sha1/testSHA1.C index 603829fac42e9c0a684742a39d73f7bafe841dcf..9a2ac37c487dd2fe31d78c23d33c13eb21916e8c 100644 --- a/applications/test/sha1/testSHA1.C +++ b/applications/test/sha1/testSHA1.C @@ -41,7 +41,7 @@ using namespace Foam; int main(int argc, char * argv[]) { SHA1 sha; - SHA1::Digest shaDig; + SHA1Digest shaDig; std::string str("The quick brown fox jumps over the lazy dog"); Info<< shaDig << nl; @@ -72,7 +72,7 @@ int main(int argc, char * argv[]) sha.clear(); sha.append(str); - SHA1::Digest shaDig_A = sha; + SHA1Digest shaDig_A = sha; SHA1 sha_A = sha; diff --git a/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H index c61da69d9107a2bbeb8827da0d064850ce494eb0..00b72fd7bfb8ab31af5b44ba5ae7a8b66aa70919 100644 --- a/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H +++ b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H @@ -158,7 +158,7 @@ public: : OSstream ( - *(new osha1stream()), + *(new osha1stream), "OSHA1stream.sinkFile_", format, version @@ -185,7 +185,7 @@ public: } //- Return SHA1::Digest for the data processed until now - Foam::SHA1::Digest digest() + Foam::SHA1Digest digest() { return sha1().digest(); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 985aee472c8d962f137d6f6efd72efc0e8eed28f..a757caebcc9729ffbe51019c3cc2e3c35abe9543 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -233,7 +233,7 @@ Foam::label Foam::dictionary::endLineNumber() const } -Foam::SHA1::Digest Foam::dictionary::digest() const +Foam::SHA1Digest Foam::dictionary::digest() const { OSHA1stream os; diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 027909ba22ed852f2d7d5bac9d9b98e70c9d776c..68fe20004cfd188790af07316df5625916ae682b 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -60,7 +60,6 @@ SourceFiles #include "HashTable.H" #include "wordList.H" #include "className.H" -#include "SHA1.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -70,6 +69,8 @@ namespace Foam // Forward declaration of friend functions and operators class regExp; class dictionary; +class SHA1Digest; + Istream& operator>>(Istream&, dictionary&); Ostream& operator<<(Ostream&, const dictionary&); @@ -212,7 +213,7 @@ public: label endLineNumber() const; //- Return the SHA1 digest of the dictionary contents - SHA1::Digest digest() const; + SHA1Digest digest() const; // Search and lookup diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C index 56373824f65536e1526c721852d809a6e2eba189..7bf07845e2f87f1200bfb6c3b8380f03e945a2ef 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.C @@ -47,8 +47,8 @@ Description // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // //! @cond fileScope -// The bytes used to pad buffer to the next 64-byte boundary. -// (RFC 1321, 3.1: Step 1) +// The bytes used to pad buffer to the next 64-byte boundary. +// (RFC 1321, 3.1: Step 1) static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; //! @endcond fileScope @@ -324,7 +324,7 @@ Foam::SHA1::processBlock(const void *data, size_t len) } -void Foam::SHA1::calcDigest(SHA1::Digest& dig) const +void Foam::SHA1::calcDigest(SHA1Digest& dig) const { if (bufTotal_[0] || bufTotal_[1]) { @@ -411,9 +411,9 @@ bool Foam::SHA1::finalize() } -Foam::SHA1::Digest Foam::SHA1::digest() const +Foam::SHA1Digest Foam::SHA1::digest() const { - SHA1::Digest dig; + SHA1Digest dig; if (finalized_) { diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H index 55ed2eed70e349e47cd186c819bb8a556e607103..e2be6300a87e6c3943f932f2b1bbab9160bd8fa7 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1.H @@ -31,10 +31,12 @@ Description Adapted from the gnulib implementation. +SeeAlso + Foam::SHA1Digest + SourceFiles SHA1I.H SHA1.C - SHA1Digest.C \*---------------------------------------------------------------------------*/ @@ -45,6 +47,8 @@ SourceFiles #include <cstddef> #include <stdint.h> // C++0x uses <cstdint> +#include "SHA1Digest.H" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -55,6 +59,7 @@ class Ostream; // Forward declaration of friend functions and operators class SHA1; +class SHA1Digest; Ostream& operator<<(Ostream&, const SHA1&); @@ -64,42 +69,6 @@ Ostream& operator<<(Ostream&, const SHA1&); class SHA1 { -public: - - // Public classes - - //- The SHA1 digest itself - class Digest - { - public: - friend class SHA1; - - //- The length of the digest contents - static const unsigned length = 20; - - //- Construct null - Digest(); - - //- Reset the digest to zero - void clear(); - - //- Equality operator - bool operator==(const Digest&) const; - - //- Inequality operator - bool operator!=(const Digest&) const; - - friend Ostream& operator<<(Ostream&, const SHA1::Digest&); - - private: - - //- The digest contents - unsigned char v_[length]; - }; - - -private: - // Private data //- Track if the hashsum has been finalized (added count, etc) @@ -138,7 +107,7 @@ private: void processBytes(const void *data, size_t len); //- Calculate current digest from appended data. - void calcDigest(SHA1::Digest&) const; + void calcDigest(SHA1Digest&) const; public: @@ -172,16 +141,16 @@ public: bool finalize(); //- Calculate current digest from appended data. - Digest digest() const; + SHA1Digest digest() const; // Member Operators //- Equality operator - inline bool operator==(const SHA1::Digest&) const; + inline bool operator==(const SHA1Digest&) const; //- Inequality operator - inline bool operator!=(const SHA1::Digest&) const; + inline bool operator!=(const SHA1Digest&) const; //- Equality operator inline bool operator==(const SHA1&) const; @@ -190,8 +159,7 @@ public: inline bool operator!=(const SHA1&) const; //- Convert to a digest, calculate current digest from appended data. - inline operator SHA1::Digest() const; - + inline operator SHA1Digest() const; // Friend Functions diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C index d87035b0049bb3ebd5b1db9ae060303053c048e7..b9df2d3cefb391a063822a49c6aa4f4fc40909a3 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.C @@ -24,80 +24,69 @@ License \*---------------------------------------------------------------------------*/ -#include "SHA1.H" +#include "SHA1Digest.H" #include "IOstreams.H" #include <cstring> // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +//! @cond fileScope +const char hexChars[] = "0123456789abcdef"; +//! @endcond fileScope -// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::SHA1::Digest::Digest() +Foam::SHA1Digest::SHA1Digest() { clear(); } -// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // - -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // - - -// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // - - // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -void Foam::SHA1::Digest::clear() +void Foam::SHA1Digest::clear() { - memset(v_, '\0', length); + memset(v_, 0, length); } // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // -bool Foam::SHA1::Digest::operator==(const SHA1::Digest& rhs) const +bool Foam::SHA1Digest::operator==(const SHA1Digest& rhs) const { - for (size_t i = 0; i < length; ++i) + for (unsigned i = 0; i < length; ++i) { if (v_[i] != rhs.v_[i]) { return false; } } + return true; } -bool Foam::SHA1::Digest::operator!=(const SHA1::Digest& rhs) const +bool Foam::SHA1Digest::operator!=(const SHA1Digest& rhs) const { return !operator==(rhs); } -// * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * * // - - // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * // -Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1::Digest& dig) +Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1Digest& dig) { - const char hexChars[] = "0123456789abcdef"; - const unsigned char *v = dig.v_; - for (size_t i=0; i < dig.length; i++) + + for (unsigned i = 0; i < dig.length; ++i) { os.write(hexChars[((v[i] >> 4) & 0xF)]); os.write(hexChars[(v[i] & 0xF)]); } - os.check("Ostream& operator<<(Ostream&, const SHA1::Digest&)"); + os.check("Ostream& operator<<(Ostream&, const SHA1Digest&)"); return os; } diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H new file mode 100644 index 0000000000000000000000000000000000000000..27c9e8510fcd5c11476341c07bbc91c0833b9f97 --- /dev/null +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1Digest.H @@ -0,0 +1,97 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::SHA1Digest + +Description + The SHA1 message digest. + +SeeAlso + Foam::SHA1 + +SourceFiles + SHA1Digest.C + +\*---------------------------------------------------------------------------*/ + +#ifndef SHA1Digest_H +#define SHA1Digest_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of classes +class Ostream; + +// Forward declaration of friend functions and operators +class SHA1; +class SHA1Digest; +Ostream& operator<<(Ostream&, const SHA1Digest&); + + +/*---------------------------------------------------------------------------*\ + Class SHA1Digest Declaration +\*---------------------------------------------------------------------------*/ + +class SHA1Digest +{ +public: + friend class SHA1; + + //- The length of the digest contents + static const unsigned length = 20; + + //- Construct a zero digest + SHA1Digest(); + + //- Reset the digest to zero + void clear(); + + //- Equality operator + bool operator==(const SHA1Digest&) const; + + //- Inequality operator + bool operator!=(const SHA1Digest&) const; + + friend Ostream& operator<<(Ostream&, const SHA1Digest&); + +private: + + //- The digest contents + unsigned char v_[length]; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H index d0fce820b49a38a733c7736477f942e557a90195..19ab5bedbd95cdd65331946e831673c6671e7cea 100644 --- a/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H +++ b/src/OpenFOAM/primitives/hashes/SHA1/SHA1I.H @@ -80,13 +80,13 @@ inline Foam::SHA1& Foam::SHA1::append(const char* str) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -inline bool Foam::SHA1::operator==(const SHA1::Digest& rhs) const +inline bool Foam::SHA1::operator==(const SHA1Digest& rhs) const { return this->digest() == rhs; } -inline bool Foam::SHA1::operator!=(const SHA1::Digest& rhs) const +inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const { return this->digest() != rhs; } @@ -105,7 +105,7 @@ inline bool Foam::SHA1::operator!=(const SHA1& rhs) const inline Foam::SHA1::operator -Foam::SHA1::Digest () const +Foam::SHA1Digest () const { return digest(); }