]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/contrib/hashes.cc
add check when sources.list changed
[apt.git] / apt-pkg / contrib / hashes.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
4/* ######################################################################
5
6 Hashes - Simple wrapper around the hash functions
7
8 This is just used to make building the methods simpler, this is the
9 only interface required..
10
11 ##################################################################### */
12 /*}}}*/
13// Include Files /*{{{*/
14#include <config.h>
15
16#include <apt-pkg/hashes.h>
17#include <apt-pkg/fileutl.h>
18#include <apt-pkg/configuration.h>
19#include <apt-pkg/macros.h>
20
21#include <unistd.h>
22#include <string>
23#include <iostream>
24 /*}}}*/
25
26const char* HashString::_SupportedHashes[] =
27{
28 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
29};
30
31HashString::HashString()
32{
33}
34
35HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
36{
37}
38
39HashString::HashString(std::string StringedHash) /*{{{*/
40{
41 // legacy: md5sum without "MD5Sum:" prefix
42 if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32)
43 {
44 Type = "MD5Sum";
45 Hash = StringedHash;
46 return;
47 }
48 std::string::size_type pos = StringedHash.find(":");
49 Type = StringedHash.substr(0,pos);
50 Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
51
52 if(_config->FindB("Debug::Hashes",false) == true)
53 std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
54}
55 /*}}}*/
56bool HashString::VerifyFile(std::string filename) const /*{{{*/
57{
58 std::string fileHash = GetHashForFile(filename);
59
60 if(_config->FindB("Debug::Hashes",false) == true)
61 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
62
63 return (fileHash == Hash);
64}
65 /*}}}*/
66bool HashString::FromFile(std::string filename) /*{{{*/
67{
68 // pick the strongest hash
69 if (Type == "")
70 Type = _SupportedHashes[0];
71
72 Hash = GetHashForFile(filename);
73 return true;
74}
75 /*}}}*/
76std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
77{
78 std::string fileHash;
79
80 FileFd Fd(filename, FileFd::ReadOnly);
81 if(Type == "MD5Sum")
82 {
83 MD5Summation MD5;
84 MD5.AddFD(Fd);
85 fileHash = (std::string)MD5.Result();
86 }
87 else if (Type == "SHA1")
88 {
89 SHA1Summation SHA1;
90 SHA1.AddFD(Fd);
91 fileHash = (std::string)SHA1.Result();
92 }
93 else if (Type == "SHA256")
94 {
95 SHA256Summation SHA256;
96 SHA256.AddFD(Fd);
97 fileHash = (std::string)SHA256.Result();
98 }
99 else if (Type == "SHA512")
100 {
101 SHA512Summation SHA512;
102 SHA512.AddFD(Fd);
103 fileHash = (std::string)SHA512.Result();
104 }
105 Fd.Close();
106
107 return fileHash;
108}
109 /*}}}*/
110const char** HashString::SupportedHashes()
111{
112 return _SupportedHashes;
113}
114
115bool HashString::empty() const
116{
117 return (Type.empty() || Hash.empty());
118}
119
120std::string HashString::toStr() const
121{
122 return Type + std::string(":") + Hash;
123}
124
125// Hashes::AddFD - Add the contents of the FD /*{{{*/
126// ---------------------------------------------------------------------
127/* */
128bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
129 bool const addSHA1, bool const addSHA256, bool const addSHA512)
130{
131 unsigned char Buf[64*64];
132 ssize_t Res = 0;
133 int ToEOF = (Size == 0);
134 while (Size != 0 || ToEOF)
135 {
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
140 return false;
141 if (ToEOF && Res == 0) // EOF
142 break;
143 Size -= Res;
144 if (addMD5 == true)
145 MD5.Add(Buf,Res);
146 if (addSHA1 == true)
147 SHA1.Add(Buf,Res);
148 if (addSHA256 == true)
149 SHA256.Add(Buf,Res);
150 if (addSHA512 == true)
151 SHA512.Add(Buf,Res);
152 }
153 return true;
154}
155bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5,
156 bool const addSHA1, bool const addSHA256, bool const addSHA512)
157{
158 unsigned char Buf[64*64];
159 bool const ToEOF = (Size == 0);
160 while (Size != 0 || ToEOF)
161 {
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
166 return false;
167 if (ToEOF == false)
168 {
169 if (a != n) // short read
170 return false;
171 }
172 else if (a == 0) // EOF
173 break;
174 Size -= a;
175 if (addMD5 == true)
176 MD5.Add(Buf, a);
177 if (addSHA1 == true)
178 SHA1.Add(Buf, a);
179 if (addSHA256 == true)
180 SHA256.Add(Buf, a);
181 if (addSHA512 == true)
182 SHA512.Add(Buf, a);
183 }
184 return true;
185}
186 /*}}}*/