ENH: minor code cleanup for SHA1 calculation
- append single character - make append() methods void: methods are never chained anyhow - refactor digest comparison (code reduction) COMP: add overflow handling for OSHA1stream
This commit is contained in:
@ -36,10 +36,11 @@ Description
|
||||
template<class T>
|
||||
T Foam::ReadHex(ISstream& is)
|
||||
{
|
||||
// Takes into account that 'a' (or 'A') is 10
|
||||
static const int alphaOffset = toupper('A') - 10;
|
||||
// Takes into account that '0' is 0
|
||||
static const int zeroOffset = int('0');
|
||||
// The char '0' == 0
|
||||
static constexpr int offsetZero = int('0');
|
||||
|
||||
// The char 'A' (or 'a') == 10
|
||||
static constexpr int offsetAlpha = int('A') - 10;
|
||||
|
||||
char c = 0;
|
||||
|
||||
@ -63,11 +64,11 @@ T Foam::ReadHex(ISstream& is)
|
||||
|
||||
if (isdigit(c))
|
||||
{
|
||||
result += int(c) - zeroOffset;
|
||||
result += int(c) - offsetZero;
|
||||
}
|
||||
else
|
||||
{
|
||||
result += toupper(c) - alphaOffset;
|
||||
result += toupper(c) - offsetAlpha;
|
||||
}
|
||||
} while (is.get(c));
|
||||
|
||||
|
||||
@ -34,8 +34,8 @@ SourceFiles
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef ReadHex_H
|
||||
#define ReadHex_H
|
||||
#ifndef Foam_ReadHex_H
|
||||
#define Foam_ReadHex_H
|
||||
|
||||
#include "ISstream.H"
|
||||
|
||||
@ -44,7 +44,7 @@ SourceFiles
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
//- Read a hex label from an input stream
|
||||
//- Read a hex integer from an input stream
|
||||
template<class T>
|
||||
T ReadHex(ISstream& is);
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2019-2021 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -32,8 +32,8 @@ Description
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef OSHA1stream_H
|
||||
#define OSHA1stream_H
|
||||
#ifndef Foam_OSHA1stream_H
|
||||
#define Foam_OSHA1stream_H
|
||||
|
||||
#include "OSstream.H"
|
||||
#include "SHA1.H"
|
||||
@ -63,10 +63,17 @@ class osha1stream
|
||||
|
||||
protected:
|
||||
|
||||
//- Handle overflow
|
||||
virtual int overflow(int c = EOF)
|
||||
{
|
||||
if (c != EOF) sha1_.append(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
//- Put sequence of characters
|
||||
virtual std::streamsize xsputn(const char* s, std::streamsize n)
|
||||
{
|
||||
sha1_.append(s, n);
|
||||
if (n) sha1_.append(s, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
@ -76,7 +83,7 @@ class osha1stream
|
||||
sha1buf() = default;
|
||||
|
||||
//- Full access to the sha1
|
||||
inline SHA1& sha1()
|
||||
SHA1& sha1() noexcept
|
||||
{
|
||||
return sha1_;
|
||||
}
|
||||
@ -108,7 +115,7 @@ public:
|
||||
}
|
||||
|
||||
//- Full access to the sha1
|
||||
SHA1& sha1()
|
||||
SHA1& sha1() noexcept
|
||||
{
|
||||
return buf_.sha1();
|
||||
}
|
||||
@ -146,7 +153,7 @@ public:
|
||||
// Member Functions
|
||||
|
||||
//- Full access to the sha1
|
||||
SHA1& sha1()
|
||||
SHA1& sha1() noexcept
|
||||
{
|
||||
return stream_.sha1();
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -42,8 +42,8 @@ SourceFiles
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef SHA1_H
|
||||
#define SHA1_H
|
||||
#ifndef Foam_SHA1_H
|
||||
#define Foam_SHA1_H
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
@ -98,13 +98,13 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct null
|
||||
//- Default construct
|
||||
inline SHA1();
|
||||
|
||||
//- Construct null and append initial string
|
||||
//- Default construct and append initial string content
|
||||
inline explicit SHA1(const char* str);
|
||||
|
||||
//- Construct null and append initial std::string
|
||||
//- Default construct and append initial string content
|
||||
inline explicit SHA1(const std::string& str);
|
||||
|
||||
|
||||
@ -113,17 +113,20 @@ public:
|
||||
//- Reset the hashed data before appending more
|
||||
void clear() noexcept;
|
||||
|
||||
//- Append data for processing
|
||||
inline SHA1& append(const char* str);
|
||||
|
||||
//- Append data for processing
|
||||
inline SHA1& append(const char* data, size_t len);
|
||||
//- Append single character
|
||||
inline void append(char c);
|
||||
|
||||
//- Append string for processing
|
||||
inline SHA1& append(const std::string& str);
|
||||
inline void append(const char* str);
|
||||
|
||||
//- Append data for processing
|
||||
inline void append(const char* data, size_t len);
|
||||
|
||||
//- Append string for processing
|
||||
inline void append(const std::string& str);
|
||||
|
||||
//- Append substring for processing
|
||||
inline SHA1& append
|
||||
inline void append
|
||||
(
|
||||
const std::string& str,
|
||||
size_t pos,
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -40,7 +40,7 @@ static const char hexChars[] = "0123456789abcdef";
|
||||
static constexpr int offsetZero = int('0');
|
||||
|
||||
// The char 'A' (or 'a') == 10
|
||||
static constexpr int offsetUpper = int('A') - 10;
|
||||
static constexpr int offsetAlpha = int('A') - 10;
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
|
||||
@ -70,12 +70,42 @@ static unsigned char readHexDigit(Istream& is)
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return toupper(c) - offsetUpper;
|
||||
return toupper(c) - offsetAlpha;
|
||||
}
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
||||
|
||||
bool Foam::SHA1Digest::isEqual(const char* hexdigits, std::size_t len) const
|
||||
{
|
||||
// Skip possible '_' prefix
|
||||
if (*hexdigits == '_')
|
||||
{
|
||||
++hexdigits;
|
||||
--len;
|
||||
}
|
||||
|
||||
// Incorrect length - can never match
|
||||
if (len != 2*dig_.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& byteVal : dig_)
|
||||
{
|
||||
const char upp = hexChars[((byteVal >> 4) & 0xF)];
|
||||
const char low = hexChars[(byteVal & 0xF)];
|
||||
|
||||
if (upp != *hexdigits++) return false;
|
||||
if (low != *hexdigits++) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::SHA1Digest::SHA1Digest()
|
||||
@ -113,10 +143,25 @@ bool Foam::SHA1Digest::empty() const
|
||||
}
|
||||
|
||||
|
||||
Foam::Istream& Foam::SHA1Digest::read(Istream& is)
|
||||
{
|
||||
for (auto& byteVal : dig_)
|
||||
{
|
||||
const unsigned char upp = readHexDigit(is);
|
||||
const unsigned char low = readHexDigit(is);
|
||||
|
||||
byteVal = (upp << 4) + low;
|
||||
}
|
||||
|
||||
is.check(FUNCTION_NAME);
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
std::string Foam::SHA1Digest::str(const bool prefixed) const
|
||||
{
|
||||
std::string buf;
|
||||
unsigned nChar = 0;
|
||||
std::size_t nChar = 0;
|
||||
|
||||
if (prefixed)
|
||||
{
|
||||
@ -138,21 +183,6 @@ std::string Foam::SHA1Digest::str(const bool prefixed) const
|
||||
}
|
||||
|
||||
|
||||
Foam::Istream& Foam::SHA1Digest::read(Istream& is)
|
||||
{
|
||||
for (auto& byteVal : dig_)
|
||||
{
|
||||
const unsigned char upp = readHexDigit(is);
|
||||
const unsigned char low = readHexDigit(is);
|
||||
|
||||
byteVal = (upp << 4) + low;
|
||||
}
|
||||
|
||||
is.check(FUNCTION_NAME);
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
Foam::Ostream& Foam::SHA1Digest::write(Ostream& os, const bool prefixed) const
|
||||
{
|
||||
if (prefixed)
|
||||
@ -187,29 +217,7 @@ bool Foam::SHA1Digest::operator==(const std::string& hexdigits) const
|
||||
return empty();
|
||||
}
|
||||
|
||||
// Skip possible '_' prefix
|
||||
unsigned nChar = 0;
|
||||
if (hexdigits[0] == '_')
|
||||
{
|
||||
++nChar;
|
||||
}
|
||||
|
||||
// Incorrect length - can never match
|
||||
if (hexdigits.size() != nChar + 2*dig_.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& byteVal : dig_)
|
||||
{
|
||||
const char upp = hexChars[((byteVal >> 4) & 0xF)]; // Upper nibble
|
||||
const char low = hexChars[(byteVal & 0xF)]; // Lower nibble
|
||||
|
||||
if (upp != hexdigits[nChar++]) return false;
|
||||
if (low != hexdigits[nChar++]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return isEqual(hexdigits.data(), hexdigits.length());
|
||||
}
|
||||
|
||||
|
||||
@ -221,47 +229,25 @@ bool Foam::SHA1Digest::operator==(const char* hexdigits) const
|
||||
return empty();
|
||||
}
|
||||
|
||||
// Skip possible '_' prefix
|
||||
unsigned nChar = 0;
|
||||
if (hexdigits[0] == '_')
|
||||
{
|
||||
++nChar;
|
||||
}
|
||||
|
||||
// Incorrect length - can never match
|
||||
if (strlen(hexdigits) != nChar + 2*dig_.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& byteVal : dig_)
|
||||
{
|
||||
const char upp = hexChars[((byteVal >> 4) & 0xF)];
|
||||
const char low = hexChars[(byteVal & 0xF)];
|
||||
|
||||
if (upp != hexdigits[nChar++]) return false;
|
||||
if (low != hexdigits[nChar++]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return isEqual(hexdigits, std::char_traits<char>::length(hexdigits));
|
||||
}
|
||||
|
||||
|
||||
bool Foam::SHA1Digest::operator!=(const SHA1Digest& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
return !this->operator==(rhs);
|
||||
}
|
||||
|
||||
|
||||
bool Foam::SHA1Digest::operator!=(const std::string& rhs) const
|
||||
bool Foam::SHA1Digest::operator!=(const std::string& hexdigits) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
return !this->operator==(hexdigits);
|
||||
}
|
||||
|
||||
|
||||
bool Foam::SHA1Digest::operator!=(const char* rhs) const
|
||||
bool Foam::SHA1Digest::operator!=(const char* hexdigits) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
return !this->operator==(hexdigits);
|
||||
}
|
||||
|
||||
|
||||
@ -275,7 +261,8 @@ Foam::Istream& Foam::operator>>(Istream& is, SHA1Digest& dig)
|
||||
|
||||
Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1Digest& dig)
|
||||
{
|
||||
return dig.write(os);
|
||||
// Write with prefixed = false
|
||||
return dig.write(os, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -38,8 +38,8 @@ SourceFiles
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef SHA1Digest_H
|
||||
#define SHA1Digest_H
|
||||
#ifndef Foam_SHA1Digest_H
|
||||
#define Foam_SHA1Digest_H
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
@ -74,13 +74,15 @@ class SHA1Digest
|
||||
return dig_.data();
|
||||
}
|
||||
|
||||
//- Byte-wise compare digest contents
|
||||
bool isEqual(const char* hexdigits, std::size_t len) const;
|
||||
|
||||
// Permit SHA1 to calculate the digest
|
||||
friend class SHA1;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
// Static Data Members
|
||||
|
||||
//- A null digest (ie, all zero)
|
||||
@ -89,7 +91,7 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct a zero digest
|
||||
//- Default construct a zero digest
|
||||
SHA1Digest();
|
||||
|
||||
//- Read construct a digest
|
||||
@ -120,7 +122,7 @@ public:
|
||||
// Member Operators
|
||||
|
||||
//- Equality operator
|
||||
bool operator==(const SHA1Digest&) const;
|
||||
bool operator==(const SHA1Digest& rhs) const;
|
||||
|
||||
//- Compare to (40-byte) text representation (eg, from sha1sum)
|
||||
// An %empty string is equivalent to
|
||||
@ -136,7 +138,7 @@ public:
|
||||
|
||||
|
||||
//- Inequality operator
|
||||
bool operator!=(const SHA1Digest&) const;
|
||||
bool operator!=(const SHA1Digest& rhs) const;
|
||||
|
||||
//- Inequality operator
|
||||
bool operator!=(const std::string& hexdigits) const;
|
||||
@ -149,7 +151,7 @@ public:
|
||||
// IOstream Operators
|
||||
|
||||
//- Read (40-byte) text representation (ignoring leading '_' prefix)
|
||||
Istream& operator>>(Istream&is , SHA1Digest& dig);
|
||||
Istream& operator>>(Istream& is, SHA1Digest& dig);
|
||||
|
||||
//- Write (40-byte) text representation, unquoted and without prefix
|
||||
Ostream& operator<<(Ostream& os, const SHA1Digest& dig);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2019-2021 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2022 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -26,8 +26,6 @@ License
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "SHA1.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::SHA1::SHA1()
|
||||
@ -52,31 +50,34 @@ inline Foam::SHA1::SHA1(const std::string& str)
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::SHA1& Foam::SHA1::append(const char* data, size_t len)
|
||||
inline void Foam::SHA1::append(char c)
|
||||
{
|
||||
processBytes(data, len);
|
||||
return *this;
|
||||
processBytes(&c, 1);
|
||||
}
|
||||
|
||||
|
||||
inline Foam::SHA1& Foam::SHA1::append(const char* str)
|
||||
inline void Foam::SHA1::append(const char* data, size_t len)
|
||||
{
|
||||
processBytes(data, len);
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::SHA1::append(const char* str)
|
||||
{
|
||||
if (str && *str)
|
||||
{
|
||||
processBytes(str, std::char_traits<char>::length(str));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::SHA1& Foam::SHA1::append(const std::string& str)
|
||||
inline void Foam::SHA1::append(const std::string& str)
|
||||
{
|
||||
processBytes(str.data(), str.length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::SHA1& Foam::SHA1::append
|
||||
inline void Foam::SHA1::append
|
||||
(
|
||||
const std::string& str,
|
||||
size_t pos,
|
||||
@ -92,8 +93,6 @@ inline Foam::SHA1& Foam::SHA1::append
|
||||
|
||||
processBytes(str.data() + pos, len);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@ -175,15 +174,15 @@ inline bool Foam::SHA1::operator!=(const SHA1Digest& rhs) const
|
||||
}
|
||||
|
||||
|
||||
inline bool Foam::SHA1::operator!=(const std::string& rhs) const
|
||||
inline bool Foam::SHA1::operator!=(const std::string& hexdigits) const
|
||||
{
|
||||
return !this->operator==(rhs);
|
||||
return !this->operator==(hexdigits);
|
||||
}
|
||||
|
||||
|
||||
inline bool Foam::SHA1::operator!=(const char* rhs) const
|
||||
inline bool Foam::SHA1::operator!=(const char* hexdigits) const
|
||||
{
|
||||
return !this->operator==(rhs);
|
||||
return !this->operator==(hexdigits);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user