]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
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", 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();
119 const char** HashString::SupportedHashes() /*{{{*/
121 return _SupportedHashes
;
124 APT_PURE
bool HashString::empty() const /*{{{*/
126 return (Type
.empty() || Hash
.empty());
129 std::string
HashString::toStr() const /*{{{*/
131 return Type
+ ":" + Hash
;
134 APT_PURE
bool HashString::operator==(HashString
const &other
) const /*{{{*/
136 return (strcasecmp(Type
.c_str(), other
.Type
.c_str()) == 0 && Hash
== other
.Hash
);
138 APT_PURE
bool HashString::operator!=(HashString
const &other
) const
140 return !(*this == other
);
144 HashString
const * HashStringList::find(char const * const type
) const /*{{{*/
146 if (type
== NULL
|| type
[0] == '\0')
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)
157 for (std::vector
<HashString
>::const_iterator hs
= list
.begin(); hs
!= list
.end(); ++hs
)
158 if (strcasecmp(hs
->HashType().c_str(), type
) == 0)
163 bool HashStringList::supported(char const * const type
) /*{{{*/
165 for (char const * const * t
= HashString::SupportedHashes(); *t
!= NULL
; ++t
)
166 if (strcasecmp(*t
, type
) == 0)
171 bool HashStringList::push_back(const HashString
&hashString
) /*{{{*/
173 if (hashString
.HashType().empty() == true ||
174 hashString
.HashValue().empty() == true ||
175 supported(hashString
.HashType().c_str()) == false)
178 // ensure that each type is added only once
179 HashString
const * const hs
= find(hashString
.HashType().c_str());
181 return *hs
== hashString
;
183 list
.push_back(hashString
);
187 bool HashStringList::VerifyFile(std::string filename
) const /*{{{*/
189 if (list
.empty() == true)
191 HashString
const * const hs
= find(NULL
);
192 if (hs
== NULL
|| hs
->VerifyFile(filename
) == false)
197 bool HashStringList::operator==(HashStringList
const &other
) const /*{{{*/
200 for (const_iterator hs
= begin(); hs
!= end(); ++hs
)
202 HashString
const * const ohs
= other
.find(hs
->HashType());
213 bool HashStringList::operator!=(HashStringList
const &other
) const
215 return !(*this == other
);
219 // Hashes::AddFD - Add the contents of the FD /*{{{*/
220 // ---------------------------------------------------------------------
222 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, bool const addMD5
,
223 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
225 unsigned char Buf
[64*64];
226 bool const ToEOF
= (Size
== UntilEOF
);
227 while (Size
!= 0 || ToEOF
)
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
234 if (ToEOF
&& Res
== 0) // EOF
241 if (addSHA256
== true)
243 if (addSHA512
== true)
248 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
, bool const addMD5
,
249 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
251 unsigned char Buf
[64*64];
252 bool const ToEOF
= (Size
== 0);
253 while (Size
!= 0 || ToEOF
)
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
262 if (a
!= n
) // short read
265 else if (a
== 0) // EOF
272 if (addSHA256
== true)
274 if (addSHA512
== true)