]>
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 // legacy: md5sum without "MD5Sum:" prefix
46 if (StringedHash
.find(":") == std::string::npos
&& StringedHash
.size() == 32)
52 std::string::size_type pos
= StringedHash
.find(":");
53 Type
= StringedHash
.substr(0,pos
);
54 Hash
= StringedHash
.substr(pos
+1, StringedHash
.size() - pos
);
56 if(_config
->FindB("Debug::Hashes",false) == true)
57 std::clog
<< "HashString(string): " << Type
<< " : " << Hash
<< std::endl
;
60 bool HashString::VerifyFile(std::string filename
) const /*{{{*/
62 std::string fileHash
= GetHashForFile(filename
);
64 if(_config
->FindB("Debug::Hashes",false) == true)
65 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
67 return (fileHash
== Hash
);
70 bool HashString::FromFile(std::string filename
) /*{{{*/
72 // pick the strongest hash
74 Type
= _SupportedHashes
[0];
76 Hash
= GetHashForFile(filename
);
80 std::string
HashString::GetHashForFile(std::string filename
) const /*{{{*/
84 FileFd
Fd(filename
, FileFd::ReadOnly
);
89 fileHash
= (std::string
)MD5
.Result();
91 else if (Type
== "SHA1")
95 fileHash
= (std::string
)SHA1
.Result();
97 else if (Type
== "SHA256")
99 SHA256Summation SHA256
;
101 fileHash
= (std::string
)SHA256
.Result();
103 else if (Type
== "SHA512")
105 SHA512Summation SHA512
;
107 fileHash
= (std::string
)SHA512
.Result();
114 const char** HashString::SupportedHashes()
116 return _SupportedHashes
;
119 APT_PURE
bool HashString::empty() const
121 return (Type
.empty() || Hash
.empty());
124 std::string
HashString::toStr() const
126 return Type
+ std::string(":") + Hash
;
129 // Hashes::AddFD - Add the contents of the FD /*{{{*/
130 // ---------------------------------------------------------------------
132 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, bool const addMD5
,
133 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
135 unsigned char Buf
[64*64];
136 bool const ToEOF
= (Size
== UntilEOF
);
137 while (Size
!= 0 || ToEOF
)
139 unsigned long long n
= sizeof(Buf
);
140 if (!ToEOF
) n
= std::min(Size
, n
);
141 ssize_t
const Res
= read(Fd
,Buf
,n
);
142 if (Res
< 0 || (!ToEOF
&& Res
!= (ssize_t
) n
)) // error, or short read
144 if (ToEOF
&& Res
== 0) // EOF
151 if (addSHA256
== true)
153 if (addSHA512
== true)
158 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
, bool const addMD5
,
159 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
161 unsigned char Buf
[64*64];
162 bool const ToEOF
= (Size
== 0);
163 while (Size
!= 0 || ToEOF
)
165 unsigned long long n
= sizeof(Buf
);
166 if (!ToEOF
) n
= std::min(Size
, n
);
167 unsigned long long a
= 0;
168 if (Fd
.Read(Buf
, n
, &a
) == false) // error
172 if (a
!= n
) // short read
175 else if (a
== 0) // EOF
182 if (addSHA256
== true)
184 if (addSHA512
== true)