1 // -*- mode: cpp; mode: fold -*-
3 // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
4 /* ######################################################################
6 Hashes - Simple wrapper around the hash functions
8 This is just used to make building the methods simpler, this is the
9 only interface required..
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/hashes.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/md5.h>
20 #include <apt-pkg/sha1.h>
21 #include <apt-pkg/sha2.h>
30 const char * HashString::_SupportedHashes
[] =
32 "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", NULL
35 HashString::HashString()
39 HashString::HashString(std::string Type
, std::string Hash
) : Type(Type
), Hash(Hash
)
43 HashString::HashString(std::string StringedHash
) /*{{{*/
45 if (StringedHash
.find(":") == std::string::npos
)
47 // legacy: md5sum without "MD5Sum:" prefix
48 if (StringedHash
.size() == 32)
53 if(_config
->FindB("Debug::Hashes",false) == true)
54 std::clog
<< "HashString(string): invalid StringedHash " << StringedHash
<< std::endl
;
57 std::string::size_type pos
= StringedHash
.find(":");
58 Type
= StringedHash
.substr(0,pos
);
59 Hash
= StringedHash
.substr(pos
+1, StringedHash
.size() - pos
);
61 if(_config
->FindB("Debug::Hashes",false) == true)
62 std::clog
<< "HashString(string): " << Type
<< " : " << Hash
<< std::endl
;
65 bool HashString::VerifyFile(std::string filename
) const /*{{{*/
67 std::string fileHash
= GetHashForFile(filename
);
69 if(_config
->FindB("Debug::Hashes",false) == true)
70 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
72 return (fileHash
== Hash
);
75 bool HashString::FromFile(std::string filename
) /*{{{*/
77 // pick the strongest hash
79 Type
= _SupportedHashes
[0];
81 Hash
= GetHashForFile(filename
);
85 std::string
HashString::GetHashForFile(std::string filename
) const /*{{{*/
89 FileFd
Fd(filename
, FileFd::ReadOnly
);
90 if(strcasecmp(Type
.c_str(), "MD5Sum") == 0)
94 fileHash
= (std::string
)MD5
.Result();
96 else if (strcasecmp(Type
.c_str(), "SHA1") == 0)
100 fileHash
= (std::string
)SHA1
.Result();
102 else if (strcasecmp(Type
.c_str(), "SHA256") == 0)
104 SHA256Summation SHA256
;
106 fileHash
= (std::string
)SHA256
.Result();
108 else if (strcasecmp(Type
.c_str(), "SHA512") == 0)
110 SHA512Summation SHA512
;
112 fileHash
= (std::string
)SHA512
.Result();
114 else if (strcasecmp(Type
.c_str(), "Checksum-FileSize") == 0)
115 strprintf(fileHash
, "%llu", Fd
.FileSize());
121 const char** HashString::SupportedHashes() /*{{{*/
123 return _SupportedHashes
;
126 APT_PURE
bool HashString::empty() const /*{{{*/
128 return (Type
.empty() || Hash
.empty());
131 std::string
HashString::toStr() const /*{{{*/
133 return Type
+ ":" + Hash
;
136 APT_PURE
bool HashString::operator==(HashString
const &other
) const /*{{{*/
138 return (strcasecmp(Type
.c_str(), other
.Type
.c_str()) == 0 && Hash
== other
.Hash
);
140 APT_PURE
bool HashString::operator!=(HashString
const &other
) const
142 return !(*this == other
);
146 bool HashStringList::usable() const /*{{{*/
150 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
151 if (forcedType
.empty() == true)
153 // FileSize alone isn't usable
154 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
155 if (hs
->HashType() != "Checksum-FileSize")
159 return find(forcedType
) != NULL
;
162 HashString
const * HashStringList::find(char const * const type
) const /*{{{*/
164 if (type
== NULL
|| type
[0] == '\0')
166 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
167 if (forcedType
.empty() == false)
168 return find(forcedType
.c_str());
169 for (char const * const * t
= HashString::SupportedHashes(); *t
!= NULL
; ++t
)
170 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
171 if (strcasecmp(hs
->HashType().c_str(), *t
) == 0)
175 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
176 if (strcasecmp(hs
->HashType().c_str(), type
) == 0)
181 bool HashStringList::supported(char const * const type
) /*{{{*/
183 for (char const * const * t
= HashString::SupportedHashes(); *t
!= NULL
; ++t
)
184 if (strcasecmp(*t
, type
) == 0)
189 bool HashStringList::push_back(const HashString
&hashString
) /*{{{*/
191 if (hashString
.HashType().empty() == true ||
192 hashString
.HashValue().empty() == true ||
193 supported(hashString
.HashType().c_str()) == false)
196 // ensure that each type is added only once
197 HashString
const * const hs
= find(hashString
.HashType().c_str());
199 return *hs
== hashString
;
201 list
.push_back(hashString
);
205 bool HashStringList::VerifyFile(std::string filename
) const /*{{{*/
207 if (usable() == false)
210 Hashes
hashes(*this);
211 FileFd
file(filename
, FileFd::ReadOnly
);
212 HashString
const * const hsf
= find("Checksum-FileSize");
215 std::string fileSize
;
216 strprintf(fileSize
, "%llu", file
.FileSize());
217 if (hsf
->HashValue() != fileSize
)
221 HashStringList
const hsl
= hashes
.GetHashStringList();
225 bool HashStringList::operator==(HashStringList
const &other
) const /*{{{*/
227 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
228 if (forcedType
.empty() == false)
230 HashString
const * const hs
= find(forcedType
);
231 HashString
const * const ohs
= other
.find(forcedType
);
232 if (hs
== NULL
|| ohs
== NULL
)
237 for (const_iterator hs
= begin(); hs
!= end(); ++hs
)
239 HashString
const * const ohs
= other
.find(hs
->HashType());
250 bool HashStringList::operator!=(HashStringList
const &other
) const
252 return !(*this == other
);
256 // PrivateHashes /*{{{*/
257 class PrivateHashes
{
259 unsigned long long FileSize
;
260 unsigned int CalcHashes
;
262 PrivateHashes(unsigned int const CalcHashes
) : FileSize(0), CalcHashes(CalcHashes
) {}
265 // Hashes::Add* - Add the contents of data or FD /*{{{*/
266 bool Hashes::Add(const unsigned char * const Data
, unsigned long long const Size
)
269 APT_IGNORE_DEPRECATED_PUSH
270 if ((d
->CalcHashes
& MD5SUM
) == MD5SUM
)
271 Res
&= MD5
.Add(Data
, Size
);
272 if ((d
->CalcHashes
& SHA1SUM
) == SHA1SUM
)
273 Res
&= SHA1
.Add(Data
, Size
);
274 if ((d
->CalcHashes
& SHA256SUM
) == SHA256SUM
)
275 Res
&= SHA256
.Add(Data
, Size
);
276 if ((d
->CalcHashes
& SHA512SUM
) == SHA512SUM
)
277 Res
&= SHA512
.Add(Data
, Size
);
278 APT_IGNORE_DEPRECATED_POP
282 bool Hashes::Add(const unsigned char * const Data
, unsigned long long const Size
, unsigned int const Hashes
)
284 d
->CalcHashes
= Hashes
;
285 return Add(Data
, Size
);
287 bool Hashes::AddFD(int const Fd
,unsigned long long Size
)
289 unsigned char Buf
[64*64];
290 bool const ToEOF
= (Size
== UntilEOF
);
291 while (Size
!= 0 || ToEOF
)
293 unsigned long long n
= sizeof(Buf
);
294 if (!ToEOF
) n
= std::min(Size
, n
);
295 ssize_t
const Res
= read(Fd
,Buf
,n
);
296 if (Res
< 0 || (!ToEOF
&& Res
!= (ssize_t
) n
)) // error, or short read
298 if (ToEOF
&& Res
== 0) // EOF
301 if (Add(Buf
, Res
) == false)
306 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, unsigned int const Hashes
)
308 d
->CalcHashes
= Hashes
;
309 return AddFD(Fd
, Size
);
311 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
)
313 unsigned char Buf
[64*64];
314 bool const ToEOF
= (Size
== 0);
315 while (Size
!= 0 || ToEOF
)
317 unsigned long long n
= sizeof(Buf
);
318 if (!ToEOF
) n
= std::min(Size
, n
);
319 unsigned long long a
= 0;
320 if (Fd
.Read(Buf
, n
, &a
) == false) // error
324 if (a
!= n
) // short read
327 else if (a
== 0) // EOF
330 if (Add(Buf
, a
) == false)
335 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
, unsigned int const Hashes
)
337 d
->CalcHashes
= Hashes
;
338 return AddFD(Fd
, Size
);
341 HashStringList
Hashes::GetHashStringList()
343 HashStringList hashes
;
344 APT_IGNORE_DEPRECATED_PUSH
345 if ((d
->CalcHashes
& MD5SUM
) == MD5SUM
)
346 hashes
.push_back(HashString("MD5Sum", MD5
.Result().Value()));
347 if ((d
->CalcHashes
& SHA1SUM
) == SHA1SUM
)
348 hashes
.push_back(HashString("SHA1", SHA1
.Result().Value()));
349 if ((d
->CalcHashes
& SHA256SUM
) == SHA256SUM
)
350 hashes
.push_back(HashString("SHA256", SHA256
.Result().Value()));
351 if ((d
->CalcHashes
& SHA512SUM
) == SHA512SUM
)
352 hashes
.push_back(HashString("SHA512", SHA512
.Result().Value()));
353 APT_IGNORE_DEPRECATED_POP
355 strprintf(SizeStr
, "%llu", d
->FileSize
);
356 hashes
.push_back(HashString("Checksum-FileSize", SizeStr
));
359 APT_IGNORE_DEPRECATED_PUSH
360 Hashes::Hashes() { d
= new PrivateHashes(~0); }
361 Hashes::Hashes(unsigned int const Hashes
) { d
= new PrivateHashes(Hashes
); }
362 Hashes::Hashes(HashStringList
const &Hashes
) {
363 unsigned int calcHashes
= Hashes
.usable() ? 0 : ~0;
364 if (Hashes
.find("MD5Sum") != NULL
)
365 calcHashes
|= MD5SUM
;
366 if (Hashes
.find("SHA1") != NULL
)
367 calcHashes
|= SHA1SUM
;
368 if (Hashes
.find("SHA256") != NULL
)
369 calcHashes
|= SHA256SUM
;
370 if (Hashes
.find("SHA512") != NULL
)
371 calcHashes
|= SHA512SUM
;
372 d
= new PrivateHashes(calcHashes
);
374 Hashes::~Hashes() { delete d
; }
375 APT_IGNORE_DEPRECATED_POP