OpenCPN Partial API docs
Loading...
Searching...
No Matches
pincode.cpp
1
2
3/***************************************************************************
4 * Copyright (C) 2023 Alec Leamas *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 **************************************************************************/
21
22#include "model/pincode.h"
23
24#include <algorithm>
25#include <iomanip>
26#include <random>
27#include <sstream>
28
29#include "picosha2.h"
30
31std::string Pincode::CompatHash() {
32 std::linear_congruential_engine<unsigned long long, 48271, 0,
33 0xFFFFFFFFFFFFFFFF>
34 engine;
35 engine.seed(m_value);
36 unsigned long long compat_val = engine();
37 char buffer[100];
38 snprintf(buffer, sizeof(buffer) - 1, "%0llX", compat_val);
39 return std::string(buffer);
40}
41
43 srand(time(0));
44 return Pincode(std::min(rand() % 10000 + 1, 9999));
45}
46
47uint64_t Pincode::Get() const { return m_value; }
48
49std::string Pincode::ToString() const {
50 std::stringstream ss;
51 ss << std::setw(4) << std::setfill('0') << m_value;
52 return ss.str();
53}
54
55std::string Pincode::Hash() const {
56 std::string hash_hex_str;
57 picosha2::hash256_hex_string(ToString(), hash_hex_str);
58 return hash_hex_str.substr(0, 12);
59}
60
61std::string Pincode::IntToHash(uint64_t value) { return Pincode(value).Hash(); }
A random generated int value with accessors for string and hashcode.
Definition pincode.h:27
static std::string IntToHash(uint64_t value)
convert numeric value to hash string.
Definition pincode.cpp:61
static Pincode Create()
Create a new pincode based on a random value.
Definition pincode.cpp:42
std::string Hash() const
Return a hashvalue string.
Definition pincode.cpp:55
std::string ToString() const
Return value as string.
Definition pincode.cpp:49
uint64_t Get() const
Return numeric value:
Definition pincode.cpp:47
std::string CompatHash()
Return a hashvalue as computed on 5.8 hosts.
Definition pincode.cpp:31