OpenCPN Partial API docs
Loading...
Searching...
No Matches
pincode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 Alec Leamas *
3 * *
4 * This program 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 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <algorithm>
25#include <iomanip>
26#include <random>
27#include <sstream>
28
29#include "picosha2.h"
30
31#include "model/pincode.h"
32
33std::string Pincode::CompatHash() {
34 std::linear_congruential_engine<unsigned long long, 48271, 0,
35 0xFFFFFFFFFFFFFFFF>
36 engine;
37 engine.seed(m_value);
38 unsigned long long compat_val = engine();
39 char buffer[100];
40 snprintf(buffer, sizeof(buffer) - 1, "%0llX", compat_val);
41 return std::string(buffer);
42}
43
45 srand(time(0));
46 return Pincode(std::min(rand() % 10000 + 1, 9999));
47}
48
49uint64_t Pincode::Get() const { return m_value; }
50
51std::string Pincode::ToString() const {
52 std::stringstream ss;
53 ss << std::setw(4) << std::setfill('0') << m_value;
54 return ss.str();
55}
56
57std::string Pincode::Hash() const {
58 std::string hash_hex_str;
59 picosha2::hash256_hex_string(ToString(), hash_hex_str);
60 return hash_hex_str.substr(0, 12);
61}
62
63std::string Pincode::IntToHash(uint64_t value) { return Pincode(value).Hash(); }
A random generated int value with accessors for string and hashcode.
Definition pincode.h:31
static std::string IntToHash(uint64_t value)
convert numeric value to hash string.
Definition pincode.cpp:63
static Pincode Create()
Create a new pincode based on a random value.
Definition pincode.cpp:44
std::string Hash() const
Return a hashvalue string.
Definition pincode.cpp:57
std::string ToString() const
Return value as string.
Definition pincode.cpp:51
uint64_t Get() const
Return numeric value:
Definition pincode.cpp:49
std::string CompatHash()
Return a hashvalue as computed on 5.8 hosts.
Definition pincode.cpp:33
Peer communications pincode abstraction.