IBR-DTN  1.0.0
KeyExchangeProtocol.cpp
Go to the documentation of this file.
1 /*
2  * KeyExchangeProtocol.cpp
3  *
4  * Copyright (C) 2014 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Thomas Schrader <schrader.thomas@gmail.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
24 
25 #include <openssl/sha.h>
26 #include <iomanip>
27 
28 namespace dtn
29 {
30  namespace security
31  {
33  {
34  }
35 
37  : manager(m), _protocol_id(protocol_id)
38  {
39  }
40 
42  {
43  }
44 
45  void KeyExchangeProtocol::add(std::map<int, KeyExchangeProtocol*> &list)
46  {
47  list[getProtocol()] = this;
48  }
49 
51  {
52  return _protocol_id;
53  }
54 
56  {
57  return new KeyExchangeSession(getProtocol(), peer, uniqueId, NULL);
58  }
59 
60  void KeyExchangeProtocol::sha256(std::ostream &stream, const std::string &data)
61  {
62  unsigned char hash[SHA256_DIGEST_LENGTH];
63  SHA256((const unsigned char*) data.c_str(), data.size(), hash);
64 
65  for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
66  {
67  stream << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
68  }
69  }
70 
71  std::string KeyExchangeProtocol::toHex(const std::string &data)
72  {
73  std::stringstream stream;
74 
75  for (string::size_type i = 0; i < data.size(); ++i)
76  {
77  unsigned char c = data[i];
78  stream << std::hex << std::setw(2) << std::setfill('0') << (int)c;
79  }
80 
81  return stream.str();
82  }
83  }
84 }
KeyExchangeProtocol(KeyExchangeManager &manager, int protocol_id)
static std::string toHex(const std::string &data)
void add(std::map< int, KeyExchangeProtocol * > &list)
static void sha256(std::ostream &stream, const std::string &data)
virtual KeyExchangeSession * createSession(const dtn::data::EID &peer, unsigned int uniqueId)