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>
31 const char * HashString::_SupportedHashes
[] =
33 "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", NULL
36 HashString::HashString()
40 HashString::HashString(std::string Type
, std::string Hash
) : Type(Type
), Hash(Hash
)
44 HashString::HashString(std::string StringedHash
) /*{{{*/
46 if (StringedHash
.find(":") == std::string::npos
)
48 // legacy: md5sum without "MD5Sum:" prefix
49 if (StringedHash
.size() == 32)
54 if(_config
->FindB("Debug::Hashes",false) == true)
55 std::clog
<< "HashString(string): invalid StringedHash " << StringedHash
<< std::endl
;
58 std::string::size_type pos
= StringedHash
.find(":");
59 Type
= StringedHash
.substr(0,pos
);
60 Hash
= StringedHash
.substr(pos
+1, StringedHash
.size() - pos
);
62 if(_config
->FindB("Debug::Hashes",false) == true)
63 std::clog
<< "HashString(string): " << Type
<< " : " << Hash
<< std::endl
;
66 bool HashString::VerifyFile(std::string filename
) const /*{{{*/
68 std::string fileHash
= GetHashForFile(filename
);
70 if(_config
->FindB("Debug::Hashes",false) == true)
71 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
73 return (fileHash
== Hash
);
76 bool HashString::FromFile(std::string filename
) /*{{{*/
78 // pick the strongest hash
80 Type
= _SupportedHashes
[0];
82 Hash
= GetHashForFile(filename
);
86 std::string
HashString::GetHashForFile(std::string filename
) const /*{{{*/
90 FileFd
Fd(filename
, FileFd::ReadOnly
);
91 if(strcasecmp(Type
.c_str(), "MD5Sum") == 0)
95 fileHash
= (std::string
)MD5
.Result();
97 else if (strcasecmp(Type
.c_str(), "SHA1") == 0)
101 fileHash
= (std::string
)SHA1
.Result();
103 else if (strcasecmp(Type
.c_str(), "SHA256") == 0)
105 SHA256Summation SHA256
;
107 fileHash
= (std::string
)SHA256
.Result();
109 else if (strcasecmp(Type
.c_str(), "SHA512") == 0)
111 SHA512Summation SHA512
;
113 fileHash
= (std::string
)SHA512
.Result();
115 else if (strcasecmp(Type
.c_str(), "Checksum-FileSize") == 0)
116 strprintf(fileHash
, "%llu", Fd
.FileSize());
122 const char** HashString::SupportedHashes() /*{{{*/
124 return _SupportedHashes
;
127 APT_PURE
bool HashString::empty() const /*{{{*/
129 return (Type
.empty() || Hash
.empty());
132 APT_PURE
bool HashString::usable() const /*{{{*/
135 (Type
!= "Checksum-FileSize") &&
136 (Type
!= "MD5Sum") &&
141 std::string
HashString::toStr() const /*{{{*/
143 return Type
+ ":" + Hash
;
146 APT_PURE
bool HashString::operator==(HashString
const &other
) const /*{{{*/
148 return (strcasecmp(Type
.c_str(), other
.Type
.c_str()) == 0 && Hash
== other
.Hash
);
150 APT_PURE
bool HashString::operator!=(HashString
const &other
) const
152 return !(*this == other
);
156 bool HashStringList::usable() const /*{{{*/
160 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
161 if (forcedType
.empty() == true)
163 // See if there is at least one usable hash
164 for (auto const &hs
: list
)
169 return find(forcedType
) != NULL
;
172 HashString
const * HashStringList::find(char const * const type
) const /*{{{*/
174 if (type
== NULL
|| type
[0] == '\0')
176 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
177 if (forcedType
.empty() == false)
178 return find(forcedType
.c_str());
179 for (char const * const * t
= HashString::SupportedHashes(); *t
!= NULL
; ++t
)
180 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
181 if (strcasecmp(hs
->HashType().c_str(), *t
) == 0)
185 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
186 if (strcasecmp(hs
->HashType().c_str(), type
) == 0)
191 unsigned long long HashStringList::FileSize() const /*{{{*/
193 HashString
const * const hsf
= find("Checksum-FileSize");
196 std::string
const hv
= hsf
->HashValue();
197 return strtoull(hv
.c_str(), NULL
, 10);
200 bool HashStringList::FileSize(unsigned long long const Size
) /*{{{*/
203 strprintf(size
, "%llu", Size
);
204 return push_back(HashString("Checksum-FileSize", size
));
207 bool HashStringList::supported(char const * const type
) /*{{{*/
209 for (char const * const * t
= HashString::SupportedHashes(); *t
!= NULL
; ++t
)
210 if (strcasecmp(*t
, type
) == 0)
215 bool HashStringList::push_back(const HashString
&hashString
) /*{{{*/
217 if (hashString
.HashType().empty() == true ||
218 hashString
.HashValue().empty() == true ||
219 supported(hashString
.HashType().c_str()) == false)
222 // ensure that each type is added only once
223 HashString
const * const hs
= find(hashString
.HashType().c_str());
225 return *hs
== hashString
;
227 list
.push_back(hashString
);
231 bool HashStringList::VerifyFile(std::string filename
) const /*{{{*/
233 if (usable() == false)
236 Hashes
hashes(*this);
237 FileFd
file(filename
, FileFd::ReadOnly
);
238 HashString
const * const hsf
= find("Checksum-FileSize");
241 std::string fileSize
;
242 strprintf(fileSize
, "%llu", file
.FileSize());
243 if (hsf
->HashValue() != fileSize
)
247 HashStringList
const hsl
= hashes
.GetHashStringList();
251 bool HashStringList::operator==(HashStringList
const &other
) const /*{{{*/
253 std::string
const forcedType
= _config
->Find("Acquire::ForceHash", "");
254 if (forcedType
.empty() == false)
256 HashString
const * const hs
= find(forcedType
);
257 HashString
const * const ohs
= other
.find(forcedType
);
258 if (hs
== NULL
|| ohs
== NULL
)
263 for (const_iterator hs
= begin(); hs
!= end(); ++hs
)
265 HashString
const * const ohs
= other
.find(hs
->HashType());
276 bool HashStringList::operator!=(HashStringList
const &other
) const
278 return !(*this == other
);
282 // PrivateHashes /*{{{*/
283 class PrivateHashes
{
285 unsigned long long FileSize
;
286 unsigned int CalcHashes
;
288 explicit PrivateHashes(unsigned int const CalcHashes
) : FileSize(0), CalcHashes(CalcHashes
) {}
289 explicit PrivateHashes(HashStringList
const &Hashes
) : FileSize(0) {
290 unsigned int calcHashes
= Hashes
.usable() ? 0 : ~0;
291 if (Hashes
.find("MD5Sum") != NULL
)
292 calcHashes
|= Hashes::MD5SUM
;
293 if (Hashes
.find("SHA1") != NULL
)
294 calcHashes
|= Hashes::SHA1SUM
;
295 if (Hashes
.find("SHA256") != NULL
)
296 calcHashes
|= Hashes::SHA256SUM
;
297 if (Hashes
.find("SHA512") != NULL
)
298 calcHashes
|= Hashes::SHA512SUM
;
299 CalcHashes
= calcHashes
;
303 // Hashes::Add* - Add the contents of data or FD /*{{{*/
304 bool Hashes::Add(const unsigned char * const Data
, unsigned long long const Size
)
307 APT_IGNORE_DEPRECATED_PUSH
308 if ((d
->CalcHashes
& MD5SUM
) == MD5SUM
)
309 Res
&= MD5
.Add(Data
, Size
);
310 if ((d
->CalcHashes
& SHA1SUM
) == SHA1SUM
)
311 Res
&= SHA1
.Add(Data
, Size
);
312 if ((d
->CalcHashes
& SHA256SUM
) == SHA256SUM
)
313 Res
&= SHA256
.Add(Data
, Size
);
314 if ((d
->CalcHashes
& SHA512SUM
) == SHA512SUM
)
315 Res
&= SHA512
.Add(Data
, Size
);
316 APT_IGNORE_DEPRECATED_POP
320 bool Hashes::Add(const unsigned char * const Data
, unsigned long long const Size
, unsigned int const Hashes
)
322 d
->CalcHashes
= Hashes
;
323 return Add(Data
, Size
);
325 bool Hashes::AddFD(int const Fd
,unsigned long long Size
)
327 unsigned char Buf
[64*64];
328 bool const ToEOF
= (Size
== UntilEOF
);
329 while (Size
!= 0 || ToEOF
)
331 unsigned long long n
= sizeof(Buf
);
332 if (!ToEOF
) n
= std::min(Size
, n
);
333 ssize_t
const Res
= read(Fd
,Buf
,n
);
334 if (Res
< 0 || (!ToEOF
&& Res
!= (ssize_t
) n
)) // error, or short read
336 if (ToEOF
&& Res
== 0) // EOF
339 if (Add(Buf
, Res
) == false)
344 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, unsigned int const Hashes
)
346 d
->CalcHashes
= Hashes
;
347 return AddFD(Fd
, Size
);
349 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
)
351 unsigned char Buf
[64*64];
352 bool const ToEOF
= (Size
== 0);
353 while (Size
!= 0 || ToEOF
)
355 unsigned long long n
= sizeof(Buf
);
356 if (!ToEOF
) n
= std::min(Size
, n
);
357 unsigned long long a
= 0;
358 if (Fd
.Read(Buf
, n
, &a
) == false) // error
362 if (a
!= n
) // short read
365 else if (a
== 0) // EOF
368 if (Add(Buf
, a
) == false)
373 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
, unsigned int const Hashes
)
375 d
->CalcHashes
= Hashes
;
376 return AddFD(Fd
, Size
);
379 HashStringList
Hashes::GetHashStringList()
381 HashStringList hashes
;
382 APT_IGNORE_DEPRECATED_PUSH
383 if ((d
->CalcHashes
& MD5SUM
) == MD5SUM
)
384 hashes
.push_back(HashString("MD5Sum", MD5
.Result().Value()));
385 if ((d
->CalcHashes
& SHA1SUM
) == SHA1SUM
)
386 hashes
.push_back(HashString("SHA1", SHA1
.Result().Value()));
387 if ((d
->CalcHashes
& SHA256SUM
) == SHA256SUM
)
388 hashes
.push_back(HashString("SHA256", SHA256
.Result().Value()));
389 if ((d
->CalcHashes
& SHA512SUM
) == SHA512SUM
)
390 hashes
.push_back(HashString("SHA512", SHA512
.Result().Value()));
391 APT_IGNORE_DEPRECATED_POP
392 hashes
.FileSize(d
->FileSize
);
395 APT_IGNORE_DEPRECATED_PUSH
396 Hashes::Hashes() : d(new PrivateHashes(~0)) { }
397 Hashes::Hashes(unsigned int const Hashes
) : d(new PrivateHashes(Hashes
)) {}
398 Hashes::Hashes(HashStringList
const &Hashes
) : d(new PrivateHashes(Hashes
)) {}
399 Hashes::~Hashes() { delete d
; }
400 APT_IGNORE_DEPRECATED_POP