]>
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(string Type
, string Hash
) : Type(Type
), Hash(Hash
)
39 HashString::HashString(string StringedHash
) /*{{{*/
41 // legacy: md5sum without "MD5Sum:" prefix
42 if (StringedHash
.find(":") == string::npos
&& StringedHash
.size() == 32)
48 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(string filename
) const /*{{{*/
60 FileFd
Fd(filename
, FileFd::ReadOnly
);
64 MD5
.AddFD(Fd
.Fd(), Fd
.Size());
65 fileHash
= (string
)MD5
.Result();
67 else if (Type
== "SHA1")
70 SHA1
.AddFD(Fd
.Fd(), Fd
.Size());
71 fileHash
= (string
)SHA1
.Result();
73 else if (Type
== "SHA256")
75 SHA256Summation SHA256
;
76 SHA256
.AddFD(Fd
.Fd(), Fd
.Size());
77 fileHash
= (string
)SHA256
.Result();
79 else if (Type
== "SHA512")
81 SHA512Summation SHA512
;
82 SHA512
.AddFD(Fd
.Fd(), Fd
.Size());
83 fileHash
= (string
)SHA512
.Result();
87 if(_config
->FindB("Debug::Hashes",false) == true)
88 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
90 return (fileHash
== Hash
);
93 const char** HashString::SupportedHashes()
95 return _SupportedHashes
;
98 bool HashString::empty() const
100 return (Type
.empty() || Hash
.empty());
103 string
HashString::toStr() const
105 return Type
+string(":")+Hash
;
108 // Hashes::AddFD - Add the contents of the FD /*{{{*/
109 // ---------------------------------------------------------------------
111 bool Hashes::AddFD(int const Fd
,unsigned long long Size
, bool const addMD5
,
112 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
114 unsigned char Buf
[64*64];
116 int ToEOF
= (Size
== 0);
117 while (Size
!= 0 || ToEOF
)
119 unsigned long long n
= sizeof(Buf
);
120 if (!ToEOF
) n
= min(Size
, n
);
121 Res
= read(Fd
,Buf
,n
);
122 if (Res
< 0 || (!ToEOF
&& Res
!= (ssize_t
) n
)) // error, or short read
124 if (ToEOF
&& Res
== 0) // EOF
131 if (addSHA256
== true)
133 if (addSHA512
== true)