KiCad PCB EDA Suite
MD5_HASH Class Reference

#include <md5_hash.h>

Classes

struct  MD5_CTX
 

Public Member Functions

 MD5_HASH ()
 
 MD5_HASH (const MD5_HASH &aOther)
 
 ~MD5_HASH ()
 
void Init ()
 
void Hash (uint8_t *data, uint32_t length)
 
void Hash (int value)
 
void Finalize ()
 
bool IsValid () const
 
void SetValid (bool aValid)
 
MD5_HASHoperator= (const MD5_HASH &aOther)
 
bool operator== (const MD5_HASH &aOther) const
 
bool operator!= (const MD5_HASH &aOther) const
 
std::string Format ()
 

Private Member Functions

void md5_transform (MD5_CTX *ctx, uint8_t data[])
 
void md5_init (MD5_CTX *ctx)
 
void md5_update (MD5_CTX *ctx, uint8_t data[], uint32_t len)
 
void md5_final (MD5_CTX *ctx, uint8_t hash[])
 

Private Attributes

bool m_valid
 
MD5_CTX m_ctx
 
uint8_t m_hash [16]
 

Detailed Description

Definition at line 11 of file md5_hash.h.

Constructor & Destructor Documentation

◆ MD5_HASH() [1/2]

MD5_HASH::MD5_HASH ( )

Definition at line 32 of file md5_hash.cpp.

33 {
34  Init();
35 }
void Init()
Definition: md5_hash.cpp:59

References Init().

◆ MD5_HASH() [2/2]

MD5_HASH::MD5_HASH ( const MD5_HASH aOther)

Definition at line 37 of file md5_hash.cpp.

38 {
39  m_valid = aOther.m_valid;
40  m_ctx = aOther.m_ctx;
41  memcpy( m_hash, aOther.m_hash, 16 );
42 }
bool m_valid
Definition: md5_hash.h:51
uint8_t m_hash[16]
Definition: md5_hash.h:53
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, m_hash, and m_valid.

◆ ~MD5_HASH()

MD5_HASH::~MD5_HASH ( )

Definition at line 44 of file md5_hash.cpp.

45 {
46 
47 }

Member Function Documentation

◆ Finalize()

void MD5_HASH::Finalize ( )

Definition at line 76 of file md5_hash.cpp.

77 {
78  //printf("%p final\n", this);
80  m_valid = true;
81 
82 }
void md5_final(MD5_CTX *ctx, uint8_t hash[])
Definition: md5_hash.cpp:236
bool m_valid
Definition: md5_hash.h:51
uint8_t m_hash[16]
Definition: md5_hash.h:53
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, m_hash, m_valid, and md5_final().

Referenced by SHAPE_POLY_SET::checksum().

◆ Format()

std::string MD5_HASH::Format ( )
Returns
Build a hexadecimal string from the 16 bytes of MD5_HASH Mainly for debug purposes.

Definition at line 95 of file md5_hash.cpp.

96 {
97  std::string data;
98 
99  // Build a hexadecimal string from the 16 bytes of MD5_HASH:
100  for( int ii = 0; ii < 16; ++ii )
101  {
102  char lsb = ( m_hash[ii] & 0x0F ) + '0';
103 
104  if( lsb > '9' )
105  lsb += 'A'-'9';
106 
107  char msb = ( ( m_hash[ii] >> 4 ) & 0x0F ) + '0';
108 
109  if( msb > '9' )
110  msb += 'A'-'9';
111 
112  data += msb;
113  data += lsb;
114  data += ' ';
115  }
116 
117  return data;
118 }
uint8_t m_hash[16]
Definition: md5_hash.h:53

References m_hash.

◆ Hash() [1/2]

void MD5_HASH::Hash ( uint8_t *  data,
uint32_t  length 
)

Definition at line 66 of file md5_hash.cpp.

67 {
68  md5_update(&m_ctx, data, length);
69 }
void md5_update(MD5_CTX *ctx, uint8_t data[], uint32_t len)
Definition: md5_hash.cpp:221
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, and md5_update().

Referenced by SHAPE_POLY_SET::checksum().

◆ Hash() [2/2]

void MD5_HASH::Hash ( int  value)

Definition at line 71 of file md5_hash.cpp.

72 {
73  md5_update(&m_ctx, (uint8_t*) &value, sizeof(int) );
74 }
void md5_update(MD5_CTX *ctx, uint8_t data[], uint32_t len)
Definition: md5_hash.cpp:221
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, and md5_update().

◆ Init()

void MD5_HASH::Init ( )

Definition at line 59 of file md5_hash.cpp.

60 {
61  //printf("%p init\n", this);
62  m_valid = false;
63  md5_init(&m_ctx);
64 }
void md5_init(MD5_CTX *ctx)
Definition: md5_hash.cpp:210
bool m_valid
Definition: md5_hash.h:51
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, m_valid, and md5_init().

Referenced by MD5_HASH().

◆ IsValid()

bool MD5_HASH::IsValid ( ) const
inline

Definition at line 24 of file md5_hash.h.

24 { return m_valid; };
bool m_valid
Definition: md5_hash.h:51

References m_valid.

Referenced by SHAPE_POLY_SET::CacheTriangulation(), SHAPE_POLY_SET::GetHash(), and SHAPE_POLY_SET::IsTriangulationUpToDate().

◆ md5_final()

void MD5_HASH::md5_final ( MD5_CTX ctx,
uint8_t  hash[] 
)
private

Definition at line 236 of file md5_hash.cpp.

237 {
238  uint32_t i;
239 
240  i = ctx->datalen;
241 
242  // Pad whatever data is left in the buffer.
243  if (ctx->datalen < 56) {
244  ctx->data[i++] = 0x80;
245  while (i < 56)
246  ctx->data[i++] = 0x00;
247  }
248  else if (ctx->datalen >= 56) {
249  ctx->data[i++] = 0x80;
250  while (i < 64)
251  ctx->data[i++] = 0x00;
252  md5_transform(ctx,ctx->data);
253  memset(ctx->data,0,56);
254  }
255 
256  // Append to the padding the total message's length in bits and transform.
257  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],8 * ctx->datalen);
258  ctx->data[56] = ctx->bitlen[0];
259  ctx->data[57] = ctx->bitlen[0] >> 8;
260  ctx->data[58] = ctx->bitlen[0] >> 16;
261  ctx->data[59] = ctx->bitlen[0] >> 24;
262  ctx->data[60] = ctx->bitlen[1];
263  ctx->data[61] = ctx->bitlen[1] >> 8;
264  ctx->data[62] = ctx->bitlen[1] >> 16;
265  ctx->data[63] = ctx->bitlen[1] >> 24;
266  md5_transform(ctx,ctx->data);
267 
268  // Since this implementation uses little endian byte ordering and MD uses big endian,
269  // reverse all the bytes when copying the final state to the output hash.
270  for (i=0; i < 4; ++i) {
271  hash[i] = (ctx->state[0] >> (i*8)) & 0x000000ff;
272  hash[i+4] = (ctx->state[1] >> (i*8)) & 0x000000ff;
273  hash[i+8] = (ctx->state[2] >> (i*8)) & 0x000000ff;
274  hash[i+12] = (ctx->state[3] >> (i*8)) & 0x000000ff;
275  }
276 }
void md5_transform(MD5_CTX *ctx, uint8_t data[])
Definition: md5_hash.cpp:121
size_t i
Definition: json11.cpp:649
#define DBL_INT_ADD(a, b, c)
Definition: md5_hash.cpp:12

References MD5_HASH::MD5_CTX::bitlen, MD5_HASH::MD5_CTX::data, MD5_HASH::MD5_CTX::datalen, DBL_INT_ADD, i, md5_transform(), and MD5_HASH::MD5_CTX::state.

Referenced by Finalize().

◆ md5_init()

void MD5_HASH::md5_init ( MD5_CTX ctx)
private

Definition at line 210 of file md5_hash.cpp.

211 {
212  ctx->datalen = 0;
213  ctx->bitlen[0] = 0;
214  ctx->bitlen[1] = 0;
215  ctx->state[0] = 0x67452301;
216  ctx->state[1] = 0xEFCDAB89;
217  ctx->state[2] = 0x98BADCFE;
218  ctx->state[3] = 0x10325476;
219 }

References MD5_HASH::MD5_CTX::bitlen, MD5_HASH::MD5_CTX::datalen, and MD5_HASH::MD5_CTX::state.

Referenced by Init().

◆ md5_transform()

void MD5_HASH::md5_transform ( MD5_CTX ctx,
uint8_t  data[] 
)
private

Definition at line 121 of file md5_hash.cpp.

122 {
123  uint32_t a,b,c,d,m[16],i,j;
124 
125  // MD5 specifies big endian byte order, but this implementation assumes a little
126  // endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
127  // on output (in md5_final()).
128  for (i=0,j=0; i < 16; ++i, j += 4)
129  m[i] = (data[j]) + (data[j+1] << 8) + (data[j+2] << 16) + (data[j+3] << 24);
130 
131  a = ctx->state[0];
132  b = ctx->state[1];
133  c = ctx->state[2];
134  d = ctx->state[3];
135 
136  FF(a,b,c,d,m[0], 7,0xd76aa478);
137  FF(d,a,b,c,m[1], 12,0xe8c7b756);
138  FF(c,d,a,b,m[2], 17,0x242070db);
139  FF(b,c,d,a,m[3], 22,0xc1bdceee);
140  FF(a,b,c,d,m[4], 7,0xf57c0faf);
141  FF(d,a,b,c,m[5], 12,0x4787c62a);
142  FF(c,d,a,b,m[6], 17,0xa8304613);
143  FF(b,c,d,a,m[7], 22,0xfd469501);
144  FF(a,b,c,d,m[8], 7,0x698098d8);
145  FF(d,a,b,c,m[9], 12,0x8b44f7af);
146  FF(c,d,a,b,m[10],17,0xffff5bb1);
147  FF(b,c,d,a,m[11],22,0x895cd7be);
148  FF(a,b,c,d,m[12], 7,0x6b901122);
149  FF(d,a,b,c,m[13],12,0xfd987193);
150  FF(c,d,a,b,m[14],17,0xa679438e);
151  FF(b,c,d,a,m[15],22,0x49b40821);
152 
153  GG(a,b,c,d,m[1], 5,0xf61e2562);
154  GG(d,a,b,c,m[6], 9,0xc040b340);
155  GG(c,d,a,b,m[11],14,0x265e5a51);
156  GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
157  GG(a,b,c,d,m[5], 5,0xd62f105d);
158  GG(d,a,b,c,m[10], 9,0x02441453);
159  GG(c,d,a,b,m[15],14,0xd8a1e681);
160  GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
161  GG(a,b,c,d,m[9], 5,0x21e1cde6);
162  GG(d,a,b,c,m[14], 9,0xc33707d6);
163  GG(c,d,a,b,m[3], 14,0xf4d50d87);
164  GG(b,c,d,a,m[8], 20,0x455a14ed);
165  GG(a,b,c,d,m[13], 5,0xa9e3e905);
166  GG(d,a,b,c,m[2], 9,0xfcefa3f8);
167  GG(c,d,a,b,m[7], 14,0x676f02d9);
168  GG(b,c,d,a,m[12],20,0x8d2a4c8a);
169 
170  HH(a,b,c,d,m[5], 4,0xfffa3942);
171  HH(d,a,b,c,m[8], 11,0x8771f681);
172  HH(c,d,a,b,m[11],16,0x6d9d6122);
173  HH(b,c,d,a,m[14],23,0xfde5380c);
174  HH(a,b,c,d,m[1], 4,0xa4beea44);
175  HH(d,a,b,c,m[4], 11,0x4bdecfa9);
176  HH(c,d,a,b,m[7], 16,0xf6bb4b60);
177  HH(b,c,d,a,m[10],23,0xbebfbc70);
178  HH(a,b,c,d,m[13], 4,0x289b7ec6);
179  HH(d,a,b,c,m[0], 11,0xeaa127fa);
180  HH(c,d,a,b,m[3], 16,0xd4ef3085);
181  HH(b,c,d,a,m[6], 23,0x04881d05);
182  HH(a,b,c,d,m[9], 4,0xd9d4d039);
183  HH(d,a,b,c,m[12],11,0xe6db99e5);
184  HH(c,d,a,b,m[15],16,0x1fa27cf8);
185  HH(b,c,d,a,m[2], 23,0xc4ac5665);
186 
187  II(a,b,c,d,m[0], 6,0xf4292244);
188  II(d,a,b,c,m[7], 10,0x432aff97);
189  II(c,d,a,b,m[14],15,0xab9423a7);
190  II(b,c,d,a,m[5], 21,0xfc93a039);
191  II(a,b,c,d,m[12], 6,0x655b59c3);
192  II(d,a,b,c,m[3], 10,0x8f0ccc92);
193  II(c,d,a,b,m[10],15,0xffeff47d);
194  II(b,c,d,a,m[1], 21,0x85845dd1);
195  II(a,b,c,d,m[8], 6,0x6fa87e4f);
196  II(d,a,b,c,m[15],10,0xfe2ce6e0);
197  II(c,d,a,b,m[6], 15,0xa3014314);
198  II(b,c,d,a,m[13],21,0x4e0811a1);
199  II(a,b,c,d,m[4], 6,0xf7537e82);
200  II(d,a,b,c,m[11],10,0xbd3af235);
201  II(c,d,a,b,m[2], 15,0x2ad7d2bb);
202  II(b,c,d,a,m[9], 21,0xeb86d391);
203 
204  ctx->state[0] += a;
205  ctx->state[1] += b;
206  ctx->state[2] += c;
207  ctx->state[3] += d;
208 }
#define FF(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:20
#define GG(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:22
#define II(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:26
#define HH(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:24
size_t i
Definition: json11.cpp:649

References FF, GG, HH, i, II, and MD5_HASH::MD5_CTX::state.

Referenced by md5_final(), and md5_update().

◆ md5_update()

void MD5_HASH::md5_update ( MD5_CTX ctx,
uint8_t  data[],
uint32_t  len 
)
private

Definition at line 221 of file md5_hash.cpp.

222 {
223  uint32_t i;
224 
225  for (i=0; i < len; ++i) {
226  ctx->data[ctx->datalen] = data[i];
227  ctx->datalen++;
228  if (ctx->datalen == 64) {
229  md5_transform(ctx,ctx->data);
230  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],512);
231  ctx->datalen = 0;
232  }
233  }
234 }
void md5_transform(MD5_CTX *ctx, uint8_t data[])
Definition: md5_hash.cpp:121
size_t i
Definition: json11.cpp:649
#define DBL_INT_ADD(a, b, c)
Definition: md5_hash.cpp:12

References MD5_HASH::MD5_CTX::bitlen, MD5_HASH::MD5_CTX::data, MD5_HASH::MD5_CTX::datalen, DBL_INT_ADD, i, and md5_transform().

Referenced by Hash().

◆ operator!=()

bool MD5_HASH::operator!= ( const MD5_HASH aOther) const

Definition at line 89 of file md5_hash.cpp.

90 {
91  return ( memcmp( m_hash, aOther.m_hash, 16 ) != 0 );
92 }
uint8_t m_hash[16]
Definition: md5_hash.h:53

References m_hash.

◆ operator=()

MD5_HASH & MD5_HASH::operator= ( const MD5_HASH aOther)

Definition at line 49 of file md5_hash.cpp.

50 {
51  m_valid = aOther.m_valid;
52  m_ctx = aOther.m_ctx;
53  memcpy( m_hash, aOther.m_hash, 16 );
54 
55  return *this;
56 }
bool m_valid
Definition: md5_hash.h:51
uint8_t m_hash[16]
Definition: md5_hash.h:53
MD5_CTX m_ctx
Definition: md5_hash.h:52

References m_ctx, m_hash, and m_valid.

◆ operator==()

bool MD5_HASH::operator== ( const MD5_HASH aOther) const

Definition at line 84 of file md5_hash.cpp.

85 {
86  return ( memcmp( m_hash, aOther.m_hash, 16 ) == 0 );
87 }
uint8_t m_hash[16]
Definition: md5_hash.h:53

References m_hash.

◆ SetValid()

void MD5_HASH::SetValid ( bool  aValid)
inline

Definition at line 26 of file md5_hash.h.

26 { m_valid = aValid; }
bool m_valid
Definition: md5_hash.h:51

References m_valid.

Member Data Documentation

◆ m_ctx

MD5_CTX MD5_HASH::m_ctx
private

Definition at line 52 of file md5_hash.h.

Referenced by Finalize(), Hash(), Init(), MD5_HASH(), and operator=().

◆ m_hash

uint8_t MD5_HASH::m_hash[16]
private

Definition at line 53 of file md5_hash.h.

Referenced by Finalize(), Format(), MD5_HASH(), operator!=(), operator=(), and operator==().

◆ m_valid

bool MD5_HASH::m_valid
private

Definition at line 51 of file md5_hash.h.

Referenced by Finalize(), Init(), IsValid(), MD5_HASH(), operator=(), and SetValid().


The documentation for this class was generated from the following files: