]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.cc
apt-pkg/contrib/md5.cc: fix md5sum by using the right type (unsinged char*) and avoid...
[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 FileFd fd;
57 MD5Summation MD5;
58 SHA1Summation SHA1;
59 SHA256Summation SHA256;
60 SHA256Summation SHA512;
61 string fileHash;
62
63 FileFd Fd(filename, FileFd::ReadOnly);
64 if(Type == "MD5Sum")
65 {
66 MD5.AddFD(Fd.Fd(), Fd.Size());
67 fileHash = (string)MD5.Result();
68 }
69 else if (Type == "SHA1")
70 {
71 SHA1.AddFD(Fd.Fd(), Fd.Size());
72 fileHash = (string)SHA1.Result();
73 }
74 else if (Type == "SHA256")
75 {
76 SHA256.AddFD(Fd.Fd(), Fd.Size());
77 fileHash = (string)SHA256.Result();
78 }
79 else if (Type == "SHA512")
80 {
81 SHA512.AddFD(Fd.Fd(), Fd.Size());
82 fileHash = (string)SHA512.Result();
83 }
84 Fd.Close();
85
86 if(_config->FindB("Debug::Hashes",false) == true)
87 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
88
89 return (fileHash == Hash);
90 }
91 /*}}}*/
92 const char** HashString::SupportedHashes()
93 {
94 return _SupportedHashes;
95 }
96
97 bool HashString::empty() const
98 {
99 return (Type.empty() || Hash.empty());
100 }
101
102 string HashString::toStr() const
103 {
104 return Type+string(":")+Hash;
105 }
106
107 // Hashes::AddFD - Add the contents of the FD /*{{{*/
108 // ---------------------------------------------------------------------
109 /* */
110 bool Hashes::AddFD(int Fd,unsigned long Size)
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 MD5.Add(Buf,Res);
126 SHA1.Add(Buf,Res);
127 SHA256.Add(Buf,Res);
128 SHA512.Add(Buf,Res);
129 }
130 return true;
131 }
132 /*}}}*/
133