]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
41a0037cd5ac9974cd6fee41770d54e0ef3a3346
[apt.git] / apt-pkg / contrib / hashes.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
4 /* ######################################################################
5
6 Hashes - Simple wrapper around the hash functions
7
8 This is just used to make building the methods simpler, this is the
9 only interface required..
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #include <config.h>
15
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>
22
23 #include <stddef.h>
24 #include <algorithm>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string>
28 #include <iostream>
29 /*}}}*/
30
31 const char * HashString::_SupportedHashes[] =
32 {
33 "SHA512", "SHA256", "SHA1", "MD5Sum", "Checksum-FileSize", NULL
34 };
35
36 HashString::HashString()
37 {
38 }
39
40 HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
41 {
42 }
43
44 HashString::HashString(std::string StringedHash) /*{{{*/
45 {
46 if (StringedHash.find(":") == std::string::npos)
47 {
48 // legacy: md5sum without "MD5Sum:" prefix
49 if (StringedHash.size() == 32)
50 {
51 Type = "MD5Sum";
52 Hash = StringedHash;
53 }
54 if(_config->FindB("Debug::Hashes",false) == true)
55 std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
56 return;
57 }
58 std::string::size_type pos = StringedHash.find(":");
59 Type = StringedHash.substr(0,pos);
60 Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
61
62 if(_config->FindB("Debug::Hashes",false) == true)
63 std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
64 }
65 /*}}}*/
66 bool HashString::VerifyFile(std::string filename) const /*{{{*/
67 {
68 std::string fileHash = GetHashForFile(filename);
69
70 if(_config->FindB("Debug::Hashes",false) == true)
71 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
72
73 return (fileHash == Hash);
74 }
75 /*}}}*/
76 bool HashString::FromFile(std::string filename) /*{{{*/
77 {
78 // pick the strongest hash
79 if (Type == "")
80 Type = _SupportedHashes[0];
81
82 Hash = GetHashForFile(filename);
83 return true;
84 }
85 /*}}}*/
86 std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
87 {
88 std::string fileHash;
89
90 FileFd Fd(filename, FileFd::ReadOnly);
91 if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
92 {
93 MD5Summation MD5;
94 MD5.AddFD(Fd);
95 fileHash = (std::string)MD5.Result();
96 }
97 else if (strcasecmp(Type.c_str(), "SHA1") == 0)
98 {
99 SHA1Summation SHA1;
100 SHA1.AddFD(Fd);
101 fileHash = (std::string)SHA1.Result();
102 }
103 else if (strcasecmp(Type.c_str(), "SHA256") == 0)
104 {
105 SHA256Summation SHA256;
106 SHA256.AddFD(Fd);
107 fileHash = (std::string)SHA256.Result();
108 }
109 else if (strcasecmp(Type.c_str(), "SHA512") == 0)
110 {
111 SHA512Summation SHA512;
112 SHA512.AddFD(Fd);
113 fileHash = (std::string)SHA512.Result();
114 }
115 else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0)
116 strprintf(fileHash, "%llu", Fd.FileSize());
117 Fd.Close();
118
119 return fileHash;
120 }
121 /*}}}*/
122 const char** HashString::SupportedHashes() /*{{{*/
123 {
124 return _SupportedHashes;
125 }
126 /*}}}*/
127 APT_PURE bool HashString::empty() const /*{{{*/
128 {
129 return (Type.empty() || Hash.empty());
130 }
131 /*}}}*/
132 APT_PURE bool HashString::usable() const /*{{{*/
133 {
134 return (
135 (Type != "Checksum-FileSize") &&
136 (Type != "MD5Sum")
137 );
138 }
139 std::string HashString::toStr() const /*{{{*/
140 {
141 return Type + ":" + Hash;
142 }
143 /*}}}*/
144 APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
145 {
146 return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
147 }
148 APT_PURE bool HashString::operator!=(HashString const &other) const
149 {
150 return !(*this == other);
151 }
152 /*}}}*/
153
154 bool HashStringList::usable() const /*{{{*/
155 {
156 if (empty() == true)
157 return false;
158 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
159 if (forcedType.empty() == true)
160 {
161 // See if there is at least one usable hash
162 for (auto const &hs: list)
163 if (hs.usable())
164 return true;
165 return false;
166 }
167 return find(forcedType) != NULL;
168 }
169 /*}}}*/
170 HashString const * HashStringList::find(char const * const type) const /*{{{*/
171 {
172 if (type == NULL || type[0] == '\0')
173 {
174 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
175 if (forcedType.empty() == false)
176 return find(forcedType.c_str());
177 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
178 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
179 if (strcasecmp(hs->HashType().c_str(), *t) == 0)
180 return &*hs;
181 return NULL;
182 }
183 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
184 if (strcasecmp(hs->HashType().c_str(), type) == 0)
185 return &*hs;
186 return NULL;
187 }
188 /*}}}*/
189 unsigned long long HashStringList::FileSize() const /*{{{*/
190 {
191 HashString const * const hsf = find("Checksum-FileSize");
192 if (hsf == NULL)
193 return 0;
194 std::string const hv = hsf->HashValue();
195 return strtoull(hv.c_str(), NULL, 10);
196 }
197 /*}}}*/
198 bool HashStringList::FileSize(unsigned long long const Size) /*{{{*/
199 {
200 std::string size;
201 strprintf(size, "%llu", Size);
202 return push_back(HashString("Checksum-FileSize", size));
203 }
204 /*}}}*/
205 bool HashStringList::supported(char const * const type) /*{{{*/
206 {
207 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
208 if (strcasecmp(*t, type) == 0)
209 return true;
210 return false;
211 }
212 /*}}}*/
213 bool HashStringList::push_back(const HashString &hashString) /*{{{*/
214 {
215 if (hashString.HashType().empty() == true ||
216 hashString.HashValue().empty() == true ||
217 supported(hashString.HashType().c_str()) == false)
218 return false;
219
220 // ensure that each type is added only once
221 HashString const * const hs = find(hashString.HashType().c_str());
222 if (hs != NULL)
223 return *hs == hashString;
224
225 list.push_back(hashString);
226 return true;
227 }
228 /*}}}*/
229 bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
230 {
231 if (usable() == false)
232 return false;
233
234 Hashes hashes(*this);
235 FileFd file(filename, FileFd::ReadOnly);
236 HashString const * const hsf = find("Checksum-FileSize");
237 if (hsf != NULL)
238 {
239 std::string fileSize;
240 strprintf(fileSize, "%llu", file.FileSize());
241 if (hsf->HashValue() != fileSize)
242 return false;
243 }
244 hashes.AddFD(file);
245 HashStringList const hsl = hashes.GetHashStringList();
246 return hsl == *this;
247 }
248 /*}}}*/
249 bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
250 {
251 std::string const forcedType = _config->Find("Acquire::ForceHash", "");
252 if (forcedType.empty() == false)
253 {
254 HashString const * const hs = find(forcedType);
255 HashString const * const ohs = other.find(forcedType);
256 if (hs == NULL || ohs == NULL)
257 return false;
258 return *hs == *ohs;
259 }
260 short matches = 0;
261 for (const_iterator hs = begin(); hs != end(); ++hs)
262 {
263 HashString const * const ohs = other.find(hs->HashType());
264 if (ohs == NULL)
265 continue;
266 if (*hs != *ohs)
267 return false;
268 ++matches;
269 }
270 if (matches == 0)
271 return false;
272 return true;
273 }
274 bool HashStringList::operator!=(HashStringList const &other) const
275 {
276 return !(*this == other);
277 }
278 /*}}}*/
279
280 // PrivateHashes /*{{{*/
281 class PrivateHashes {
282 public:
283 unsigned long long FileSize;
284 unsigned int CalcHashes;
285
286 explicit PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
287 explicit PrivateHashes(HashStringList const &Hashes) : FileSize(0) {
288 unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
289 if (Hashes.find("MD5Sum") != NULL)
290 calcHashes |= Hashes::MD5SUM;
291 if (Hashes.find("SHA1") != NULL)
292 calcHashes |= Hashes::SHA1SUM;
293 if (Hashes.find("SHA256") != NULL)
294 calcHashes |= Hashes::SHA256SUM;
295 if (Hashes.find("SHA512") != NULL)
296 calcHashes |= Hashes::SHA512SUM;
297 CalcHashes = calcHashes;
298 }
299 };
300 /*}}}*/
301 // Hashes::Add* - Add the contents of data or FD /*{{{*/
302 bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
303 {
304 bool Res = true;
305 APT_IGNORE_DEPRECATED_PUSH
306 if ((d->CalcHashes & MD5SUM) == MD5SUM)
307 Res &= MD5.Add(Data, Size);
308 if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
309 Res &= SHA1.Add(Data, Size);
310 if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
311 Res &= SHA256.Add(Data, Size);
312 if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
313 Res &= SHA512.Add(Data, Size);
314 APT_IGNORE_DEPRECATED_POP
315 d->FileSize += Size;
316 return Res;
317 }
318 bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes)
319 {
320 d->CalcHashes = Hashes;
321 return Add(Data, Size);
322 }
323 bool Hashes::AddFD(int const Fd,unsigned long long Size)
324 {
325 unsigned char Buf[64*64];
326 bool const ToEOF = (Size == UntilEOF);
327 while (Size != 0 || ToEOF)
328 {
329 unsigned long long n = sizeof(Buf);
330 if (!ToEOF) n = std::min(Size, n);
331 ssize_t const Res = read(Fd,Buf,n);
332 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
333 return false;
334 if (ToEOF && Res == 0) // EOF
335 break;
336 Size -= Res;
337 if (Add(Buf, Res) == false)
338 return false;
339 }
340 return true;
341 }
342 bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
343 {
344 d->CalcHashes = Hashes;
345 return AddFD(Fd, Size);
346 }
347 bool Hashes::AddFD(FileFd &Fd,unsigned long long Size)
348 {
349 unsigned char Buf[64*64];
350 bool const ToEOF = (Size == 0);
351 while (Size != 0 || ToEOF)
352 {
353 unsigned long long n = sizeof(Buf);
354 if (!ToEOF) n = std::min(Size, n);
355 unsigned long long a = 0;
356 if (Fd.Read(Buf, n, &a) == false) // error
357 return false;
358 if (ToEOF == false)
359 {
360 if (a != n) // short read
361 return false;
362 }
363 else if (a == 0) // EOF
364 break;
365 Size -= a;
366 if (Add(Buf, a) == false)
367 return false;
368 }
369 return true;
370 }
371 bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
372 {
373 d->CalcHashes = Hashes;
374 return AddFD(Fd, Size);
375 }
376 /*}}}*/
377 HashStringList Hashes::GetHashStringList()
378 {
379 HashStringList hashes;
380 APT_IGNORE_DEPRECATED_PUSH
381 if ((d->CalcHashes & MD5SUM) == MD5SUM)
382 hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
383 if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
384 hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
385 if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
386 hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
387 if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
388 hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
389 APT_IGNORE_DEPRECATED_POP
390 hashes.FileSize(d->FileSize);
391 return hashes;
392 }
393 APT_IGNORE_DEPRECATED_PUSH
394 Hashes::Hashes() : d(new PrivateHashes(~0)) { }
395 Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {}
396 Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {}
397 Hashes::~Hashes() { delete d; }
398 APT_IGNORE_DEPRECATED_POP