]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
Merge branch 'bugfix/apt-search-case' into debian/sid
[apt.git] / apt-pkg / contrib / hashes.cc
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/md5.h>
20 #include <apt-pkg/sha1.h>
21 #include <apt-pkg/sha2.h>
22
23 #include <stddef.h>
24 #include <algorithm>
25 #include <unistd.h>
26 #include <string>
27 #include <iostream>
28 /*}}}*/
29
30 const char* HashString::_SupportedHashes[] =
31 {
32 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
33 };
34
35 HashString::HashString()
36 {
37 }
38
39 HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
40 {
41 }
42
43 HashString::HashString(std::string StringedHash) /*{{{*/
44 {
45 // legacy: md5sum without "MD5Sum:" prefix
46 if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32)
47 {
48 Type = "MD5Sum";
49 Hash = StringedHash;
50 return;
51 }
52 std::string::size_type pos = StringedHash.find(":");
53 Type = StringedHash.substr(0,pos);
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 }
59 /*}}}*/
60 bool HashString::VerifyFile(std::string filename) const /*{{{*/
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 /*{{{*/
81 {
82 std::string fileHash;
83
84 FileFd Fd(filename, FileFd::ReadOnly);
85 if(Type == "MD5Sum")
86 {
87 MD5Summation MD5;
88 MD5.AddFD(Fd);
89 fileHash = (std::string)MD5.Result();
90 }
91 else if (Type == "SHA1")
92 {
93 SHA1Summation SHA1;
94 SHA1.AddFD(Fd);
95 fileHash = (std::string)SHA1.Result();
96 }
97 else if (Type == "SHA256")
98 {
99 SHA256Summation SHA256;
100 SHA256.AddFD(Fd);
101 fileHash = (std::string)SHA256.Result();
102 }
103 else if (Type == "SHA512")
104 {
105 SHA512Summation SHA512;
106 SHA512.AddFD(Fd);
107 fileHash = (std::string)SHA512.Result();
108 }
109 Fd.Close();
110
111 return fileHash;
112 }
113 /*}}}*/
114 const char** HashString::SupportedHashes()
115 {
116 return _SupportedHashes;
117 }
118
119 APT_PURE bool HashString::empty() const
120 {
121 return (Type.empty() || Hash.empty());
122 }
123
124 std::string HashString::toStr() const
125 {
126 return Type + std::string(":") + Hash;
127 }
128
129 // Hashes::AddFD - Add the contents of the FD /*{{{*/
130 // ---------------------------------------------------------------------
131 /* */
132 bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
133 bool const addSHA1, bool const addSHA256, bool const addSHA512)
134 {
135 unsigned char Buf[64*64];
136 bool const ToEOF = (Size == 0);
137 while (Size != 0 || ToEOF)
138 {
139 unsigned long long n = sizeof(Buf);
140 if (!ToEOF) n = std::min(Size, n);
141 ssize_t const Res = read(Fd,Buf,n);
142 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
143 return false;
144 if (ToEOF && Res == 0) // EOF
145 break;
146 Size -= Res;
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);
155 }
156 return true;
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;
188 }
189 /*}}}*/