Fabcoin Core  0.16.2
P2P Digital Currency
UDP.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #include "UDP.h"
23 using namespace std;
24 using namespace dev;
25 using namespace dev::p2p;
26 
27 const char* RLPXWarn::name() { return "!X!"; }
28 const char* RLPXNote::name() { return "-X-"; }
29 
31 {
32  assert(packetType());
33 
34  RLPStream rlpxstream;
35 // rlpxstream.appendRaw(toPublic(_k).asBytes()); // for mdc-based signature
36  rlpxstream.appendRaw(bytes(1, packetType())); // prefix by 1 byte for type
37  streamRLP(rlpxstream);
38  bytes rlpxBytes(rlpxstream.out());
39 
40  bytesConstRef rlpx(&rlpxBytes);
41  h256 sighash(dev::sha3(rlpx)); // H(type||data)
42  Signature sig = dev::sign(_k, sighash); // S(H(type||data))
43 
44  data.resize(h256::size + Signature::size + rlpx.size());
45  bytesRef rlpxHash(&data[0], h256::size);
47  bytesRef rlpxPayload(&data[h256::size + Signature::size], rlpx.size());
48 
49  sig.ref().copyTo(rlpxSig);
50  rlpx.copyTo(rlpxPayload);
51 
52  bytesConstRef signedRLPx(&data[h256::size], data.size() - h256::size);
53  dev::sha3(signedRLPx).ref().copyTo(rlpxHash);
54 
55  return sighash;
56 }
57 
58 Public RLPXDatagramFace::authenticate(bytesConstRef _sig, bytesConstRef _rlp)
59 {
60  Signature const& sig = *(Signature const*)_sig.data();
61  return dev::recover(sig, sha3(_rlp));
62 }
63 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
bytesRef ref()
Definition: FixedHash.h:133
const char * name
Definition: rest.cpp:36
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:203
std::vector< byte > bytes
Definition: Common.h:75
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
_T * data() const
Definition: vector_ref.h:51
uint8_t const size_t const size
Definition: sha3.h:20
void copyTo(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, up to the max size of _t...
Definition: vector_ref.h:69
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
Class for writing to an RLP bytestream.
Definition: RLP.h:383
RLPStream & appendRaw(bytesConstRef _rlp, size_t _itemCount=1)
Appends raw (pre-serialised) RLP data. Use with caution.
Definition: RLP.cpp:230
uint8_t const * data
Definition: sha3.h:19