Esempio n. 1
0
 public static string Sum(SrvsFile m)
 {
     if (!srvmd5memo.ContainsKey(m.Name))
     {
         Sum(m.Request);
         Sum(m.Response);
         string hashablereq = PrepareToHash(m.Request);
         string hashableres = PrepareToHash(m.Response);
         if (hashablereq == null || hashableres == null)
         {
             return(null);
         }
         byte[]        req = Encoding.ASCII.GetBytes(hashablereq);
         byte[]        res = Encoding.ASCII.GetBytes(hashableres);
         StringBuilder sb  = new StringBuilder();
         System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
         md5.TransformBlock(req, 0, req.Length, req, 0);
         md5.TransformFinalBlock(res, 0, res.Length);
         for (int i = 0; i < md5.Hash.Length; i++)
         {
             sb.AppendFormat("{0:x2}", md5.Hash[i]);
         }
         srvmd5memo.Add(m.Name, sb.ToString());
     }
     return(srvmd5memo[m.Name]);
 }
Esempio n. 2
0
        public override ClientWardenModule GetModuleForClient()
        {
            ClientWardenModule mod = new ClientWardenModule();

            uint length = (uint)WardenModuleWin.Module.Length;

            // data assign
            mod.CompressedSize = length;
            mod.CompressedData = WardenModuleWin.Module;
            mod.Key            = WardenModuleWin.ModuleKey;

            // md5 hash
            System.Security.Cryptography.MD5 ctx = System.Security.Cryptography.MD5.Create();
            ctx.Initialize();
            ctx.TransformBlock(mod.CompressedData, 0, mod.CompressedData.Length, mod.CompressedData, 0);
            ctx.TransformBlock(mod.Id, 0, mod.Id.Length, mod.Id, 0);

            return(mod);
        }
Esempio n. 3
0
File: MD5.cs Progetto: uustory/u-mpq
        public static string CalculateMD5(Stream stream, int bufferSize)
        {
            byte[] _emptyBuffer = new byte[0];
            System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
            byte[] buffer = new byte[bufferSize];
            int    readBytes;

            while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0)
            {
                md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
            }
            md5Hasher.TransformFinalBlock(_emptyBuffer, 0, 0);
            return(CUtils.ToHexString(md5Hasher.Hash));
        }
Esempio n. 4
0
        private void UpdateHash(byte[] buffer, int offset, int count)
        {
            if (m_finalHash != null)
            {
                throw new Exception("Cannot read/write after hash is read");
            }

            //If we have a fragment from the last block, fill up
            if (m_hashbufferLength > 0 && count + m_hashbufferLength > m_hashbuffer.Length)
            {
                int bytesToUse = m_hashbuffer.Length - m_hashbufferLength;
                Array.Copy(buffer, m_hashbuffer, bytesToUse);
                m_hash.TransformBlock(m_hashbuffer, 0, m_hashbuffer.Length, m_hashbuffer, 0);
                count             -= bytesToUse;
                offset            += bytesToUse;
                m_hashbufferLength = 0;
            }

            //Take full blocks directly
            int fullBlocks = count / m_hashbuffer.Length;

            if (fullBlocks > 0)
            {
                int bytesToUse = fullBlocks * m_hashbuffer.Length;
                m_hash.TransformBlock(buffer, offset, bytesToUse, buffer, offset);
                count  -= bytesToUse;
                offset += bytesToUse;
            }

            //Keep trailing bytes
            if (count > 0)
            {
                Array.Copy(buffer, offset, m_hashbuffer, 0, count);
                m_hashbufferLength = count;
            }
        }
Esempio n. 5
0
        public ImagePixelLock(Bitmap inSource, System.Drawing.Rectangle inLockRegion, Boolean inCreateCopy)
        {
            if (inSource.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            {
                throw new ArgumentException("Given bitmap has an unsupported pixel format.");
            }

            IsCopy = inCreateCopy;

            if (inCreateCopy)
            {
                bitmap = (Bitmap)inSource.Clone();
            }
            else
            {
                bitmap = inSource;
            }

            data   = bitmap.LockBits(inLockRegion, ImageLockMode.ReadWrite, inSource.PixelFormat);
            Pixels = (int *)data.Scan0.ToPointer();

            // compute checksum from pixeldata
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            int *ptr = (int *)data.Scan0.ToPointer();

            for (int i = 0, byteCount = Width * Height * 4; i < byteCount; i += buffer.Length)
            {
                int count = Math.Min(buffer.Length, byteCount - i);

                System.Runtime.InteropServices.Marshal.Copy((IntPtr)ptr, buffer, 0, count);
                md5.TransformBlock(buffer, 0, count, tmpBuffer, 0);

                ptr += count / 4;
            }

            md5.TransformFinalBlock(new byte[0], 0, 0);

            byte[] checksum = md5.Hash;

            for (int i = 0; i < 8; i++)
            {
                Checksum |= (((Int64)checksum[i]) << (i * 8));
            }
        }
Esempio n. 6
0
        public void UpdateImage(string inPath)
        {
            if (!System.IO.File.Exists(inPath) || !inPath.EndsWith(".png"))
            {
                return;
            }

            Texture2D Tex2D;

            byte[] FileData;

            FileData = System.IO.File.ReadAllBytes(inPath);
            Tex2D    = new Texture2D(2, 2); // Create new "empty" texture
            if (!Tex2D.LoadImage(FileData)) // Load the imagedata into the texture (size is set automatically)
            {
                return;                     // If data = readable -> return texture
            }
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();

            byte[] pathBytes = System.Text.Encoding.UTF8.GetBytes(inPath);

            md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);

            byte[] contentBytes = System.IO.File.ReadAllBytes(inPath);

            md5.TransformFinalBlock(contentBytes, 0, contentBytes.Length);


            myTextureData = new TextureData(Tex2D, inPath, System.BitConverter.ToString(md5.Hash).Replace("-", "").ToLower());

            float ratio = myTextureData.myTexture.width / myTextureData.myTexture.height;

            if (80 / 60 > ratio)
            {
                myImg.rectTransform.sizeDelta = new Vector2(30 * ratio, 30);
            }
            else
            {
                myImg.rectTransform.sizeDelta = new Vector2(40, 40 / ratio);
            }

            ImgNameText.text = System.IO.Path.GetFileName(inPath);
            myImg.sprite     = Sprite.Create(myTextureData.myTexture, new Rect(0, 0, myTextureData.myTexture.width, myTextureData.myTexture.height), new Vector2(0.5f, 0.5f));
        }
Esempio n. 7
0
        public static string Sum(params byte[][] data)
        {
            StringBuilder sb = new StringBuilder();

            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            if (data.Length > 0)
            {
                for (int i = 0; i < data.Length - 1; i++)
                {
                    md5.TransformBlock(data[i], 0, data[i].Length, data[0], 0);
                }
                md5.TransformFinalBlock(data[data.Length - 1], 0, data[data.Length - 1].Length);
            }
            for (int i = 0; i < md5.Hash.Length; i++)
            {
                sb.AppendFormat("{0:x2}", md5.Hash[i]);
            }
            return(sb.ToString());
        }