hmac.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 by Julian Wiesener
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 /* HMAC algorithm as described by RFC 2104
30  * http://tools.ietf.org/html/rfc2104
31  */
32 
33 #ifndef CXXTOOLS_HMAC_H
34 #define CXXTOOLS_HMAC_H
35 
36 #include <sstream>
37 #include <algorithm>
38 
39 namespace cxxtools
40 {
41 
42 template <typename Algo>
43 void hmac_pad_key(Algo& key_hash, const std::string& key, std::string& o_key_pad, std::string& i_key_pad)
44 {
45  std::string _key(key);
46 
47  if(key.length() > Algo::blockSize)
48  _key = key_hash.getDigest();
49  _key.resize(Algo::blockSize);
50 
51  std::string::const_iterator key_it = _key.begin();
52  for(std::string::iterator pad_it = o_key_pad.begin();
53  pad_it != o_key_pad.end(); pad_it++)
54  *pad_it = *pad_it ^ *key_it++;
55 
56  key_it = _key.begin();
57  for(std::string::iterator pad_it = i_key_pad.begin();
58  pad_it != i_key_pad.end(); pad_it++)
59  *pad_it = *pad_it ^ *key_it++;
60 }
61 
62 template <typename Algo, typename data_type>
63 std::string hmac(const std::string& key, const data_type& msg)
64 {
65  Algo key_hash(key);
66  std::string o_key_pad(Algo::blockSize, 0x5c);
67  std::string i_key_pad(Algo::blockSize, 0x36);
68 
69  cxxtools::hmac_pad_key(key_hash, key, o_key_pad, i_key_pad);
70 
71  Algo inner_hash(i_key_pad + msg);
72  Algo outer_hash(o_key_pad + inner_hash.getDigest());
73  return outer_hash.getHexDigest();
74 }
75 
76 template <typename Algo, typename iterator_type>
77 std::string hmac(const std::string& key,
78  iterator_type from, iterator_type to)
79 {
80  Algo key_hash(key);
81  std::string o_key_pad(Algo::blockSize, 0x5c);
82  std::string i_key_pad(Algo::blockSize, 0x36);
83  std::stringstream is;
84 
85  cxxtools::hmac_pad_key(key_hash, key, o_key_pad, i_key_pad);
86 
87  std::copy(from, to, std::ostreambuf_iterator<char>(is));
88 
89  Algo inner_hash(i_key_pad + is.str());
90  Algo outer_hash(o_key_pad + inner_hash.getDigest());
91  return outer_hash.getHexDigest();
92 }
93 
94 }
95 
96 #endif // CXXTOOLS_HMAC_H