-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathencoding_and_decoding.cpp
More file actions
41 lines (34 loc) · 1.2 KB
/
encoding_and_decoding.cpp
File metadata and controls
41 lines (34 loc) · 1.2 KB
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
#include <pgp-packet/packet.h>
#include <sodium.h>
#include <iostream>
#include <cassert>
#include <vector>
int main()
{
// initialize libsodium
if (sodium_init() == -1) {
return 1;
}
// create our simple user id packet
pgp::packet packet{
pgp::in_place_type_t<pgp::user_id>{},
std::string{ "Anne Onymous <anonymous@example.org>" }
};
// create a buffer for storing the binary data - we allocate the
// exact number of bytes the packet requests, and then we create
// a range_encoder around it. the range_encoder works by writing
// the raw data to a provided range of bytes, which must stay in
// scope during the encoder operation.
pgp::vector<uint8_t> data;
data.resize(packet.size());
// write out the packet data to the given buffer
packet.encode(pgp::range_encoder{ data });
// now create a decoder to decode the freshly filled data buffer
// and use this decoder to create a second packet containing the
// same user id body with the exact same data
pgp::decoder decoder{ data };
pgp::packet copied_packet{ decoder };
// the packets should be identical
assert(packet == copied_packet);
return 0;
}