OpenCPN Partial API docs
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 
31 std::string Pincode::CompatHash() {
32  std::linear_congruential_engine<unsigned long long, 48271, 0,
33  0xFFFFFFFFFFFFFFFF> engine;
34  engine.seed(m_value);
35  unsigned long long compat_val = engine();
36  char buffer[100];
37  snprintf(buffer, sizeof(buffer)-1, "%0llX", compat_val);
38  return std::string(buffer);
39 }
40 
42  srand(time(0));
43  return Pincode(std::min(rand() % 10000 + 1, 9999));
44 }
45 
46 uint64_t Pincode::Get() const { return m_value; }
47 
48 std::string Pincode::ToString() const {
49  std::stringstream ss;
50  ss << std::setw(4) << std::setfill('0') << m_value;
51  return ss.str();
52 }
53 
54 std::string Pincode::Hash() const {
55  std::string hash_hex_str;
56  picosha2::hash256_hex_string(ToString(), hash_hex_str);
57  return hash_hex_str.substr(0,12);
58 }
59 
60 std::string Pincode::IntToHash(uint64_t value) {
61  return Pincode(value).Hash();
62 }
A random generated int value with accessors for string and hashcode.
Definition: pincode.h:27
Pincode(uint64_t v)
Create a new pincode based on a known value.
Definition: pincode.h:33
static std::string IntToHash(uint64_t value)
convert numeric value to hash string.
Definition: pincode.cpp:60
static Pincode Create()
Create a new pincode based on a random value.
Definition: pincode.cpp:41
std::string Hash() const
Return a hashvalue string.
Definition: pincode.cpp:54
std::string ToString() const
Return value as string.
Definition: pincode.cpp:48
uint64_t Get() const
Return numeric value:
Definition: pincode.cpp:46
std::string CompatHash()
Return a hashvalue as computed on 5.8 hosts.
Definition: pincode.cpp:31