monotone

monotone Mtn Source Tree

Root/mac.hh

1#ifndef __MAC_HH__
2#define __MAC_HH__
3
4// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
5// all rights reserved.
6// licensed to the public under the terms of the GNU GPL (>= 2)
7// see the file COPYING for details
8
9// message authentication code (HMAC) calculation.
10
11#include <string>
12
13#include "app_state.hh"
14#include "constants.hh"
15#include "transforms.hh"
16
17#include "cryptopp/filters.h"
18#include "cryptopp/sha.h"
19#include "cryptopp/hex.h"
20#include "cryptopp/hmac.h"
21#include "cryptopp/osrng.h"
22
23void make_random_seed(app_state & app,
24 std::string & hexseed)
25{
26 using namespace CryptoPP;
27 bool request_blocking_rng = false;
28 if (!app.lua.hook_non_blocking_rng_ok())
29 {
30#ifndef BLOCKING_RNG_AVAILABLE
31 throw oops("no blocking RNG available and non-blocking RNG rejected");
32#else
33 request_blocking_rng = true;
34#endif
35 }
36
37 byte seed[constants::vchecklen];
38 AutoSeededRandomPool rng(request_blocking_rng);
39 rng.GenerateBlock(seed, constants::vchecklen);
40 std::string out(reinterpret_cast<char *>(seed), constants::vchecklen);
41 StringSource str(out, true,
42 new HexEncoder(new StringSink(hexseed)));
43 hexseed = lowercase(hexseed);
44}
45
46void calculate_mac(std::string const & mackey,
47 std::string const & data,
48 std::string & mac)
49{
50 using namespace CryptoPP;
51 char buf[HMAC<SHA>::DIGESTSIZE];
52 HMAC<SHA> mac_calc(reinterpret_cast<byte const *>(mackey.data()),
53 static_cast<unsigned int>(mackey.size()));
54 mac_calc.CalculateDigest(reinterpret_cast<byte *>(buf),
55 reinterpret_cast<byte const *>(data.data()),
56 static_cast<unsigned int>(data.size()));
57 std::string out(buf, HMAC<SHA>::DIGESTSIZE);
58 mac.clear();
59 StringSource str(out, true,
60 new HexEncoder(new StringSink(mac)));
61 mac = lowercase(mac);
62}
63
64#endif // __MAC_HH__

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status