summaryrefslogtreecommitdiff
path: root/src/messageDigest.cc
blob: ce860f893ce1a0fb357c5dc6a71a239ba33d4615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "messageDigest.h"
#include "util.h"
#include "array_fun.h"

namespace aria2 {

const std::string MessageDigestContext::SHA1("sha-1");

const std::string MessageDigestContext::SHA256("sha-256");

const std::string MessageDigestContext::MD5("md-5");

namespace {
struct DigestAlgoEntry {
  MessageDigestContext::DigestAlgo algo;
  int strength;
  DigestAlgoEntry(const MessageDigestContext::DigestAlgo& algo, int strength):
    algo(algo), strength(strength) {}
};
}

typedef std::map<std::string, DigestAlgoEntry>
DigestAlgoMap;

static const DigestAlgoMap& getDigestAlgos()
{
  enum AlgoStrength {
    STRENGTH_MD5 = 0,
    STRENGTH_SHA_1 = 1,
    STRENGTH_SHA_256 = 2
  };
  static const DigestAlgoMap::value_type digests[] = {
#ifdef HAVE_LIBSSL
    DigestAlgoMap::value_type("md5", DigestAlgoEntry(EVP_md5(), STRENGTH_MD5)),
    DigestAlgoMap::value_type
    ("sha-1", DigestAlgoEntry(EVP_sha1(), STRENGTH_SHA_1)),
    DigestAlgoMap::value_type
    ("sha1", DigestAlgoEntry(EVP_sha1(), STRENGTH_SHA_1)),
# ifdef HAVE_EVP_SHA256
    DigestAlgoMap::value_type
    ("sha-256", DigestAlgoEntry(EVP_sha256(), STRENGTH_SHA_256)),
    DigestAlgoMap::value_type
    ("sha256", DigestAlgoEntry(EVP_sha256(), STRENGTH_SHA_256)),
# endif // HAVE_EVP_SHA256
#elif HAVE_LIBGCRYPT
    DigestAlgoMap::value_type
    ("md5", DigestAlgoEntry(GCRY_MD_MD5, STRENGTH_MD5)),
    DigestAlgoMap::value_type
    ("sha-1", DigestAlgoEntry(GCRY_MD_SHA1, STRENGTH_SHA_1)),
    DigestAlgoMap::value_type
    ("sha1", DigestAlgoEntry(GCRY_MD_SHA1, STRENGTH_SHA_1)),
    DigestAlgoMap::value_type
    ("sha-256", DigestAlgoEntry(GCRY_MD_SHA256, STRENGTH_SHA_256)),
    DigestAlgoMap::value_type
    ("sha256", DigestAlgoEntry(GCRY_MD_SHA256, STRENGTH_SHA_256)),
#endif // HAVE_LIBGCRYPT
  };
  static const DigestAlgoMap algomap(vbegin(digests), vend(digests));
  return algomap;
}

std::string MessageDigestContext::digestFinal()
{
  size_t length = digestLength(algo);
  unsigned char* rawMD = new unsigned char[length];
  digestFinal(rawMD);
  std::string rawMDString(&rawMD[0], &rawMD[length]);
  delete [] rawMD;
  return rawMDString;
}

bool MessageDigestContext::supports(const std::string& algostring)
{
  const DigestAlgoMap& allAlgos = getDigestAlgos();
  DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  if(itr == allAlgos.end()) {
    return false;
  } else {
    return true;
  }
}

MessageDigestContext::DigestAlgo
MessageDigestContext::getDigestAlgo(const std::string& algostring)
{
  const DigestAlgoMap& allAlgos = getDigestAlgos();
  DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  if(itr == allAlgos.end()) {
    throw DL_ABORT_EX
      (StringFormat("Digest algorithm %s is not supported.",
                    algostring.c_str()).str());
  }
  return (*itr).second.algo;
}

std::string MessageDigestContext::getSupportedAlgoString()
{
  const DigestAlgoMap& allAlgos = getDigestAlgos();
  std::string algos;
  for(DigestAlgoMap::const_iterator itr = allAlgos.begin(),
        eoi = allAlgos.end(); itr != eoi; ++itr) {
    algos += (*itr).first;
    algos += ", ";
  }
  return util::trim(algos, ", ");
}

bool MessageDigestContext::isStronger
(const std::string& lhs, const std::string& rhs)
{
  const DigestAlgoMap& allAlgos = getDigestAlgos();
  DigestAlgoMap::const_iterator lhsitr = allAlgos.find(lhs);
  DigestAlgoMap::const_iterator rhsitr = allAlgos.find(rhs);
  if(lhsitr == allAlgos.end() || rhsitr == allAlgos.end()) {
    return false;
  }
  return (*lhsitr).second.strength > (*rhsitr).second.strength;
}

} // namespace aria2