-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.cpp
50 lines (42 loc) · 1.1 KB
/
Random.cpp
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
//
// random.cpp
// browser-cc
//
// Created by Nissassin Seventeen on 10/2/15.
// Copyright (c) 2015 Nissassin Seventeen. All rights reserved.
//
#include <stddef.h>
#include <cstdlib>
#include <ctime>
#include <initializer_list>
#include "Random.hpp"
#include "Util.hpp"
#include "BitUtil.hpp"
Random::Random() {
time_t t;
this->gmtUnixTime = (uint32_t) time(&t);
srand(this->gmtUnixTime);
for (int i = 0; i < RANDOM_BYTES_NUM / 4; i++) {
uint32_t r = rand();
for (int j = 0; j < 3; j++) {
this->randomBytes[i * 4 + j] = BitUtil::filterByte(r);
r = (uint32_t) BitUtil::cutLastBits(r, 8);
}
}
}
size_t Random::size() const {
return 4 + RANDOM_BYTES_NUM * 1;
}
Random::Random(const vector<uint8_t> &data, size_t offset) {
this->gmtUnixTime = Util::takeData32(data, offset);
offset += 4;
for (int i = 0; i < RANDOM_BYTES_NUM; i++)
this->randomBytes[i] = data[offset + i];
}
vector<uint8_t> Random::toData() const {
vector<uint8_t> data;
Util::addData(data, (uint32_t) this->gmtUnixTime);
data.insert(data.end(), this->randomBytes,
this->randomBytes + RANDOM_BYTES_NUM);
return data;
}