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
 

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 10 of file md5_hash.h.

Constructor & Destructor Documentation

MD5_HASH::MD5_HASH ( )

Definition at line 32 of file md5_hash.cpp.

References Init().

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

Definition at line 37 of file md5_hash.cpp.

References m_ctx, m_hash, and m_valid.

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:45
uint8_t m_hash[16]
Definition: md5_hash.h:47
MD5_CTX m_ctx
Definition: md5_hash.h:46
MD5_HASH::~MD5_HASH ( )

Definition at line 44 of file md5_hash.cpp.

45 {
46 
47 }

Member Function Documentation

void MD5_HASH::Finalize ( )

Definition at line 76 of file md5_hash.cpp.

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

Referenced by SHAPE_POLY_SET::checksum().

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:210
bool m_valid
Definition: md5_hash.h:45
uint8_t m_hash[16]
Definition: md5_hash.h:47
MD5_CTX m_ctx
Definition: md5_hash.h:46
void MD5_HASH::Hash ( uint8_t *  data,
uint32_t  length 
)

Definition at line 66 of file md5_hash.cpp.

References m_ctx, and md5_update().

Referenced by SHAPE_POLY_SET::checksum().

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:195
MD5_CTX m_ctx
Definition: md5_hash.h:46
void MD5_HASH::Hash ( int  value)

Definition at line 71 of file md5_hash.cpp.

References m_ctx, and md5_update().

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:195
MD5_CTX m_ctx
Definition: md5_hash.h:46
void MD5_HASH::Init ( )

Definition at line 59 of file md5_hash.cpp.

References m_ctx, m_valid, and md5_init().

Referenced by MD5_HASH().

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:184
bool m_valid
Definition: md5_hash.h:45
MD5_CTX m_ctx
Definition: md5_hash.h:46
bool MD5_HASH::IsValid ( ) const
inline

Definition at line 23 of file md5_hash.h.

References m_valid.

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

23 { return m_valid; };
bool m_valid
Definition: md5_hash.h:45
void MD5_HASH::md5_final ( MD5_CTX ctx,
uint8_t  hash[] 
)
private

Definition at line 210 of file md5_hash.cpp.

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().

211 {
212  uint32_t i;
213 
214  i = ctx->datalen;
215 
216  // Pad whatever data is left in the buffer.
217  if (ctx->datalen < 56) {
218  ctx->data[i++] = 0x80;
219  while (i < 56)
220  ctx->data[i++] = 0x00;
221  }
222  else if (ctx->datalen >= 56) {
223  ctx->data[i++] = 0x80;
224  while (i < 64)
225  ctx->data[i++] = 0x00;
226  md5_transform(ctx,ctx->data);
227  memset(ctx->data,0,56);
228  }
229 
230  // Append to the padding the total message's length in bits and transform.
231  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],8 * ctx->datalen);
232  ctx->data[56] = ctx->bitlen[0];
233  ctx->data[57] = ctx->bitlen[0] >> 8;
234  ctx->data[58] = ctx->bitlen[0] >> 16;
235  ctx->data[59] = ctx->bitlen[0] >> 24;
236  ctx->data[60] = ctx->bitlen[1];
237  ctx->data[61] = ctx->bitlen[1] >> 8;
238  ctx->data[62] = ctx->bitlen[1] >> 16;
239  ctx->data[63] = ctx->bitlen[1] >> 24;
240  md5_transform(ctx,ctx->data);
241 
242  // Since this implementation uses little endian byte ordering and MD uses big endian,
243  // reverse all the bytes when copying the final state to the output hash.
244  for (i=0; i < 4; ++i) {
245  hash[i] = (ctx->state[0] >> (i*8)) & 0x000000ff;
246  hash[i+4] = (ctx->state[1] >> (i*8)) & 0x000000ff;
247  hash[i+8] = (ctx->state[2] >> (i*8)) & 0x000000ff;
248  hash[i+12] = (ctx->state[3] >> (i*8)) & 0x000000ff;
249  }
250 }
void md5_transform(MD5_CTX *ctx, uint8_t data[])
Definition: md5_hash.cpp:95
size_t i
Definition: json11.cpp:597
#define DBL_INT_ADD(a, b, c)
Definition: md5_hash.cpp:12
void MD5_HASH::md5_init ( MD5_CTX ctx)
private

Definition at line 184 of file md5_hash.cpp.

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

Referenced by Init().

185 {
186  ctx->datalen = 0;
187  ctx->bitlen[0] = 0;
188  ctx->bitlen[1] = 0;
189  ctx->state[0] = 0x67452301;
190  ctx->state[1] = 0xEFCDAB89;
191  ctx->state[2] = 0x98BADCFE;
192  ctx->state[3] = 0x10325476;
193 }
void MD5_HASH::md5_transform ( MD5_CTX ctx,
uint8_t  data[] 
)
private

Definition at line 95 of file md5_hash.cpp.

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

Referenced by md5_final(), and md5_update().

96 {
97  uint32_t a,b,c,d,m[16],i,j;
98 
99  // MD5 specifies big endian byte order, but this implementation assumes a little
100  // endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
101  // on output (in md5_final()).
102  for (i=0,j=0; i < 16; ++i, j += 4)
103  m[i] = (data[j]) + (data[j+1] << 8) + (data[j+2] << 16) + (data[j+3] << 24);
104 
105  a = ctx->state[0];
106  b = ctx->state[1];
107  c = ctx->state[2];
108  d = ctx->state[3];
109 
110  FF(a,b,c,d,m[0], 7,0xd76aa478);
111  FF(d,a,b,c,m[1], 12,0xe8c7b756);
112  FF(c,d,a,b,m[2], 17,0x242070db);
113  FF(b,c,d,a,m[3], 22,0xc1bdceee);
114  FF(a,b,c,d,m[4], 7,0xf57c0faf);
115  FF(d,a,b,c,m[5], 12,0x4787c62a);
116  FF(c,d,a,b,m[6], 17,0xa8304613);
117  FF(b,c,d,a,m[7], 22,0xfd469501);
118  FF(a,b,c,d,m[8], 7,0x698098d8);
119  FF(d,a,b,c,m[9], 12,0x8b44f7af);
120  FF(c,d,a,b,m[10],17,0xffff5bb1);
121  FF(b,c,d,a,m[11],22,0x895cd7be);
122  FF(a,b,c,d,m[12], 7,0x6b901122);
123  FF(d,a,b,c,m[13],12,0xfd987193);
124  FF(c,d,a,b,m[14],17,0xa679438e);
125  FF(b,c,d,a,m[15],22,0x49b40821);
126 
127  GG(a,b,c,d,m[1], 5,0xf61e2562);
128  GG(d,a,b,c,m[6], 9,0xc040b340);
129  GG(c,d,a,b,m[11],14,0x265e5a51);
130  GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
131  GG(a,b,c,d,m[5], 5,0xd62f105d);
132  GG(d,a,b,c,m[10], 9,0x02441453);
133  GG(c,d,a,b,m[15],14,0xd8a1e681);
134  GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
135  GG(a,b,c,d,m[9], 5,0x21e1cde6);
136  GG(d,a,b,c,m[14], 9,0xc33707d6);
137  GG(c,d,a,b,m[3], 14,0xf4d50d87);
138  GG(b,c,d,a,m[8], 20,0x455a14ed);
139  GG(a,b,c,d,m[13], 5,0xa9e3e905);
140  GG(d,a,b,c,m[2], 9,0xfcefa3f8);
141  GG(c,d,a,b,m[7], 14,0x676f02d9);
142  GG(b,c,d,a,m[12],20,0x8d2a4c8a);
143 
144  HH(a,b,c,d,m[5], 4,0xfffa3942);
145  HH(d,a,b,c,m[8], 11,0x8771f681);
146  HH(c,d,a,b,m[11],16,0x6d9d6122);
147  HH(b,c,d,a,m[14],23,0xfde5380c);
148  HH(a,b,c,d,m[1], 4,0xa4beea44);
149  HH(d,a,b,c,m[4], 11,0x4bdecfa9);
150  HH(c,d,a,b,m[7], 16,0xf6bb4b60);
151  HH(b,c,d,a,m[10],23,0xbebfbc70);
152  HH(a,b,c,d,m[13], 4,0x289b7ec6);
153  HH(d,a,b,c,m[0], 11,0xeaa127fa);
154  HH(c,d,a,b,m[3], 16,0xd4ef3085);
155  HH(b,c,d,a,m[6], 23,0x04881d05);
156  HH(a,b,c,d,m[9], 4,0xd9d4d039);
157  HH(d,a,b,c,m[12],11,0xe6db99e5);
158  HH(c,d,a,b,m[15],16,0x1fa27cf8);
159  HH(b,c,d,a,m[2], 23,0xc4ac5665);
160 
161  II(a,b,c,d,m[0], 6,0xf4292244);
162  II(d,a,b,c,m[7], 10,0x432aff97);
163  II(c,d,a,b,m[14],15,0xab9423a7);
164  II(b,c,d,a,m[5], 21,0xfc93a039);
165  II(a,b,c,d,m[12], 6,0x655b59c3);
166  II(d,a,b,c,m[3], 10,0x8f0ccc92);
167  II(c,d,a,b,m[10],15,0xffeff47d);
168  II(b,c,d,a,m[1], 21,0x85845dd1);
169  II(a,b,c,d,m[8], 6,0x6fa87e4f);
170  II(d,a,b,c,m[15],10,0xfe2ce6e0);
171  II(c,d,a,b,m[6], 15,0xa3014314);
172  II(b,c,d,a,m[13],21,0x4e0811a1);
173  II(a,b,c,d,m[4], 6,0xf7537e82);
174  II(d,a,b,c,m[11],10,0xbd3af235);
175  II(c,d,a,b,m[2], 15,0x2ad7d2bb);
176  II(b,c,d,a,m[9], 21,0xeb86d391);
177 
178  ctx->state[0] += a;
179  ctx->state[1] += b;
180  ctx->state[2] += c;
181  ctx->state[3] += d;
182 }
#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:597
void MD5_HASH::md5_update ( MD5_CTX ctx,
uint8_t  data[],
uint32_t  len 
)
private

Definition at line 195 of file md5_hash.cpp.

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().

196 {
197  uint32_t i;
198 
199  for (i=0; i < len; ++i) {
200  ctx->data[ctx->datalen] = data[i];
201  ctx->datalen++;
202  if (ctx->datalen == 64) {
203  md5_transform(ctx,ctx->data);
204  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],512);
205  ctx->datalen = 0;
206  }
207  }
208 }
void md5_transform(MD5_CTX *ctx, uint8_t data[])
Definition: md5_hash.cpp:95
size_t i
Definition: json11.cpp:597
#define DBL_INT_ADD(a, b, c)
Definition: md5_hash.cpp:12
bool MD5_HASH::operator!= ( const MD5_HASH aOther) const

Definition at line 89 of file md5_hash.cpp.

References m_hash.

Referenced by SetValid().

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

Definition at line 49 of file md5_hash.cpp.

References m_ctx, m_hash, and m_valid.

Referenced by SetValid().

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:45
uint8_t m_hash[16]
Definition: md5_hash.h:47
MD5_CTX m_ctx
Definition: md5_hash.h:46
bool MD5_HASH::operator== ( const MD5_HASH aOther) const

Definition at line 84 of file md5_hash.cpp.

References m_hash.

Referenced by SetValid().

85 {
86  return ( memcmp( m_hash, aOther.m_hash, 16 ) == 0 );
87 }
uint8_t m_hash[16]
Definition: md5_hash.h:47
void MD5_HASH::SetValid ( bool  aValid)
inline

Definition at line 25 of file md5_hash.h.

References m_valid, operator!=(), operator=(), and operator==().

25 { m_valid = aValid; }
bool m_valid
Definition: md5_hash.h:45

Member Data Documentation

MD5_CTX MD5_HASH::m_ctx
private

Definition at line 46 of file md5_hash.h.

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

uint8_t MD5_HASH::m_hash[16]
private

Definition at line 47 of file md5_hash.h.

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

bool MD5_HASH::m_valid
private

Definition at line 45 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: