1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "SHA1Digest.H"
27 #include "IOstreams.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 const Foam::SHA1Digest Foam::SHA1Digest::null;
36 static const char hexChars[] = "0123456789abcdef";
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 unsigned char Foam::SHA1Digest::readHexDigit(Istream& is)
44 // Takes into account that 'a' (or 'A') is 10
45 static const int alphaOffset = toupper('A') - 10;
46 // Takes into account that '0' is 0
47 static const int zeroOffset = int('0');
50 // silently ignore leading or intermediate '_'
60 FatalIOErrorIn("SHA1Digest::readHexDigit(Istream&)", is)
61 << "Illegal hex digit: '" << c << "'"
62 << exit(FatalIOError);
67 return int(c) - zeroOffset;
71 return toupper(c) - alphaOffset;
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
78 Foam::SHA1Digest::SHA1Digest()
84 Foam::SHA1Digest::SHA1Digest(Istream& is)
90 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
92 void Foam::SHA1Digest::clear()
94 memset(v_, 0, length);
98 bool Foam::SHA1Digest::empty() const
100 for (unsigned i = 0; i < length; ++i)
112 std::string Foam::SHA1Digest::str(const bool prefixed) const
119 buf.resize(1 + length*2);
124 buf.resize(length*2);
127 for (unsigned i = 0; i < length; ++i)
129 buf[nChar++] = hexChars[((v_[i] >> 4) & 0xF)];
130 buf[nChar++] = hexChars[(v_[i] & 0xF)];
137 Foam::Ostream& Foam::SHA1Digest::write(Ostream& os, const bool prefixed) const
144 for (unsigned i = 0; i < length; ++i)
146 os.write(hexChars[((v_[i] >> 4) & 0xF)]);
147 os.write(hexChars[(v_[i] & 0xF)]);
150 os.check("SHA1Digest::write(Ostream&, const bool)");
155 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
157 bool Foam::SHA1Digest::operator==(const SHA1Digest& rhs) const
159 for (unsigned i = 0; i < length; ++i)
161 if (v_[i] != rhs.v_[i])
171 bool Foam::SHA1Digest::operator==(const std::string& hexdigits) const
173 // null or empty string is not an error - interpret as '0000..'
174 if (hexdigits.empty())
179 // skip possible '_' prefix
181 if (hexdigits[0] == '_')
186 // incorrect length - can never match
187 if (hexdigits.size() != charI + length*2)
192 for (unsigned i = 0; i < length; ++i)
194 const char c1 = hexChars[((v_[i] >> 4) & 0xF)];
195 const char c2 = hexChars[(v_[i] & 0xF)];
197 if (c1 != hexdigits[charI++]) return false;
198 if (c2 != hexdigits[charI++]) return false;
205 bool Foam::SHA1Digest::operator==(const char* hexdigits) const
207 // null or empty string is not an error - interpret as '0000..'
208 if (!hexdigits || !*hexdigits)
213 // skip possible '_' prefix
215 if (hexdigits[0] == '_')
220 // incorrect length - can never match
221 if (strlen(hexdigits) != charI + length*2)
226 for (unsigned i = 0; i < length; ++i)
228 const char c1 = hexChars[((v_[i] >> 4) & 0xF)];
229 const char c2 = hexChars[(v_[i] & 0xF)];
231 if (c1 != hexdigits[charI++]) return false;
232 if (c2 != hexdigits[charI++]) return false;
239 bool Foam::SHA1Digest::operator!=(const SHA1Digest& rhs) const
241 return !operator==(rhs);
245 bool Foam::SHA1Digest::operator!=(const std::string& rhs) const
247 return !operator==(rhs);
251 bool Foam::SHA1Digest::operator!=(const char* rhs) const
253 return !operator==(rhs);
257 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
259 Foam::Istream& Foam::operator>>(Istream& is, SHA1Digest& dig)
261 unsigned char *v = dig.v_;
263 for (unsigned i = 0; i < dig.length; ++i)
265 unsigned char c1 = SHA1Digest::readHexDigit(is);
266 unsigned char c2 = SHA1Digest::readHexDigit(is);
268 v[i] = (c1 << 4) + c2;
271 is.check("Istream& operator>>(Istream&, SHA1Digest&)");
276 Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1Digest& dig)
278 return dig.write(os);
282 // ************************************************************************* //