]>
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 /*{{{*/ | |
63b1700f | 14 | #include <apt-pkg/hashes.h> |
495e5cb2 MV |
15 | #include <apt-pkg/fileutl.h> |
16 | #include <apt-pkg/configuration.h> | |
63b1700f AL |
17 | |
18 | #include <unistd.h> | |
19 | #include <system.h> | |
495e5cb2 MV |
20 | #include <string> |
21 | #include <iostream> | |
63b1700f AL |
22 | /*}}}*/ |
23 | ||
495e5cb2 MV |
24 | const char* HashString::_SupportedHashes[] = |
25 | { | |
26 | "SHA256", "SHA1", "MD5Sum", NULL | |
27 | }; | |
28 | ||
29 | HashString::HashString() | |
30 | { | |
31 | } | |
32 | ||
33 | HashString::HashString(string Type, string Hash) : Type(Type), Hash(Hash) | |
34 | { | |
35 | } | |
36 | ||
92fcbfc1 | 37 | HashString::HashString(string StringedHash) /*{{{*/ |
495e5cb2 MV |
38 | { |
39 | // legacy: md5sum without "MD5Sum:" prefix | |
40 | if (StringedHash.find(":") == string::npos && StringedHash.size() == 32) | |
41 | { | |
42 | Type = "MD5Sum"; | |
43 | Hash = StringedHash; | |
44 | return; | |
45 | } | |
46 | string::size_type pos = StringedHash.find(":"); | |
8a8feb29 | 47 | Type = StringedHash.substr(0,pos); |
495e5cb2 MV |
48 | Hash = StringedHash.substr(pos+1, StringedHash.size() - pos); |
49 | ||
50 | if(_config->FindB("Debug::Hashes",false) == true) | |
51 | std::clog << "HashString(string): " << Type << " : " << Hash << std::endl; | |
52 | } | |
92fcbfc1 DK |
53 | /*}}}*/ |
54 | bool HashString::VerifyFile(string filename) const /*{{{*/ | |
495e5cb2 MV |
55 | { |
56 | FileFd fd; | |
57 | MD5Summation MD5; | |
58 | SHA1Summation SHA1; | |
59 | SHA256Summation SHA256; | |
60 | string fileHash; | |
61 | ||
62 | FileFd Fd(filename, FileFd::ReadOnly); | |
63 | if(Type == "MD5Sum") | |
64 | { | |
65 | MD5.AddFD(Fd.Fd(), Fd.Size()); | |
66 | fileHash = (string)MD5.Result(); | |
67 | } | |
68 | else if (Type == "SHA1") | |
69 | { | |
70 | SHA1.AddFD(Fd.Fd(), Fd.Size()); | |
71 | fileHash = (string)SHA1.Result(); | |
72 | } | |
73 | else if (Type == "SHA256") | |
74 | { | |
75 | SHA256.AddFD(Fd.Fd(), Fd.Size()); | |
76 | fileHash = (string)SHA256.Result(); | |
77 | } | |
78 | Fd.Close(); | |
79 | ||
80 | if(_config->FindB("Debug::Hashes",false) == true) | |
81 | std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl; | |
82 | ||
83 | return (fileHash == Hash); | |
84 | } | |
92fcbfc1 | 85 | /*}}}*/ |
495e5cb2 MV |
86 | const char** HashString::SupportedHashes() |
87 | { | |
88 | return _SupportedHashes; | |
89 | } | |
90 | ||
91 | bool HashString::empty() const | |
92 | { | |
93 | return (Type.empty() || Hash.empty()); | |
94 | } | |
95 | ||
495e5cb2 MV |
96 | string HashString::toStr() const |
97 | { | |
98 | return Type+string(":")+Hash; | |
99 | } | |
100 | ||
63b1700f AL |
101 | // Hashes::AddFD - Add the contents of the FD /*{{{*/ |
102 | // --------------------------------------------------------------------- | |
103 | /* */ | |
104 | bool Hashes::AddFD(int Fd,unsigned long Size) | |
105 | { | |
106 | unsigned char Buf[64*64]; | |
107 | int Res = 0; | |
04f4e1a3 JAK |
108 | int ToEOF = (Size == 0); |
109 | while (Size != 0 || ToEOF) | |
63b1700f | 110 | { |
04f4e1a3 JAK |
111 | unsigned n = sizeof(Buf); |
112 | if (!ToEOF) n = min(Size,(unsigned long)n); | |
113 | Res = read(Fd,Buf,n); | |
114 | if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read | |
115 | return false; | |
116 | if (ToEOF && Res == 0) // EOF | |
117 | break; | |
63b1700f AL |
118 | Size -= Res; |
119 | MD5.Add(Buf,Res); | |
120 | SHA1.Add(Buf,Res); | |
cde41ae8 | 121 | SHA256.Add(Buf,Res); |
63b1700f AL |
122 | } |
123 | return true; | |
124 | } | |
125 | /*}}}*/ | |
126 |