]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
4407574fa0efd99b801f22f6d9941ce4c0f77b5d
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>
17 #include <apt-pkg/macros.h>
24 const char* HashString::_SupportedHashes
[] =
26 "SHA512", "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 /*{{{*/
58 FileFd
Fd(filename
, FileFd::ReadOnly
);
62 MD5
.AddFD(Fd
.Fd(), Fd
.Size());
63 fileHash
= (string
)MD5
.Result();
65 else if (Type
== "SHA1")
68 SHA1
.AddFD(Fd
.Fd(), Fd
.Size());
69 fileHash
= (string
)SHA1
.Result();
71 else if (Type
== "SHA256")
73 SHA256Summation SHA256
;
74 SHA256
.AddFD(Fd
.Fd(), Fd
.Size());
75 fileHash
= (string
)SHA256
.Result();
77 else if (Type
== "SHA512")
79 SHA512Summation SHA512
;
80 SHA512
.AddFD(Fd
.Fd(), Fd
.Size());
81 fileHash
= (string
)SHA512
.Result();
85 if(_config
->FindB("Debug::Hashes",false) == true)
86 std::clog
<< "HashString::VerifyFile: got: " << fileHash
<< " expected: " << toStr() << std::endl
;
88 return (fileHash
== Hash
);
91 const char** HashString::SupportedHashes()
93 return _SupportedHashes
;
96 bool HashString::empty() const
98 return (Type
.empty() || Hash
.empty());
101 string
HashString::toStr() const
103 return Type
+string(":")+Hash
;
106 // Hashes::AddFD - Add the contents of the FD /*{{{*/
107 // ---------------------------------------------------------------------
109 bool Hashes::AddFD(int const Fd
,unsigned long Size
, bool const addMD5
,
110 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
112 unsigned char Buf
[64*64];
114 int ToEOF
= (Size
== 0);
115 while (Size
!= 0 || ToEOF
)
117 unsigned n
= sizeof(Buf
);
118 if (!ToEOF
) n
= min(Size
,(unsigned long)n
);
119 Res
= read(Fd
,Buf
,n
);
120 if (Res
< 0 || (!ToEOF
&& (unsigned) Res
!= n
)) // error, or short read
122 if (ToEOF
&& Res
== 0) // EOF
129 if (addSHA256
== true)
131 if (addSHA512
== true)