public static Md5Hash Calculate(IEnumerable <byte> bytes)
            {
                var context = new MD5Context();

                context.Initialize();
                var list = new List <byte>(bufferSize);

                using (var enumerator = bytes.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        list.Add(enumerator.Current);
                        if (list.Count == bufferSize)
                        {
                            context.Update(list.ToArray());
                            list = new List <byte>(bufferSize);
                        }
                    }
                    if (list.Count > 0)
                    {
                        context.Update(list.ToArray());
                    }
                }
                context.Final();
                return(context.ToMd5Hash());
            }
Example #2
0
        /*
        ** During testing, the special md5sum() aggregate function is available.
        ** inside SQLite.  The following routines implement that function.
        */
        static void md5step(sqlite3_context context, int argc, sqlite3_value[] argv)
        {
            MD5Context p = null;
            int        i;

            if (argc < 1)
            {
                return;
            }
            Mem pMem = sqlite3_aggregate_context(context, 1);//sizeof(*p));

            if (pMem._MD5Context == null)
            {
                pMem._MD5Context = new MD5Context();
                ((MD5Context)pMem._MD5Context)._Mem = pMem;
            }
            p = (MD5Context)pMem._MD5Context;
            if (p == null)
            {
                return;
            }
            if (!p.isInit)
            {
                MD5Init(p);
            }
            for (i = 0; i < argc; i++)
            {
                byte[] zData = sqlite3_value_text(argv[i]) == null ? null : Encoding.UTF8.GetBytes(sqlite3_value_text(argv[i]));
                if (zData != null)
                {
                    MD5Update(p, zData, zData.Length);
                }
            }
        }
            public static Md5Hash Calculate(Stream stream)
            {
                var context = new MD5Context();

                context.Initialize();
                var  size         = stream.Length;
                var  savePosition = stream.Position;
                long totalBytes   = 0;

                byte[] buffer = new byte[bufferSize];
                int    readBytes;

                try {
                    stream.Seek(0, SeekOrigin.Begin);
                    do
                    {
                        readBytes   = stream.Read(buffer, 0, bufferSize);
                        totalBytes += readBytes;
                        context.Update(buffer, readBytes);
                    }while(readBytes > 0 && totalBytes < size);
                } finally {
                    try {
                        stream.Seek(savePosition, SeekOrigin.Begin);
                    } catch {
                    }
                }
                context.Final();
                return(context.ToMd5Hash());
            }
Example #4
0
        // Update given Context to include Length bytes of Input
        public unsafe static void MD5Update(ref MD5Context Context, byte *Input, UInt32 Length)
        {
            UInt32 Index;
            UInt32 PartLen;
            UInt32 I;

            Index             = (Context.Count[0] >> 3) & 0x3f;
            Context.Count[0] += Length << 3;
            if (Context.Count[0] < (Length << 3))
            {
                Context.Count[1]++;
            }
            Context.Count[1] += Length >> 29;
            PartLen           = 64 - Index;
            if (Length >= PartLen)
            {
                CopyMemory(Context.Buffer.GetPointer(Index), (IntPtr)Input, (UIntPtr)PartLen);
                Transform(Context.Buffer.GetPointer(), ref Context.State);
                I = PartLen;
                while (I + 63 < Length)
                {
                    Transform((IntPtr)(Input + I), ref Context.State);
                    I += 64;
                }
                Index = 0;
            }
            else
            {
                I = 0;
            }
            CopyMemory(Context.Buffer.GetPointer(Index), (IntPtr)(&Input[I]), (UIntPtr)(Length - I));
        }
            public static Md5Hash Calculate(byte[] data)
            {
                var context = new MD5Context();

                context.Initialize();
                context.Update(data);
                context.Final();
                return(context.ToMd5Hash());
            }
Example #6
0
 /*
  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
  * initialization constants.
  */
 static void MD5Init(MD5Context ctx)
 {
     ctx.isInit  = true;
     ctx.buf[0]  = 0x67452301;
     ctx.buf[1]  = 0xefcdab89;
     ctx.buf[2]  = 0x98badcfe;
     ctx.buf[3]  = 0x10325476;
     ctx.bits[0] = 0;
     ctx.bits[1] = 0;
 }
Example #7
0
 // Initialize given Context
 public static void MD5Init(ref MD5Context Context)
 {
     Context.State[0] = 0x67452301;
     Context.State[1] = 0xefcdab89;
     Context.State[2] = 0x98badcfe;
     Context.State[3] = 0x10325476;
     Context.Count[0] = 0;
     Context.Count[1] = 0;
     Context.Buffer   = new MD5Buffer();
 }
Example #8
0
        /*
         * Update context to reflect the concatenation of another buffer full
         * of bytes.
         */
        static void MD5Update(MD5Context pCtx, byte[] buf, int len)
        {
            MD5Context ctx = (MD5Context)pCtx;
            int        t;

            /* Update bitcount */

            t = (int)ctx.bits[0];
            if ((ctx.bits[0] = (u32)(t + ((u32)len << 3))) < t)
            {
                ctx.bits[1]++; /* Carry from low to high */
            }
            ctx.bits[1] += (u32)(len >> 29);

            t = (t >> 3) & 0x3f; /* Bytes already in shsInfo.data */

            /* Handle any leading odd-sized chunks */

            int _buf = 0; // Offset into buffer
            int p    = t; //Offset into ctx._in

            if (t != 0)
            {
                //byte p = (byte)ctx._in + t;
                t = 64 - t;
                if (len < t)
                {
                    Buffer.BlockCopy(buf, _buf, ctx._in, p, len);// memcpy( p, buf, len );
                    return;
                }
                Buffer.BlockCopy(buf, _buf, ctx._in, p, t); //memcpy( p, buf, t );
                //byteReverse(ctx._in, 16);
                MD5Transform(ctx.buf, ctx._in);
                _buf += t;// buf += t;
                len  -= t;
            }

            /* Process data in 64-byte chunks */

            while (len >= 64)
            {
                Buffer.BlockCopy(buf, _buf, ctx._in, 0, 64);//memcpy( ctx._in, buf, 64 );
                //byteReverse(ctx._in, 16);
                MD5Transform(ctx.buf, ctx._in);
                _buf += 64;// buf += 64;
                len  -= 64;
            }

            /* Handle any remaining bytes of data. */

            Buffer.BlockCopy(buf, _buf, ctx._in, 0, len); //memcpy( ctx._in, buf, len );
        }
Example #9
0
        /*
         * Final wrapup - pad to 64-byte boundary with the bit pattern
         * 1 0* (64-bit count of bits processed, MSB-first)
         */

        static void MD5Final(byte[] digest, MD5Context pCtx)
        {
            MD5Context ctx = pCtx;
            int        count;
            int        p;

            /* Compute number of bytes mod 64 */
            count = (int)(ctx.bits[0] >> 3) & 0x3F;

            /* Set the first char of padding to 0x80.  This is safe since there is
             * always at least one byte free */
            p            = count;
            ctx._in[p++] = 0x80;

            /* Bytes of padding needed to make 64 bytes */
            count = 64 - 1 - count;

            /* Pad out to 56 mod 64 */
            if (count < 8)
            {
                /* Two lots of padding:  Pad the first block to 64 bytes */
                Array.Clear(ctx._in, p, count);//memset(p, 0, count);
                //byteReverse( ctx._in, 16 );
                MD5Transform(ctx.buf, ctx._in);

                /* Now fill the next block with 56 bytes */
                Array.Clear(ctx._in, 0, 56);//memset(ctx._in, 0, 56);
            }
            else
            {
                /* Pad block to 56 bytes */
                Array.Clear(ctx._in, p, count - 8);//memset(p, 0, count-8);
            }
            //byteReverse( ctx._in, 14 );

            /* Append length in bits and transform */
            ctx._in[14] = (byte)ctx.bits[0];
            ctx._in[15] = (byte)ctx.bits[1];

            MD5Transform(ctx.buf, ctx._in);
            //byteReverse( ctx.buf, 4 );
            Buffer.BlockCopy(ctx.buf, 0, digest, 0, 16);//memcpy(digest, ctx.buf, 16);
            //memset(ctx, 0, sizeof(ctx));    /* In case it is sensitive */
            Array.Clear(ctx._in, 0, ctx._in.Length);
            Array.Clear(ctx.bits, 0, ctx.bits.Length);
            Array.Clear(ctx.buf, 0, ctx.buf.Length);
            ctx._Mem = null;
        }
Example #10
0
        /*
        ** A TCL command for md5.  The argument is the text to be hashed.  The
        ** Result is the hash in base64.
        */
        static int md5_cmd(object cd, Tcl_Interp interp, int argc, Tcl_Obj[] argv)
        {
            MD5Context ctx = new MD5Context();

            byte[] digest = new byte[16];
            byte[] zBuf   = new byte[32];


            if (argc != 2)
            {
                TCL.Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
                                     " TEXT\"");
                return(TCL.TCL_ERROR);
            }
            MD5Init(ctx);
            MD5Update(ctx, Encoding.UTF8.GetBytes(argv[1].ToString()), Encoding.UTF8.GetByteCount(argv[1].ToString()));
            MD5Final(digest, ctx);
            DigestToBase16(digest, zBuf);
            TCL.Tcl_AppendResult(interp, Encoding.UTF8.GetString(zBuf));
            return(TCL.TCL_OK);
        }
Example #11
0
        // Finalize given Context, create Digest and zeroize Context
        public unsafe static void MD5Final(ref MD5Context Context, out MD5Digest Digest)
        {
            MD5CBits Bits;
            UInt32   Index;
            UInt32   PadLen;

            Decode(Context.Count.GetPointer(), Bits.GetPointer(), 2);
            Index = (Context.Count[0] >> 3) & 0x3f;
            if (Index < 56)
            {
                PadLen = 56 - Index;
            }
            else
            {
                PadLen = 120 - Index;
            }
            MD5Update(ref Context, (byte *)PADDING.GetPointer(), PadLen);
            MD5Update(ref Context, (byte *)Bits.GetPointer(), 8);
            Decode(Context.State.GetPointer(), Digest.GetPointer(), 4);
            Context = new MD5Context();
        }
Example #12
0
    /*
    ** A TCL command for md5.  The argument is the text to be hashed.  The
    ** Result is the hash in base64.
    */
    static int md5_cmd( object cd, Tcl_Interp interp, int argc, Tcl_Obj[] argv )
    {
      MD5Context ctx = new MD5Context();
      byte[] digest = new byte[16];
      byte[] zBuf = new byte[32];


      if ( argc != 2 )
      {
        TCL.Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
        " TEXT\"" );
        return TCL.TCL_ERROR;
      }
      MD5Init( ctx );
      MD5Update( ctx, Encoding.UTF8.GetBytes( argv[1].ToString() ), Encoding.UTF8.GetByteCount( argv[1].ToString() ) );
      MD5Final( digest, ctx );
      DigestToBase16( digest, zBuf );
      TCL.Tcl_AppendResult( interp, Encoding.UTF8.GetString( zBuf ) );
      return TCL.TCL_OK;
    }
Example #13
0
    /*
    * Final wrapup - pad to 64-byte boundary with the bit pattern
    * 1 0* (64-bit count of bits processed, MSB-first)
    */

    static void MD5Final( byte[] digest, MD5Context pCtx )
    {
      MD5Context ctx = pCtx;
      int count;
      int p;

      /* Compute number of bytes mod 64 */
      count = (int)( ctx.bits[0] >> 3 ) & 0x3F;

      /* Set the first char of padding to 0x80.  This is safe since there is
      always at least one byte free */
      p = count;
      ctx._in[p++] = 0x80;

      /* Bytes of padding needed to make 64 bytes */
      count = 64 - 1 - count;

      /* Pad out to 56 mod 64 */
      if ( count < 8 )
      {
        /* Two lots of padding:  Pad the first block to 64 bytes */
        Array.Clear( ctx._in, p, count );//memset(p, 0, count);
        //byteReverse( ctx._in, 16 );
        MD5Transform( ctx.buf, ctx._in );

        /* Now fill the next block with 56 bytes */
        Array.Clear( ctx._in, 0, 56 );//memset(ctx._in, 0, 56);
      }
      else
      {
        /* Pad block to 56 bytes */
        Array.Clear( ctx._in, p, count - 8 );//memset(p, 0, count-8);
      }
      //byteReverse( ctx._in, 14 );

      /* Append length in bits and transform */
      ctx._in[14] = (byte)ctx.bits[0];
      ctx._in[15] = (byte)ctx.bits[1];

      MD5Transform( ctx.buf, ctx._in );
      //byteReverse( ctx.buf, 4 );
      Buffer.BlockCopy( ctx.buf, 0, digest, 0, 16 );//memcpy(digest, ctx.buf, 16);
      //memset(ctx, 0, sizeof(ctx));    /* In case it is sensitive */
      Array.Clear( ctx._in, 0, ctx._in.Length );
      Array.Clear( ctx.bits, 0, ctx.bits.Length );
      Array.Clear( ctx.buf, 0, ctx.buf.Length );
      ctx._Mem = null;
    }
Example #14
0
    /*
    * Update context to reflect the concatenation of another buffer full
    * of bytes.
    */
    static void MD5Update( MD5Context pCtx, byte[] buf, int len )
    {

      MD5Context ctx = (MD5Context)pCtx;
      int t;

      /* Update bitcount */

      t = (int)ctx.bits[0];
      if ( ( ctx.bits[0] = (u32)( t + ( (u32)len << 3 ) ) ) < t )
        ctx.bits[1]++; /* Carry from low to high */
      ctx.bits[1] += (u32)( len >> 29 );

      t = ( t >> 3 ) & 0x3f;    /* Bytes already in shsInfo.data */

      /* Handle any leading odd-sized chunks */

      int _buf = 0; // Offset into buffer
      int p = t; //Offset into ctx._in
      if ( t != 0 )
      {
        //byte p = (byte)ctx._in + t;
        t = 64 - t;
        if ( len < t )
        {
          Buffer.BlockCopy( buf, _buf, ctx._in, p, len );// memcpy( p, buf, len );
          return;
        }
        Buffer.BlockCopy( buf, _buf, ctx._in, p, t ); //memcpy( p, buf, t );
        //byteReverse(ctx._in, 16);
        MD5Transform( ctx.buf, ctx._in );
        _buf += t;// buf += t;
        len -= t;
      }

      /* Process data in 64-byte chunks */

      while ( len >= 64 )
      {
        Buffer.BlockCopy( buf, _buf, ctx._in, 0, 64 );//memcpy( ctx._in, buf, 64 );
        //byteReverse(ctx._in, 16);
        MD5Transform( ctx.buf, ctx._in );
        _buf += 64;// buf += 64;
        len -= 64;
      }

      /* Handle any remaining bytes of data. */

      Buffer.BlockCopy( buf, _buf, ctx._in, 0, len ); //memcpy( ctx._in, buf, len );
    }
Example #15
0
 /*
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 * initialization constants.
 */
 static void MD5Init( MD5Context ctx )
 {
   ctx.isInit = true;
   ctx.buf[0] = 0x67452301;
   ctx.buf[1] = 0xefcdab89;
   ctx.buf[2] = 0x98badcfe;
   ctx.buf[3] = 0x10325476;
   ctx.bits[0] = 0;
   ctx.bits[1] = 0;
 }