]>
git.saurik.com Git - apt-legacy.git/blob - apt-pkg/contrib/hashes.cc
fcc2f887cb704b825f18ced40959f952e6e4e084
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
;
55 bool HashString::VerifyFile(string filename
) const
60 SHA256Summation SHA256
;
63 FileFd
Fd(filename
, FileFd::ReadOnly
);
66 MD5
.AddFD(Fd
.Fd(), Fd
.Size());
67 fileHash
= (string
)MD5
.Result();
69 else if (Type
== "SHA1")
71 SHA1
.AddFD(Fd
.Fd(), Fd
.Size());
72 fileHash
= (string
)SHA1
.Result();
74 else if (Type
== "SHA256")
76 SHA256
.AddFD(Fd
.Fd(), Fd
.Size());
77 fileHash
= (string
)SHA256
.Result();
81 if(_config
->FindB("Debug::Hashes",false) == true)
82 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
84 return (fileHash
== Hash
);
87 const char** HashString::SupportedHashes()
89 return _SupportedHashes
;
92 bool HashString::empty() const
94 return (Type
.empty() || Hash
.empty());
98 string
HashString::toStr() const
100 return Type
+string(":")+Hash
;
104 // Hashes::AddFD - Add the contents of the FD /*{{{*/
105 // ---------------------------------------------------------------------
107 bool Hashes::AddFD(int Fd
,unsigned long Size
)
109 unsigned char Buf
[64*64];
113 Res
= read(Fd
,Buf
,min(Size
,(unsigned long)sizeof(Buf
)));
114 if (Res
< 0 || (unsigned)Res
!= min(Size
,(unsigned long)sizeof(Buf
)))