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