File: crypto_core_test_util.cc

package info (click to toggle)
libtoxcore 0.2.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,124 kB
  • sloc: ansic: 75,034; cpp: 4,933; sh: 1,115; python: 651; makefile: 329; perl: 39
file content (42 lines) | stat: -rw-r--r-- 1,053 bytes parent folder | download
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
#include "crypto_core_test_util.hh"

#include <cstring>
#include <iomanip>

#include "crypto_core.h"
#include "test_util.hh"

Random_Funcs const Random_Class::vtable = {
    Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
    Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
};

Random_Class::~Random_Class() = default;

void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length)
{
    std::generate(bytes, &bytes[length], std::ref(lcg));
}

uint32_t Test_Random::random_uniform(void *obj, uint32_t upper_bound)
{
    std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
    return distrib(lcg);
}

PublicKey random_pk(const Random *rng)
{
    PublicKey pk;
    random_bytes(rng, pk.data(), pk.size());
    return pk;
}

std::ostream &operator<<(std::ostream &out, PublicKey const &pk)
{
    out << '"';
    for (uint8_t byte : pk) {
        out << std::setw(2) << std::setfill('0') << std::hex << uint32_t(byte);
    }
    out << '"';
    return out;
}