add a simple container for HashStrings
[ntk/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 <string>
27 #include <iostream>
28 /*}}}*/
29
30 const char * HashString::_SupportedHashes[] =
31 {
32 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
33 };
34
35 HashString::HashString()
36 {
37 }
38
39 HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
40 {
41 }
42
43 HashString::HashString(std::string StringedHash) /*{{{*/
44 {
45 if (StringedHash.find(":") == std::string::npos)
46 {
47 // legacy: md5sum without "MD5Sum:" prefix
48 if (StringedHash.size() == 32)
49 {
50 Type = "MD5Sum";
51 Hash = StringedHash;
52 }
53 if(_config->FindB("Debug::Hashes",false) == true)
54 std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
55 return;
56 }
57 std::string::size_type pos = StringedHash.find(":");
58 Type = StringedHash.substr(0,pos);
59 Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
60
61 if(_config->FindB("Debug::Hashes",false) == true)
62 std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
63 }
64 /*}}}*/
65 bool HashString::VerifyFile(std::string filename) const /*{{{*/
66 {
67 std::string fileHash = GetHashForFile(filename);
68
69 if(_config->FindB("Debug::Hashes",false) == true)
70 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
71
72 return (fileHash == Hash);
73 }
74 /*}}}*/
75 bool HashString::FromFile(std::string filename) /*{{{*/
76 {
77 // pick the strongest hash
78 if (Type == "")
79 Type = _SupportedHashes[0];
80
81 Hash = GetHashForFile(filename);
82 return true;
83 }
84 /*}}}*/
85 std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
86 {
87 std::string fileHash;
88
89 FileFd Fd(filename, FileFd::ReadOnly);
90 if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
91 {
92 MD5Summation MD5;
93 MD5.AddFD(Fd);
94 fileHash = (std::string)MD5.Result();
95 }
96 else if (strcasecmp(Type.c_str(), "SHA1") == 0)
97 {
98 SHA1Summation SHA1;
99 SHA1.AddFD(Fd);
100 fileHash = (std::string)SHA1.Result();
101 }
102 else if (strcasecmp(Type.c_str(), "SHA256") == 0)
103 {
104 SHA256Summation SHA256;
105 SHA256.AddFD(Fd);
106 fileHash = (std::string)SHA256.Result();
107 }
108 else if (strcasecmp(Type.c_str(), "SHA512") == 0)
109 {
110 SHA512Summation SHA512;
111 SHA512.AddFD(Fd);
112 fileHash = (std::string)SHA512.Result();
113 }
114 Fd.Close();
115
116 return fileHash;
117 }
118 /*}}}*/
119 const char** HashString::SupportedHashes() /*{{{*/
120 {
121 return _SupportedHashes;
122 }
123 /*}}}*/
124 APT_PURE bool HashString::empty() const /*{{{*/
125 {
126 return (Type.empty() || Hash.empty());
127 }
128 /*}}}*/
129 std::string HashString::toStr() const /*{{{*/
130 {
131 return Type + ":" + Hash;
132 }
133 /*}}}*/
134 APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
135 {
136 return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
137 }
138 APT_PURE bool HashString::operator!=(HashString const &other) const
139 {
140 return !(*this == other);
141 }
142 /*}}}*/
143
144 HashString const * HashStringList::find(char const * const type) const /*{{{*/
145 {
146 if (type == NULL || type[0] == '\0')
147 {
148 std::string forcedType = _config->Find("Acquire::ForceHash", "");
149 if (forcedType.empty() == false)
150 return find(forcedType.c_str());
151 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
152 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
153 if (strcasecmp(hs->HashType().c_str(), *t) == 0)
154 return &*hs;
155 return NULL;
156 }
157 for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
158 if (strcasecmp(hs->HashType().c_str(), type) == 0)
159 return &*hs;
160 return NULL;
161 }
162 /*}}}*/
163 bool HashStringList::supported(char const * const type) /*{{{*/
164 {
165 for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
166 if (strcasecmp(*t, type) == 0)
167 return true;
168 return false;
169 }
170 /*}}}*/
171 bool HashStringList::push_back(const HashString &hashString) /*{{{*/
172 {
173 if (hashString.HashType().empty() == true ||
174 hashString.HashValue().empty() == true ||
175 supported(hashString.HashType().c_str()) == false)
176 return false;
177
178 // ensure that each type is added only once
179 HashString const * const hs = find(hashString.HashType().c_str());
180 if (hs != NULL)
181 return *hs == hashString;
182
183 list.push_back(hashString);
184 return true;
185 }
186 /*}}}*/
187 bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
188 {
189 if (list.empty() == true)
190 return false;
191 HashString const * const hs = find(NULL);
192 if (hs == NULL || hs->VerifyFile(filename) == false)
193 return false;
194 return true;
195 }
196 /*}}}*/
197 bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
198 {
199 short matches = 0;
200 for (const_iterator hs = begin(); hs != end(); ++hs)
201 {
202 HashString const * const ohs = other.find(hs->HashType());
203 if (ohs == NULL)
204 continue;
205 if (*hs != *ohs)
206 return false;
207 ++matches;
208 }
209 if (matches == 0)
210 return false;
211 return true;
212 }
213 bool HashStringList::operator!=(HashStringList const &other) const
214 {
215 return !(*this == other);
216 }
217 /*}}}*/
218
219 // Hashes::AddFD - Add the contents of the FD /*{{{*/
220 // ---------------------------------------------------------------------
221 /* */
222 bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
223 bool const addSHA1, bool const addSHA256, bool const addSHA512)
224 {
225 unsigned char Buf[64*64];
226 bool const ToEOF = (Size == UntilEOF);
227 while (Size != 0 || ToEOF)
228 {
229 unsigned long long n = sizeof(Buf);
230 if (!ToEOF) n = std::min(Size, n);
231 ssize_t const Res = read(Fd,Buf,n);
232 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
233 return false;
234 if (ToEOF && Res == 0) // EOF
235 break;
236 Size -= Res;
237 if (addMD5 == true)
238 MD5.Add(Buf,Res);
239 if (addSHA1 == true)
240 SHA1.Add(Buf,Res);
241 if (addSHA256 == true)
242 SHA256.Add(Buf,Res);
243 if (addSHA512 == true)
244 SHA512.Add(Buf,Res);
245 }
246 return true;
247 }
248 bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5,
249 bool const addSHA1, bool const addSHA256, bool const addSHA512)
250 {
251 unsigned char Buf[64*64];
252 bool const ToEOF = (Size == 0);
253 while (Size != 0 || ToEOF)
254 {
255 unsigned long long n = sizeof(Buf);
256 if (!ToEOF) n = std::min(Size, n);
257 unsigned long long a = 0;
258 if (Fd.Read(Buf, n, &a) == false) // error
259 return false;
260 if (ToEOF == false)
261 {
262 if (a != n) // short read
263 return false;
264 }
265 else if (a == 0) // EOF
266 break;
267 Size -= a;
268 if (addMD5 == true)
269 MD5.Add(Buf, a);
270 if (addSHA1 == true)
271 SHA1.Add(Buf, a);
272 if (addSHA256 == true)
273 SHA256.Add(Buf, a);
274 if (addSHA512 == true)
275 SHA512.Add(Buf, a);
276 }
277 return true;
278 }
279 /*}}}*/