public Digest() { A=(BitVector) MD5InitializerConstant.A; B=(BitVector) MD5InitializerConstant.B; C=(BitVector) MD5InitializerConstant.C; D=(BitVector) MD5InitializerConstant.D; }
public static void Main() { var bitVector = new BitVector(new Bit[8] { new Bit("a0"), new Bit("a1"), new Bit("a2"), Bit.False, Bit.False, Bit.False, Bit.False, Bit.False }); var md = new Md5 {ByteInput = new[] {bitVector}}; var result = md.FingerPrint; Console.WriteLine(result); }
public static BitVector operator ~(BitVector a) { var result = new BitVector(a._bits.Select(x=>~x).ToArray()); return result; }
public static BitVector operator ^(BitVector a, BitVector b) { var result = new BitVector(a._bits.Zip(b._bits, (a1, b1) => a1 ^ b1).ToArray()); return result; }
/// <summary> /// perform transformatio using i (c^(b|~d)) /// </summary> protected void TransI(ref BitVector a, BitVector b, BitVector c, BitVector d, uint k, ushort s, uint i) { a = b + (a + (c ^ (b | ~d)) + X[k] + (BitVector)T[i - 1]).RotateLeft(s); }
/// <summary> /// perform transformatio using g((b&d) | (c & ~d) ) /// </summary> protected void TransG(ref BitVector a, BitVector b, BitVector c, BitVector d, int k, int s, int i) { a = b + (a + ((b & d) | (c & ~d)) + X[k] + (BitVector)T[i - 1]).RotateLeft(s); }
/******************************************************** * TRANSFORMATIONS : FF , GG , HH , II acc to RFC 1321 * where each Each letter represnets the aux function used *********************************************************/ /// <summary> /// perform transformatio using f(((b&c) | (~(b)&d)) /// </summary> protected void TransF(ref BitVector a, BitVector b, BitVector c, BitVector d, int k, int s, int i) { a = b + (a + ((b & c) | (~(b) & d)) + X[k] + (BitVector)T[i - 1]).RotateLeft(s); Console.WriteLine(a); }
/// <summary> /// Perform All the transformation on the data /// </summary> /// <param name="A">A</param> /// <param name="B">B </param> /// <param name="C">C</param> /// <param name="D">D</param> protected void PerformTransformation(ref BitVector A, ref BitVector B, ref BitVector C, ref BitVector D) { //// saving ABCD to be used in end of loop BitVector AA,BB,CC,DD; AA=A; BB=B; CC=C; DD=D; /* Round 1 * [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4] * [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8] * [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12] * [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16] * * */ TransF(ref A,B,C,D,0,7,1);TransF(ref D,A,B,C,1,12,2);TransF(ref C,D,A,B,2,17,3);TransF(ref B,C,D,A,3,22,4); TransF(ref A, B, C, D, 4, 7, 5); TransF(ref D, A, B, C, 5, 12, 6); TransF(ref C, D, A, B, 6, 17, 7); TransF(ref B, C, D, A, 7, 22, 8); TransF(ref A, B, C, D, 8, 7, 9); TransF(ref D, A, B, C, 9, 12, 10); TransF(ref C, D, A, B, 10, 17, 11); TransF(ref B, C, D, A, 11, 22, 12); TransF(ref A, B, C, D, 12, 7, 13); TransF(ref D, A, B, C, 13, 12, 14); TransF(ref C, D, A, B, 14, 17, 15); TransF(ref B, C, D, A, 15, 22, 16); /** rOUND 2 **[ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20] *[ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24] *[ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28] *[ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32] */ TransG(ref A,B,C,D,1,5,17);TransG(ref D,A,B,C,6,9,18);TransG(ref C,D,A,B,11,14,19);TransG(ref B,C,D,A,0,20,20); TransG(ref A,B,C,D,5,5,21);TransG(ref D,A,B,C,10,9,22);TransG(ref C,D,A,B,15,14,23);TransG(ref B,C,D,A,4,20,24); TransG(ref A,B,C,D,9,5,25);TransG(ref D,A,B,C,14,9,26);TransG(ref C,D,A,B,3,14,27);TransG(ref B,C,D,A,8,20,28); TransG(ref A,B,C,D,13,5,29);TransG(ref D,A,B,C,2,9,30);TransG(ref C,D,A,B,7,14,31);TransG(ref B,C,D,A,12,20,32); /* rOUND 3 * [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36] * [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40] * [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44] * [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48] * */ TransH(ref A,B,C,D,5,4,33);TransH(ref D,A,B,C,8,11,34);TransH(ref C,D,A,B,11,16,35);TransH(ref B,C,D,A,14,23,36); TransH(ref A,B,C,D,1,4,37);TransH(ref D,A,B,C,4,11,38);TransH(ref C,D,A,B,7,16,39);TransH(ref B,C,D,A,10,23,40); TransH(ref A,B,C,D,13,4,41);TransH(ref D,A,B,C,0,11,42);TransH(ref C,D,A,B,3,16,43);TransH(ref B,C,D,A,6,23,44); TransH(ref A,B,C,D,9,4,45);TransH(ref D,A,B,C,12,11,46);TransH(ref C,D,A,B,15,16,47);TransH(ref B,C,D,A,2,23,48); /*ORUNF 4 *[ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52] *[ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56] *[ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60] *[ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64] * */ TransI(ref A,B,C,D,0,6,49);TransI(ref D,A,B,C,7,10,50);TransI(ref C,D,A,B,14,15,51);TransI(ref B,C,D,A,5,21,52); TransI(ref A,B,C,D,12,6,53);TransI(ref D,A,B,C,3,10,54);TransI(ref C,D,A,B,10,15,55);TransI(ref B,C,D,A,1,21,56); TransI(ref A,B,C,D,8,6,57);TransI(ref D,A,B,C,15,10,58);TransI(ref C,D,A,B,6,15,59);TransI(ref B,C,D,A,13,21,60); TransI(ref A,B,C,D,4,6,61);TransI(ref D,A,B,C,11,10,62);TransI(ref C,D,A,B,2,15,63);TransI(ref B,C,D,A,9,21,64); A=A+AA; B=B+BB; C=C+CC; D=D+DD; }
/// <summary> /// Create Padded buffer for processing , buffer is padded with 0 along /// with the size in the end /// </summary> /// <returns>the padded buffer as byte array</returns> protected BitVector[] CreatePaddedBuffer() { uint pad; //no of padding bits for 448 mod 512 BitVector [] bMsg; //buffer to hold bits ulong sizeMsg; //64 bit size pad uint sizeMsgBuff; //buffer size in multiple of bytes int temp=(448-((ByteInput.Length*8)%512)); //temporary pad = (uint )((temp+512)%512); //getting no of bits to be pad if (pad==0) ///pad is in bits pad=512; //at least 1 or max 512 can be added sizeMsgBuff= (uint) ((ByteInput.Length)+ (pad/8)+8); sizeMsg=(ulong)ByteInput.Length*8; bMsg = new BitVector[sizeMsgBuff]; ///no need to pad with 0 coz new bytes for (int i = 0; i < sizeMsgBuff; i++) { bMsg[i] = (BitVector)((byte)0); } // are already initialize to 0 :) ////copying string to buffer for (int i =0; i<ByteInput.Length;i++) bMsg[i] = (BitVector)(ByteInput[i]); bMsg[ByteInput.Length] |= (BitVector)(byte)0x80; ///making first bit of padding 1, //wrting the size value for (int i =8; i >0;i--) bMsg[sizeMsgBuff - i] = (BitVector)(byte)(sizeMsg >> ((8 - i) * 8) & 0x00000000000000ff); return bMsg; }
/// <summary> /// Copies a 512 bit block into X as 16 32 bit words /// </summary> /// <param name="bMsg"> source buffer</param> /// <param name="block">no of block to copy starting from 0</param> protected void CopyBlock(BitVector[] bMsg,uint block) { block=block<<6; for (uint j=0; j<61;j+=4) { var b1 = bMsg[block + (j + 3)].Resize(32); var b2 = bMsg[block + (j + 2)].Resize(32); var b3 = bMsg[block + (j + 1)].Resize(32); var b4 = bMsg[block + (j)].Resize(32); X[j >> 2] = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; } }