File: hmac.cc

package info (click to toggle)
weakforced 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,040 kB
  • sloc: cpp: 20,397; python: 2,002; sh: 700; makefile: 432
file content (87 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download
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
/*
 * This file is part of PowerDNS or weakforced.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * 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.
 */

#include <string>
#include "hmac.hh"
#include "wforce_exception.hh"

static const EVP_MD* getMDType(HashAlgo algo)
{
  switch(algo) {
  case HashAlgo::SHA224:
    return EVP_sha224();
    break;
  case HashAlgo::SHA256:
    return EVP_sha256();
    break;
  case HashAlgo::SHA384:
    return EVP_sha384();
    break;
  case HashAlgo::SHA512:
    return EVP_sha512();
    break;
  default:
    throw WforceException("Unknown hash algorithm requested from calculateHMAC()");
  }
}

// This function is borrowed and modified from pdns src
std::string calculateHMAC(const std::string& key, const std::string& text, HashAlgo algo)
{
  const EVP_MD* md_type;
  unsigned int outlen;
  unsigned char hash[EVP_MAX_MD_SIZE];

  md_type = getMDType(algo);
  
  unsigned char* out = HMAC(md_type,
			    reinterpret_cast<const unsigned char*>(key.c_str()), key.size(),
			    reinterpret_cast<const unsigned char*>(text.c_str()), text.size(),
			    hash, &outlen);
  if (out != NULL && outlen > 0) {
    return std::string((char*) hash, outlen);
  }

  return "";
}

std::string calculateHash(const std::string& text, HashAlgo algo)
{
  EVP_MD_CTX *mdctx;
  const EVP_MD *md_type;
  unsigned char hash[EVP_MAX_MD_SIZE];
  unsigned int out_len;

  md_type = getMDType(algo);

  mdctx = EVP_MD_CTX_create();
  EVP_DigestInit_ex(mdctx, md_type, NULL);
  EVP_DigestUpdate(mdctx, reinterpret_cast<const unsigned char*>(text.c_str()), text.size());
  int ret = EVP_DigestFinal_ex(mdctx, hash, &out_len);
  EVP_MD_CTX_destroy(mdctx);

  if ((ret == 1) && (out_len > 0)) {
    return std::string((char*) hash, out_len);
  }

  return "";
}