OpenCPN Partial API docs
Loading...
Searching...
No Matches
flex_hash.cpp
1/**************************************************************************
2 * Copyright (C) 2010 by David S. Register *
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 <vector>
25#include <memory.h>
26#include "flex_hash.h"
27
28#define FLEXHASH_INTERNAL_SIZE static_cast<size_t>(20)
29
30FlexHash::FlexHash(size_t output_octets) : m_Output(output_octets) {}
31
32void FlexHash::Reset(void) { sha1_starts(&this->m_Context); }
33
34void FlexHash::Update(const void* input, size_t input_octets) {
35 sha1_update(&this->m_Context, reinterpret_cast<const unsigned char*>(input),
36 input_octets);
37}
38
39void FlexHash::Finish(void) {
40 unsigned char output[FLEXHASH_INTERNAL_SIZE];
41 sha1_finish(&this->m_Context, output);
42 if (this->m_Output.size() <= FLEXHASH_INTERNAL_SIZE) {
43 memcpy(&this->m_Output[0], output, this->m_Output.size());
44 } else {
45 memcpy(&this->m_Output[0], output, FLEXHASH_INTERNAL_SIZE);
46 size_t available_octets = FLEXHASH_INTERNAL_SIZE;
47 size_t remaining_octets = this->m_Output.size() - available_octets;
48 while (remaining_octets) {
49 size_t current_octets =
50 ((remaining_octets > FLEXHASH_INTERNAL_SIZE) ? FLEXHASH_INTERNAL_SIZE
51 : remaining_octets);
52 sha1_starts(&this->m_Context);
53 sha1_update(&this->m_Context,
54 reinterpret_cast<const unsigned char*>(&this->m_Output[0]),
55 available_octets);
56 sha1_finish(&this->m_Context, output);
57 memcpy(&this->m_Output[available_octets], output, current_octets);
58 available_octets += FLEXHASH_INTERNAL_SIZE;
59 remaining_octets -= current_octets;
60 }
61 }
62}
63
64void FlexHash::Receive(void* output) {
65 memcpy(output, &this->m_Output[0], this->m_Output.size());
66}
67
68void FlexHash::Compute(const void* input, size_t input_octets, void* output) {
69 this->Reset();
70 this->Update(input, input_octets);
71 this->Finish();
72 this->Receive(output);
73}
74
75void FlexHash::Compute(const void* input, size_t input_octets, void* output,
76 size_t output_octets) {
77 FlexHash hasher(output_octets);
78 hasher.Compute(input, input_octets, output);
79}
80
81bool FlexHash::Test(void) {
82 // Input test vector for "The quick brown fox jumps over the lazy dog".
83 static unsigned char input[] = {
84 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62,
85 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75,
86 0x6d, 0x70, 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68,
87 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67};
88 // Output test vector for SHA-1 engine and 256-bit result.
89 static unsigned char output_reference[] = {
90 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e,
91 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12, 0xa4, 0xe4,
92 0xd2, 0x6f, 0xd0, 0xc6, 0x45, 0x5e, 0x23, 0xe2, 0x18, 0x7c};
93 char output_current[(sizeof output_reference)];
94 Compute(input, (sizeof input), output_current, (sizeof output_current));
95 return (memcmp(output_current, output_reference, (sizeof output_reference)) ==
96 0);
97}
Class for computing hash of arbitrary length.
Definition flex_hash.h:34
Hash of arbitrary length.