]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
merged from lp:~mvo/apt/mvo
[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 <apt-pkg/hashes.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/macros.h>
18
19 #include <unistd.h>
20 #include <string>
21 #include <iostream>
22 /*}}}*/
23
24 const char* HashString::_SupportedHashes[] =
25 {
26 "SHA512", "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
37 HashString::HashString(string StringedHash) /*{{{*/
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(":");
47 Type = StringedHash.substr(0,pos);
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 }
53 /*}}}*/
54 bool HashString::VerifyFile(string filename) const /*{{{*/
55 {
56 string fileHash;
57
58 FileFd Fd(filename, FileFd::ReadOnly);
59 if(Type == "MD5Sum")
60 {
61 MD5Summation MD5;
62 MD5.AddFD(Fd.Fd(), Fd.Size());
63 fileHash = (string)MD5.Result();
64 }
65 else if (Type == "SHA1")
66 {
67 SHA1Summation SHA1;
68 SHA1.AddFD(Fd.Fd(), Fd.Size());
69 fileHash = (string)SHA1.Result();
70 }
71 else if (Type == "SHA256")
72 {
73 SHA256Summation SHA256;
74 SHA256.AddFD(Fd.Fd(), Fd.Size());
75 fileHash = (string)SHA256.Result();
76 }
77 else if (Type == "SHA512")
78 {
79 SHA512Summation SHA512;
80 SHA512.AddFD(Fd.Fd(), Fd.Size());
81 fileHash = (string)SHA512.Result();
82 }
83 Fd.Close();
84
85 if(_config->FindB("Debug::Hashes",false) == true)
86 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
87
88 return (fileHash == Hash);
89 }
90 /*}}}*/
91 const char** HashString::SupportedHashes()
92 {
93 return _SupportedHashes;
94 }
95
96 bool HashString::empty() const
97 {
98 return (Type.empty() || Hash.empty());
99 }
100
101 string HashString::toStr() const
102 {
103 return Type+string(":")+Hash;
104 }
105
106 // Hashes::AddFD - Add the contents of the FD /*{{{*/
107 // ---------------------------------------------------------------------
108 /* */
109 bool Hashes::AddFD(int const Fd,unsigned long Size, bool const addMD5,
110 bool const addSHA1, bool const addSHA256, bool const addSHA512)
111 {
112 unsigned char Buf[64*64];
113 int Res = 0;
114 int ToEOF = (Size == 0);
115 while (Size != 0 || ToEOF)
116 {
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
121 return false;
122 if (ToEOF && Res == 0) // EOF
123 break;
124 Size -= Res;
125 if (addMD5 == true)
126 MD5.Add(Buf,Res);
127 if (addSHA1 == true)
128 SHA1.Add(Buf,Res);
129 if (addSHA256 == true)
130 SHA256.Add(Buf,Res);
131 if (addSHA512 == true)
132 SHA512.Add(Buf,Res);
133 }
134 return true;
135 }
136 /*}}}*/
137