SNAP Library 2.1, Developer Reference  2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
md5.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00004 // MD5
00005 ClassTP(TMd5, PMd5)//{
00006 private:
00007   // first, some types:
00008   typedef TB4Def::TB4 uint4; // assumes 4 byte long
00009   typedef TB2Def::TB2 uint2; // assumes 2 byte long
00010   typedef TB1Def::TB1 uint1; // assumes 1 byte long
00011 
00012   // next, the private data:
00013   uint4 state[4];
00014   uint4 count[2]; // number of *bits*, mod 2^64
00015   uint1 buffer[64]; // input buffer
00016   uint1 Sig[16];
00017   bool DefP;
00018 
00019   // last, the private methods, mostly static:
00020   void Init(); // called by all constructors
00021   void Transform(uint1* buffer); // does the real update work.
00022   static void Encode(uint1* Dst, uint4* Src, uint4 Len);
00023   static void Decode(uint4* Dst, uint1* Src, uint4 Len);
00024   static void MemCpy(uint1* Dst, uint1* Src, uint4 Len){
00025     for (uint4 ChN=0; ChN<Len; ChN++){Dst[ChN]=Src[ChN];}}
00026   static void MemSet(uint1* Start, uint1 Val, uint4 Len){
00027     for (uint4 ChN=0; ChN<Len; ChN++){Start[ChN]=Val;}}
00028 
00029   // RotateLeft rotates x left n bits.
00030   static uint4 RotateLeft(uint4 x, uint4 n){return (x<<n)|(x>>(32-n));}
00031   // F, G, H and I are basic MD5 functions.
00032   static uint4 F(uint4 x, uint4 y, uint4 z){return (x&y)|(~x&z);}
00033   static uint4 G(uint4 x, uint4 y, uint4 z){return (x&z)|(y&~z);}
00034   static uint4 H(uint4 x, uint4 y, uint4 z){return x^y^z;}
00035   static uint4 I(uint4 x, uint4 y, uint4 z){return y^(x|~z);}
00036   // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
00037   // Rotation is separate from addition to prevent recomputation.
00038   static void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
00039     a+=F(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
00040   static void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
00041     a+=G(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
00042   static void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
00043     a+=H(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
00044   static void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
00045     a+=I(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
00046   UndefCopyAssign(TMd5);
00047 public:
00048   TMd5(){Init();}
00049   static PMd5 New(){return PMd5(new TMd5());}
00050   TMd5(const PSIn& SIn){Init(); Add(SIn); Def();}
00051   static PMd5 New(const PSIn& SIn){return PMd5(new TMd5(SIn));}
00052   TMd5(TSIn&){Fail;}
00053   static PMd5 Load(TSIn& SIn){return new TMd5(SIn);}
00054   void Save(TSOut&){Fail;}
00055 
00056   // adding data & defining digest
00057   void Add(uchar* InBf, const int& InBfL);
00058   void Add(const PSIn& SIn);
00059   void Def();
00060 
00061   // digest data retrieval
00062   void GetSigMem(TMem& Mem) const;
00063   TStr GetSigStr() const;
00064 
00065   // data package digest calculation
00066   static TStr GetMd5SigStr(const PSIn& SIn){
00067     PMd5 Md5=TMd5::New(SIn); return Md5->GetSigStr();}
00068   static TStr GetMd5SigStr(const TStr& Str){
00069     return GetMd5SigStr(TStrIn::New(Str));}
00070   static TStr GetMd5SigStr(const TMem& Mem){
00071     return GetMd5SigStr(TMemIn::New(Mem));}
00072 
00073   // testing correctnes
00074   static bool Check();
00075 
00076   friend class TMd5Sig;
00077 };
00078 
00080 // MD5-Signature
00081 class TMd5Sig{
00082 private:
00083   typedef TB1Def::TB1 uint1; // assumes 1 byte long
00084   uint1 CdT[16];
00085 public:
00086   TMd5Sig(){memset(CdT, 0, 16);}
00087   TMd5Sig(const TMd5Sig& Md5Sig){memcpy(CdT, Md5Sig.CdT, 16);}
00088   TMd5Sig(const PSIn& SIn);
00089   TMd5Sig(const TStr& Str);
00090   TMd5Sig(const TChA& ChA);
00091   TMd5Sig(const char* CStr);
00092   TMd5Sig(const TMem& Mem);
00093   TMd5Sig(const TMd5& Md5){memcpy(CdT, Md5.Sig, 16);}
00094   TMd5Sig(TSIn& SIn){SIn.LoadBf(CdT, 16);}
00095   void Save(TSOut& SOut) const {SOut.SaveBf(CdT, 16);}
00096 
00097   TMd5Sig& operator=(const TMd5Sig& Md5Sig){
00098     if (this!=&Md5Sig){memcpy(CdT, Md5Sig.CdT, 16);} return *this;}
00099   bool operator==(const TMd5Sig& Md5Sig) const {
00100     return memcmp(CdT, Md5Sig.CdT, 16)==0;}
00101   bool operator<(const TMd5Sig& Md5Sig) const {
00102     return memcmp(CdT, Md5Sig.CdT, 16)==-1;}
00103   int operator[](const int& CdN) const {
00104     Assert((0<=CdN)&&(CdN<16)); return CdT[CdN];}
00105 
00106   // hash codes
00107   int GetPrimHashCd() const;
00108   int GetSecHashCd() const;
00109 
00110   // alternative representations
00111   TStr GetStr() const;
00112   void GetUInt(const int& StartCd, uint& UInt) const {
00113     memcpy(&UInt, CdT+StartCd, sizeof(uint));}
00114 };
00115 typedef TVec<TMd5Sig> TMd5SigV;
00116