]>
Commit | Line | Data |
---|---|---|
63b1700f AL |
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 /*{{{*/ | |
ea542140 DK |
14 | #include <config.h> |
15 | ||
63b1700f | 16 | #include <apt-pkg/hashes.h> |
495e5cb2 MV |
17 | #include <apt-pkg/fileutl.h> |
18 | #include <apt-pkg/configuration.h> | |
453b82a3 DK |
19 | #include <apt-pkg/md5.h> |
20 | #include <apt-pkg/sha1.h> | |
21 | #include <apt-pkg/sha2.h> | |
aea7f4c8 | 22 | |
453b82a3 DK |
23 | #include <stddef.h> |
24 | #include <algorithm> | |
ea542140 | 25 | #include <unistd.h> |
495e5cb2 MV |
26 | #include <string> |
27 | #include <iostream> | |
63b1700f AL |
28 | /*}}}*/ |
29 | ||
495e5cb2 MV |
30 | const char* HashString::_SupportedHashes[] = |
31 | { | |
d9b9e9e2 | 32 | "SHA512", "SHA256", "SHA1", "MD5Sum", NULL |
495e5cb2 MV |
33 | }; |
34 | ||
35 | HashString::HashString() | |
36 | { | |
37 | } | |
38 | ||
8f3ba4e8 | 39 | HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash) |
495e5cb2 MV |
40 | { |
41 | } | |
42 | ||
8f3ba4e8 | 43 | HashString::HashString(std::string StringedHash) /*{{{*/ |
495e5cb2 MV |
44 | { |
45 | // legacy: md5sum without "MD5Sum:" prefix | |
8f3ba4e8 | 46 | if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32) |
495e5cb2 MV |
47 | { |
48 | Type = "MD5Sum"; | |
49 | Hash = StringedHash; | |
50 | return; | |
51 | } | |
8f3ba4e8 | 52 | std::string::size_type pos = StringedHash.find(":"); |
8a8feb29 | 53 | Type = StringedHash.substr(0,pos); |
495e5cb2 MV |
54 | Hash = StringedHash.substr(pos+1, StringedHash.size() - pos); |
55 | ||
56 | if(_config->FindB("Debug::Hashes",false) == true) | |
57 | std::clog << "HashString(string): " << Type << " : " << Hash << std::endl; | |
58 | } | |
92fcbfc1 | 59 | /*}}}*/ |
8f3ba4e8 | 60 | bool HashString::VerifyFile(std::string filename) const /*{{{*/ |
e6645b9f MV |
61 | { |
62 | std::string fileHash = GetHashForFile(filename); | |
63 | ||
64 | if(_config->FindB("Debug::Hashes",false) == true) | |
65 | std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl; | |
66 | ||
67 | return (fileHash == Hash); | |
68 | } | |
69 | /*}}}*/ | |
70 | bool HashString::FromFile(std::string filename) /*{{{*/ | |
71 | { | |
72 | // pick the strongest hash | |
73 | if (Type == "") | |
74 | Type = _SupportedHashes[0]; | |
75 | ||
76 | Hash = GetHashForFile(filename); | |
77 | return true; | |
78 | } | |
79 | /*}}}*/ | |
80 | std::string HashString::GetHashForFile(std::string filename) const /*{{{*/ | |
495e5cb2 | 81 | { |
8f3ba4e8 | 82 | std::string fileHash; |
495e5cb2 MV |
83 | |
84 | FileFd Fd(filename, FileFd::ReadOnly); | |
2dcf7b8f | 85 | if(Type == "MD5Sum") |
495e5cb2 | 86 | { |
2dcf7b8f | 87 | MD5Summation MD5; |
109eb151 | 88 | MD5.AddFD(Fd); |
8f3ba4e8 | 89 | fileHash = (std::string)MD5.Result(); |
2dcf7b8f | 90 | } |
495e5cb2 MV |
91 | else if (Type == "SHA1") |
92 | { | |
2dcf7b8f | 93 | SHA1Summation SHA1; |
109eb151 | 94 | SHA1.AddFD(Fd); |
8f3ba4e8 | 95 | fileHash = (std::string)SHA1.Result(); |
2dcf7b8f DK |
96 | } |
97 | else if (Type == "SHA256") | |
495e5cb2 | 98 | { |
2dcf7b8f | 99 | SHA256Summation SHA256; |
109eb151 | 100 | SHA256.AddFD(Fd); |
8f3ba4e8 | 101 | fileHash = (std::string)SHA256.Result(); |
495e5cb2 | 102 | } |
2dcf7b8f | 103 | else if (Type == "SHA512") |
d9b9e9e2 | 104 | { |
2dcf7b8f | 105 | SHA512Summation SHA512; |
109eb151 | 106 | SHA512.AddFD(Fd); |
8f3ba4e8 | 107 | fileHash = (std::string)SHA512.Result(); |
d9b9e9e2 | 108 | } |
495e5cb2 MV |
109 | Fd.Close(); |
110 | ||
e6645b9f | 111 | return fileHash; |
495e5cb2 | 112 | } |
92fcbfc1 | 113 | /*}}}*/ |
495e5cb2 MV |
114 | const char** HashString::SupportedHashes() |
115 | { | |
116 | return _SupportedHashes; | |
117 | } | |
118 | ||
119 | bool HashString::empty() const | |
120 | { | |
121 | return (Type.empty() || Hash.empty()); | |
122 | } | |
123 | ||
8f3ba4e8 | 124 | std::string HashString::toStr() const |
495e5cb2 | 125 | { |
8f3ba4e8 | 126 | return Type + std::string(":") + Hash; |
495e5cb2 MV |
127 | } |
128 | ||
63b1700f AL |
129 | // Hashes::AddFD - Add the contents of the FD /*{{{*/ |
130 | // --------------------------------------------------------------------- | |
131 | /* */ | |
650faab0 | 132 | bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5, |
1dab797c | 133 | bool const addSHA1, bool const addSHA256, bool const addSHA512) |
63b1700f AL |
134 | { |
135 | unsigned char Buf[64*64]; | |
9ce3cfc9 | 136 | bool const ToEOF = (Size == 0); |
04f4e1a3 | 137 | while (Size != 0 || ToEOF) |
63b1700f | 138 | { |
650faab0 | 139 | unsigned long long n = sizeof(Buf); |
8f3ba4e8 | 140 | if (!ToEOF) n = std::min(Size, n); |
9ce3cfc9 | 141 | ssize_t const Res = read(Fd,Buf,n); |
650faab0 | 142 | if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read |
1dab797c | 143 | return false; |
04f4e1a3 | 144 | if (ToEOF && Res == 0) // EOF |
1dab797c | 145 | break; |
63b1700f | 146 | Size -= Res; |
1dab797c DK |
147 | if (addMD5 == true) |
148 | MD5.Add(Buf,Res); | |
149 | if (addSHA1 == true) | |
150 | SHA1.Add(Buf,Res); | |
151 | if (addSHA256 == true) | |
152 | SHA256.Add(Buf,Res); | |
153 | if (addSHA512 == true) | |
154 | SHA512.Add(Buf,Res); | |
63b1700f AL |
155 | } |
156 | return true; | |
109eb151 DK |
157 | } |
158 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5, | |
159 | bool const addSHA1, bool const addSHA256, bool const addSHA512) | |
160 | { | |
161 | unsigned char Buf[64*64]; | |
162 | bool const ToEOF = (Size == 0); | |
163 | while (Size != 0 || ToEOF) | |
164 | { | |
165 | unsigned long long n = sizeof(Buf); | |
166 | if (!ToEOF) n = std::min(Size, n); | |
167 | unsigned long long a = 0; | |
168 | if (Fd.Read(Buf, n, &a) == false) // error | |
169 | return false; | |
170 | if (ToEOF == false) | |
171 | { | |
172 | if (a != n) // short read | |
173 | return false; | |
174 | } | |
175 | else if (a == 0) // EOF | |
176 | break; | |
177 | Size -= a; | |
178 | if (addMD5 == true) | |
179 | MD5.Add(Buf, a); | |
180 | if (addSHA1 == true) | |
181 | SHA1.Add(Buf, a); | |
182 | if (addSHA256 == true) | |
183 | SHA256.Add(Buf, a); | |
184 | if (addSHA512 == true) | |
185 | SHA512.Add(Buf, a); | |
186 | } | |
187 | return true; | |
63b1700f AL |
188 | } |
189 | /*}}}*/ |