33#define FLEXHASH_INTERNAL_SIZE static_cast<size_t>(20)
35FlexHash::FlexHash(
size_t output_octets) : m_Output(output_octets) {}
37void FlexHash::Reset(
void) { sha1_starts(&this->m_Context); }
39void FlexHash::Update(
const void* input,
size_t input_octets) {
40 sha1_update(&this->m_Context,
reinterpret_cast<const unsigned char*
>(input),
44void FlexHash::Finish(
void) {
45 unsigned char output[FLEXHASH_INTERNAL_SIZE];
46 sha1_finish(&this->m_Context, output);
47 if (this->m_Output.size() <= FLEXHASH_INTERNAL_SIZE) {
48 memcpy(&this->m_Output[0], output, this->m_Output.size());
50 memcpy(&this->m_Output[0], output, FLEXHASH_INTERNAL_SIZE);
51 size_t available_octets = FLEXHASH_INTERNAL_SIZE;
52 size_t remaining_octets = this->m_Output.size() - available_octets;
53 while (remaining_octets) {
54 size_t current_octets =
55 ((remaining_octets > FLEXHASH_INTERNAL_SIZE) ? FLEXHASH_INTERNAL_SIZE
57 sha1_starts(&this->m_Context);
58 sha1_update(&this->m_Context,
59 reinterpret_cast<const unsigned char*
>(&this->m_Output[0]),
61 sha1_finish(&this->m_Context, output);
62 memcpy(&this->m_Output[available_octets], output, current_octets);
63 available_octets += FLEXHASH_INTERNAL_SIZE;
64 remaining_octets -= current_octets;
69void FlexHash::Receive(
void* output) {
70 memcpy(output, &this->m_Output[0], this->m_Output.size());
73void FlexHash::Compute(
const void* input,
size_t input_octets,
void* output) {
75 this->Update(input, input_octets);
77 this->Receive(output);
80void FlexHash::Compute(
const void* input,
size_t input_octets,
void* output,
81 size_t output_octets) {
83 hasher.Compute(input, input_octets, output);
86bool FlexHash::Test(
void) {
88 static unsigned char input[] = {
89 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62,
90 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75,
91 0x6d, 0x70, 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68,
92 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67};
94 static unsigned char output_reference[] = {
95 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e,
96 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12, 0xa4, 0xe4,
97 0xd2, 0x6f, 0xd0, 0xc6, 0x45, 0x5e, 0x23, 0xe2, 0x18, 0x7c};
98 char output_current[(
sizeof output_reference)];
99 Compute(input, (
sizeof input), output_current, (
sizeof output_current));
100 return (memcmp(output_current, output_reference, (
sizeof output_reference)) ==