]>
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 /*{{{*/
14 #include <apt-pkg/hashes.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/configuration.h>
24 const char* HashString::_SupportedHashes
[] =
26 "SHA256", "SHA1", "MD5Sum", NULL
29 HashString::HashString()
33 HashString::HashString(string Type
, string Hash
) : Type(Type
), Hash(Hash
)
37 HashString::HashString(string StringedHash
) /*{{{*/
39 // legacy: md5sum without "MD5Sum:" prefix
40 if (StringedHash
.find(":") == string::npos
&& StringedHash
.size() == 32)
46 string::size_type pos
= StringedHash
.find(":");
47 Type
= StringedHash
.substr(0,pos
);
48 Hash
= StringedHash
.substr(pos
+1, StringedHash
.size() - pos
);
50 if(_config
->FindB("Debug::Hashes",false) == true)
51 std::clog
<< "HashString(string): " << Type
<< " : " << Hash
<< std::endl
;
54 bool HashString::VerifyFile(string filename
) const /*{{{*/
59 SHA256Summation SHA256
;
62 FileFd
Fd(filename
, FileFd::ReadOnly
);
65 MD5
.AddFD(Fd
.Fd(), Fd
.Size());
66 fileHash
= (string
)MD5
.Result();
68 else if (Type
== "SHA1")
70 SHA1
.AddFD(Fd
.Fd(), Fd
.Size());
71 fileHash
= (string
)SHA1
.Result();
73 else if (Type
== "SHA256")
75 SHA256
.AddFD(Fd
.Fd(), Fd
.Size());
76 fileHash
= (string
)SHA256
.Result();
80 if(_config
->FindB("Debug::Hashes",false) == true)
81 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
83 return (fileHash
== Hash
);
86 const char** HashString::SupportedHashes()
88 return _SupportedHashes
;
91 bool HashString::empty() const
93 return (Type
.empty() || Hash
.empty());
96 string
HashString::toStr() const
98 return Type
+string(":")+Hash
;
101 // Hashes::AddFD - Add the contents of the FD /*{{{*/
102 // ---------------------------------------------------------------------
104 bool Hashes::AddFD(int Fd
,unsigned long Size
)
106 unsigned char Buf
[64*64];
108 int ToEOF
= (Size
== 0);
109 while (Size
!= 0 || ToEOF
)
111 unsigned n
= sizeof(Buf
);
112 if (!ToEOF
) n
= min(Size
,(unsigned long)n
);
113 Res
= read(Fd
,Buf
,n
);
114 if (Res
< 0 || (!ToEOF
&& (unsigned) Res
!= n
)) // error, or short read
116 if (ToEOF
&& Res
== 0) // EOF