SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
md5.h
Go to the documentation of this file.
1 #include "bd.h"
2 
4 // MD5
6 private:
7  // first, some types:
8  typedef TB4Def::TB4 uint4; // assumes 4 byte long
9  typedef TB2Def::TB2 uint2; // assumes 2 byte long
10  typedef TB1Def::TB1 uint1; // assumes 1 byte long
11 
12  // next, the private data:
13  uint4 state[4];
14  uint4 count[2]; // number of *bits*, mod 2^64
15  uint1 buffer[64]; // input buffer
16  uint1 Sig[16];
17  bool DefP;
18 
19  // last, the private methods, mostly static:
20  void Init(); // called by all constructors
21  void Transform(uint1* buffer); // does the real update work.
22  static void Encode(uint1* Dst, uint4* Src, uint4 Len);
23  static void Decode(uint4* Dst, uint1* Src, uint4 Len);
24  static void MemCpy(uint1* Dst, uint1* Src, uint4 Len){
25  for (uint4 ChN=0; ChN<Len; ChN++){Dst[ChN]=Src[ChN];}}
26  static void MemSet(uint1* Start, uint1 Val, uint4 Len){
27  for (uint4 ChN=0; ChN<Len; ChN++){Start[ChN]=Val;}}
28 
29  // RotateLeft rotates x left n bits.
30  static uint4 RotateLeft(uint4 x, uint4 n){return (x<<n)|(x>>(32-n));}
31  // F, G, H and I are basic MD5 functions.
32  static uint4 F(uint4 x, uint4 y, uint4 z){return (x&y)|(~x&z);}
33  static uint4 G(uint4 x, uint4 y, uint4 z){return (x&z)|(y&~z);}
34  static uint4 H(uint4 x, uint4 y, uint4 z){return x^y^z;}
35  static uint4 I(uint4 x, uint4 y, uint4 z){return y^(x|~z);}
36  // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
37  // Rotation is separate from addition to prevent recomputation.
38  static void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
39  a+=F(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
40  static void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
41  a+=G(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
42  static void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
43  a+=H(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
44  static void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac){
45  a+=I(b, c, d)+x+ac; a=RotateLeft(a, s)+b;}
47 public:
48  TMd5(){Init();}
49  static PMd5 New(){return PMd5(new TMd5());}
50  TMd5(const PSIn& SIn){Init(); Add(SIn); Def();}
51  static PMd5 New(const PSIn& SIn){return PMd5(new TMd5(SIn));}
53  static PMd5 Load(TSIn& SIn){return new TMd5(SIn);}
54  void Save(TSOut&){Fail;}
55 
56  // adding data & defining digest
57  void Add(uchar* InBf, const int& InBfL);
58  void Add(const PSIn& SIn);
59  void Def();
60 
61  // digest data retrieval
62  void GetSigMem(TMem& Mem) const;
63  TStr GetSigStr() const;
64 
65  // data package digest calculation
66  static TStr GetMd5SigStr(const PSIn& SIn){
67  PMd5 Md5=TMd5::New(SIn); return Md5->GetSigStr();}
68  static TStr GetMd5SigStr(const TStr& Str){
69  return GetMd5SigStr(TStrIn::New(Str));}
70  static TStr GetMd5SigStr(const TMem& Mem){
71  return GetMd5SigStr(TMemIn::New(Mem));}
72 
73  // testing correctnes
74  static bool Check();
75 
76  friend class TMd5Sig;
77 };
78 
80 // MD5-Signature
81 class TMd5Sig{
82 private:
83  typedef TB1Def::TB1 uint1; // assumes 1 byte long
84  uint1 CdT[16];
85 public:
86  TMd5Sig(){memset(CdT, 0, 16);}
87  TMd5Sig(const TMd5Sig& Md5Sig){memcpy(CdT, Md5Sig.CdT, 16);}
88  TMd5Sig(const PSIn& SIn);
89  TMd5Sig(const TStr& Str);
90  TMd5Sig(const TChA& ChA);
91  TMd5Sig(const char* CStr);
92  TMd5Sig(const TMem& Mem);
93  TMd5Sig(const TMd5& Md5){memcpy(CdT, Md5.Sig, 16);}
94  TMd5Sig(TSIn& SIn){SIn.LoadBf(CdT, 16);}
95  void Save(TSOut& SOut) const {SOut.SaveBf(CdT, 16);}
96 
97  TMd5Sig& operator=(const TMd5Sig& Md5Sig){
98  if (this!=&Md5Sig){memcpy(CdT, Md5Sig.CdT, 16);} return *this;}
99  bool operator==(const TMd5Sig& Md5Sig) const {
100  return memcmp(CdT, Md5Sig.CdT, 16)==0;}
101  bool operator<(const TMd5Sig& Md5Sig) const {
102  return memcmp(CdT, Md5Sig.CdT, 16)==-1;}
103  int operator[](const int& CdN) const {
104  Assert((0<=CdN)&&(CdN<16)); return CdT[CdN];}
105 
106  // hash codes
107  int GetPrimHashCd() const;
108  int GetSecHashCd() const;
109 
110  // alternative representations
111  TStr GetStr() const;
112  void GetUInt(const int& StartCd, uint& UInt) const {
113  memcpy(&UInt, CdT+StartCd, sizeof(uint));}
114 };
116 
TB1Def::TB1 uint1
Definition: md5.h:83
TStr GetStr() const
Definition: md5.cpp:278
TPt< TMd5 > PMd5
Definition: md5.h:5
void Save(TSOut &SOut) const
Definition: md5.h:95
Definition: bits.h:5
TMd5Sig(TSIn &SIn)
Definition: md5.h:94
Definition: bits.h:29
TB4Def::TB4 uint4
Definition: md5.h:8
static PSIn New(const TMem &Mem)
Definition: dt.h:165
static void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
Definition: md5.h:40
unsigned int uint
Definition: bd.h:11
#define Fail
Definition: bd.h:238
bool operator==(const TMd5Sig &Md5Sig) const
Definition: md5.h:99
static void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
Definition: md5.h:44
static PMd5 Load(TSIn &SIn)
Definition: md5.h:53
static TStr GetMd5SigStr(const TStr &Str)
Definition: md5.h:68
TMd5Sig()
Definition: md5.h:86
uint1 CdT[16]
Definition: md5.h:84
#define UndefCopyAssign(TNm)
Definition: bd.h:209
Definition: fl.h:58
Definition: md5.h:5
static PMd5 New()
Definition: md5.h:49
Definition: dt.h:77
#define ClassTP(TNm, PNm)
Definition: bd.h:126
TMd5()
Definition: md5.h:48
static PMd5 New(const PSIn &SIn)
Definition: md5.h:51
static uint4 I(uint4 x, uint4 y, uint4 z)
Definition: md5.h:35
static PSIn New(const TStr &Str)
Definition: dt.h:708
#define Assert(Cond)
Definition: bd.h:251
uint1 Sig[16]
Definition: md5.h:16
TVec< TMd5Sig > TMd5SigV
Definition: md5.h:115
TB1Def::TB1 uint1
Definition: md5.h:10
unsigned char uchar
Definition: bd.h:10
void SaveBf(const void *Bf, const TSize &BfL)
Definition: fl.h:172
Definition: fl.h:128
bool operator<(const TMd5Sig &Md5Sig) const
Definition: md5.h:101
TMd5(TSIn &)
Definition: md5.h:52
static uint4 H(uint4 x, uint4 y, uint4 z)
Definition: md5.h:34
TMd5Sig & operator=(const TMd5Sig &Md5Sig)
Definition: md5.h:97
Definition: dt.h:201
void GetUInt(const int &StartCd, uint &UInt) const
Definition: md5.h:112
static TStr GetMd5SigStr(const TMem &Mem)
Definition: md5.h:70
static uint4 G(uint4 x, uint4 y, uint4 z)
Definition: md5.h:33
uchar TB1
Definition: bits.h:7
Definition: dt.h:412
Definition: bits.h:51
TB2Def::TB2 uint2
Definition: md5.h:9
void LoadBf(const void *Bf, const TSize &BfL)
Definition: fl.h:81
static void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
Definition: md5.h:38
Definition: bd.h:196
static uint4 F(uint4 x, uint4 y, uint4 z)
Definition: md5.h:32
TMd5Sig(const TMd5 &Md5)
Definition: md5.h:93
TMd5Sig(const TMd5Sig &Md5Sig)
Definition: md5.h:87
Definition: md5.h:81
int operator[](const int &CdN) const
Definition: md5.h:103
static void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
Definition: md5.h:42
TMd5(const PSIn &SIn)
Definition: md5.h:50
void Save(TSOut &)
Definition: md5.h:54
static TStr GetMd5SigStr(const PSIn &SIn)
Definition: md5.h:66
int GetSecHashCd() const
Definition: md5.cpp:271
static void MemSet(uint1 *Start, uint1 Val, uint4 Len)
Definition: md5.h:26
int GetPrimHashCd() const
Definition: md5.cpp:264
static uint4 RotateLeft(uint4 x, uint4 n)
Definition: md5.h:30
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429