-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.cc
122 lines (112 loc) · 2.6 KB
/
id.cc
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "id.hpp"
#include <openssl/rand.h>
#include <string>
#include "hash.hpp"
#include <time.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <cstring>
#include "rng.hpp"
using namespace trankesbel;
using namespace dfterm;
using namespace std;
ID::ID()
{
memset(id_value, 0, 64);
makeRandomBytes( (trankesbel::ui8*) id_value, 64);
}
string ID::serialize() const
{
return (string) bytes_to_hex(data1D((char*) id_value, 64));
}
void ID::serialize(std::string &s) const
{
s = bytes_to_hex(data1D((char*) id_value, 64));
}
ID ID::getUnSerialized(const std::string &s)
{
ID id_value;
if (s.size() == 128)
{
string s2 = hex_to_bytes(s);
memcpy(id_value.id_value, s2.c_str(), 64);
}
else
{
string hashed = hash_data(s);
if (hashed.size() == 128)
{
hashed = hex_to_bytes(hashed);
memcpy(id_value.id_value, hashed.c_str(), 64);
}
else
makeRandomBytes( (trankesbel::ui8*) id_value.id_value, 64);
}
return id_value;
}
ID::ID(const std::string &s)
{
if (s.size() == 128)
{
string s2 = hex_to_bytes(s);
memcpy(this->id_value, s2.c_str(), 64);
}
else
{
string hashed = hash_data(s);
if (hashed.size() == 128)
{
hashed = hex_to_bytes(hashed);
memcpy(this->id_value, hashed.c_str(), 64);
}
else
makeRandomBytes( (trankesbel::ui8*) this->id_value, 64);
}
}
ID::ID(const char* s, size_t s_size)
{
if (s_size == 128)
{
string s2 = hex_to_bytes(string(s, s_size));
memcpy(this->id_value, s2.c_str(), 64);
}
else
{
string hashed = hash_data(string(s, s_size));
if (hashed.size() == 128)
{
hashed = hex_to_bytes(hashed);
memcpy(this->id_value, hashed.c_str(), 64);
}
else
makeRandomBytes( (trankesbel::ui8*) this->id_value, 64);
}
}
void ID::unSerialize(const std::string &s)
{
if (s.size() == 128)
{
string s2 = hex_to_bytes(s);
memcpy(this->id_value, s2.c_str(), 64);
}
else
{
string hashed = hash_data(s);
if (hashed.size() == 128)
{
hashed = hex_to_bytes(hashed);
memcpy(this->id_value, hashed.c_str(), 64);
}
else
makeRandomBytes( (trankesbel::ui8*) this->id_value, 64);
}
}
bool ID::operator==(const ID& id_value) const
{
ui32 i1;
for (i1 = 0; i1 < 64; ++i1)
if (this->id_value[i1] != id_value.id_value[i1])
return false;
return true;
}