Fabcoin Core  0.16.2
P2P Digital Currency
adler32.cpp
Go to the documentation of this file.
1 // adler32.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "adler32.h"
5 
7 
8 void Adler32::Update(const byte *input, size_t length)
9 {
10  const unsigned long BASE = 65521;
11 
12  unsigned long s1 = m_s1;
13  unsigned long s2 = m_s2;
14 
15  if (length % 8 != 0)
16  {
17  do
18  {
19  s1 += *input++;
20  s2 += s1;
21  length--;
22  } while (length % 8 != 0);
23 
24  if (s1 >= BASE)
25  s1 -= BASE;
26  s2 %= BASE;
27  }
28 
29  while (length > 0)
30  {
31  s1 += input[0]; s2 += s1;
32  s1 += input[1]; s2 += s1;
33  s1 += input[2]; s2 += s1;
34  s1 += input[3]; s2 += s1;
35  s1 += input[4]; s2 += s1;
36  s1 += input[5]; s2 += s1;
37  s1 += input[6]; s2 += s1;
38  s1 += input[7]; s2 += s1;
39 
40  length -= 8;
41  input += 8;
42 
43  if (s1 >= BASE)
44  s1 -= BASE;
45  if (length % 0x8000 == 0)
46  s2 %= BASE;
47  }
48 
49  CRYPTOPP_ASSERT(s1 < BASE);
50  CRYPTOPP_ASSERT(s2 < BASE);
51 
52  m_s1 = (word16)s1;
53  m_s2 = (word16)s2;
54 }
55 
56 void Adler32::TruncatedFinal(byte *hash, size_t size)
57 {
59 
60  switch (size)
61  {
62  default:
63  hash[3] = byte(m_s1);
64  // fall through
65  case 3:
66  hash[2] = byte(m_s1 >> 8);
67  // fall through
68  case 2:
69  hash[1] = byte(m_s2);
70  // fall through
71  case 1:
72  hash[0] = byte(m_s2 >> 8);
73  // fall through
74  case 0:
75  ;;
76  // fall through
77  }
78 
79  Reset();
80 }
81 
uint8_t byte
Definition: Common.h:57
unsigned short word16
Definition: config.h:230
static const std::string s2("AAD")
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: adler32.cpp:56
void ThrowIfInvalidTruncatedSize(size_t size) const
Validates a truncated digest size.
Definition: cryptlib.cpp:416
ADLER-32 checksum calculations.
Definition: adler32.h:15
void Reset()
Definition: adler32.h:27
Class file for ADLER-32 checksum calculations.
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
uint8_t const size_t const size
Definition: sha3.h:20
uint8_t byte
Definition: Common.h:10
#define NAMESPACE_END
Definition: config.h:201
word16 m_s2
Definition: adler32.h:29
word16 m_s1
Definition: adler32.h:29
#define s1(x)
Definition: sha256.c:70