Source file: /~heha/hsn/esptool.zip/md5.cpp

/* Message Digest Algorithm 5
 240110	heha erstellt aus Vorlage ??? in C++ (MSVC6-kompatibel)
 */
#include "sha.h"
#include <stdio.h>	// _snprintf
#include <memory.h>	// memset
#include "intrin.h"	// byteswap, rotl

void Md5::transform() {
 struct ABCD{
  uint32 a,b,c,d;
  ABCD(uvector<4>x):a(x[0]),b(x[1]),c(x[2]),d(x[3]) {}
  operator const uint32*() const {return &a;}
// F, G, H and I are basic MD5 functions
  uint32 F() const {return b&c |~b&d;}
  uint32 G() const {return d&b |~d&c;}
  uint32 H() const {return b^c^d;}
  uint32 I() const {return c^(b|~d);}
  void operator() (uint32 n) {a=d;d=c;c=b;b=n;}
  typedef uint32(ABCD::*FU)() const;
 }abcd(X);

 static const uvector<64> sintab={
  3614090360,3905402710, 606105819,3250441966,4118548399,1200080426,2821735955,4249261313,
  1770035416,2336552879,4294925233,2304563134,1804603682,4254626195,2792965006,1236535329,
  4129170786,3225465664, 643717713,3921069994,3593408605,  38016083,3634488961,3889429448,
   568446438,3275163606,4107603335,1163531501,2850285829,4243563512,1735328473,2368359562,
  4294588738,2272392833,1839030562,4259657740,2763975236,1272893353,4139469664,3200236656,
   681279174,3936430074,3572445317,  76029189,3654602809,3873151461, 530742520,3299628645,
  4096336452,1126891415,2878612391,4237533241,1700485571,2399980690,4293915773,2240044497,
  1873313359,4264355552,2734768916,1309151649,4149444226,3174756917, 718787259,3951481745};

 static const byte ss[16]={7,12,17,22, 5,9,14,20, 4,11,16,23, 6,10,15,21};	// Schiebeweiten
 static const byte yis[4]={0,1,5,0}, yss[4]={1,5,3,7};	// Y-Indizes und Schrittweite
 static const ABCD::FU fus[4]={&ABCD::F,&ABCD::G,&ABCD::H,&ABCD::I};	// Memberfunktionen
 ABCD::FU fu;
 const byte*ps;
 size_t i, yi, ys;
 for (i=0; i<64; i++) {
  if (!(i&15)) {	// Alle 16 Iterationen (4×) neue Werte setzen
   fu=fus[i>>4];
   ps=ss+(i>>4<<2);
   yi=yis[i>>4];
   ys=yss[i>>4];
  }
  abcd(abcd.b + std::rotl(abcd.a + (abcd.*fu)() + Y[yi] + sintab[i], ps[i&3]));
  yi=(yi+ys)&15;
 }
 for (i=0; i<4; i++) X[i] += abcd[i];
}

/* The routine MD5Init initializes the message-digest context
   mdContext. All fields are set to zero.
 */
void Md5::init() {
 Sha::init();
 static const uvector<4>Xinit={
  0x67452301,0xefcdab89,0x98badcfe,0x10325476};
 X=Xinit;
}

Md5::Digest Md5::final() {
 padlen(false);
 return X;
}

bool Md5::selftest() {
 static const uvector<4>expect={
  0xd98c1dd4,0x04b2008f,0x980980e9,0x7e42f8ec};
 if (!(final()==expect)) return false;
 init();
 update("The quick brown fox jumps over the lazy dog.",44);
 static const uvector<4>ex2={
  0xc209d9e4,0x1cfbd090,0xadff68a0,0xd0cb22df};
 return final()==ex2;
}


String<8*4+1>ShaN<4>::Digest::toString() const{
 MAKEPRINTBUF(8*4+1)
 for (int i=0; i<sizeof b; i++) print("%02x",b[i]);
 return buf;
}

bool ShaN<4>::Digest::fromString(const char*s) {
 for (int i=0; i<sizeof b; i++) {
  byte hi=digit(*s++),
       lo=digit(*s++);
  if (hi>=16 || lo>=16) return false;
  b[i]=hi<<4|lo;
 }
 return true;
}
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded