]>
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/macros.h>
26 const char* HashString::_SupportedHashes
[] =
28 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
31 HashString::HashString()
35 HashString::HashString(std::string Type
, std::string Hash
) : Type(Type
), Hash(Hash
)
39 HashString::HashString(std::string StringedHash
) /*{{{*/
41 // legacy: md5sum without "MD5Sum:" prefix
42 if (StringedHash
.find(":") == std::string::npos
&& StringedHash
.size() == 32)
48 std::string::size_type pos
= StringedHash
.find(":");
49 Type
= StringedHash
.substr(0,pos
);
50 Hash
= StringedHash
.substr(pos
+1, StringedHash
.size() - pos
);
52 if(_config
->FindB("Debug::Hashes",false) == true)
53 std::clog
<< "HashString(string): " << Type
<< " : " << Hash
<< std::endl
;
56 bool HashString::VerifyFile(std::string filename
) const /*{{{*/
58 std::string fileHash
= GetHashForFile(filename
);
60 if(_config
->FindB("Debug::Hashes",false) == true)
61 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
63 return (fileHash
== Hash
);
66 bool HashString::FromFile(std::string filename
) /*{{{*/
68 // pick the strongest hash
70 Type
= _SupportedHashes
[0];
72 Hash
= GetHashForFile(filename
);
76 std::string
HashString::GetHashForFile(std::string filename
) const /*{{{*/
80 FileFd
Fd(filename
, FileFd::ReadOnly
);
85 fileHash
= (std::string
)MD5
.Result();
87 else if (Type
== "SHA1")
91 fileHash
= (std::string
)SHA1
.Result();
93 else if (Type
== "SHA256")
95 SHA256Summation SHA256
;
97 fileHash
= (std::string
)SHA256
.Result();
99 else if (Type
== "SHA512")
101 SHA512Summation SHA512
;
103 fileHash
= (std::string
)SHA512
.Result();
110 const char** HashString::SupportedHashes()
112 return _SupportedHashes
;
115 bool HashString::empty() const
117 return (Type
.empty() || Hash
.empty());
120 std::string
HashString::toStr() const
122 return Type
+ std::string(":") + Hash
;
125 // Hashes::AddFD - Add the contents of the FD /*{{{*/
126 // ---------------------------------------------------------------------
128 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, bool const addMD5
,
129 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
131 unsigned char Buf
[64*64];
133 int ToEOF
= (Size
== 0);
134 while (Size
!= 0 || ToEOF
)
136 unsigned long long n
= sizeof(Buf
);
137 if (!ToEOF
) n
= std::min(Size
, n
);
138 Res
= read(Fd
,Buf
,n
);
139 if (Res
< 0 || (!ToEOF
&& Res
!= (ssize_t
) n
)) // error, or short read
141 if (ToEOF
&& Res
== 0) // EOF
148 if (addSHA256
== true)
150 if (addSHA512
== true)
155 bool Hashes::AddFD(FileFd
&Fd
,unsigned long long Size
, bool const addMD5
,
156 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
158 unsigned char Buf
[64*64];
159 bool const ToEOF
= (Size
== 0);
160 while (Size
!= 0 || ToEOF
)
162 unsigned long long n
= sizeof(Buf
);
163 if (!ToEOF
) n
= std::min(Size
, n
);
164 unsigned long long a
= 0;
165 if (Fd
.Read(Buf
, n
, &a
) == false) // error
169 if (a
!= n
) // short read
172 else if (a
== 0) // EOF
179 if (addSHA256
== true)
181 if (addSHA512
== true)