Beispiel #1
0
        // private methods...
        void CheckMd5ForString(string message, string digest)
        {
            var actualMd5   = Md5Hash.Calculate(message);
            var expectedMd5 = Md5Hash.Parse(digest);

            Assert.That(actualMd5, Is.EqualTo(expectedMd5), "Md5 hashes are not equlas!");
        }
Beispiel #2
0
        public void PerfTest()
        {
            int count = 1000;
            var list  = Array.CreateInstance(typeof(byte[]), count) as byte[][];//;//>(count);
            var hList = new Md5Hash[count];

            hList.Initialize();
            var random = new Random();

            for (int i = 0; i < list.Length; i++)
            {
                var bytes = new byte[5];
                random.NextBytes(bytes);
                list[i] = bytes;
            }
            var dateTime = DateTime.Now;

            for (int i = 0; i < hList.Length; i++)
            {
                var hash = Md5Hash.Calculate(list[i]);
                hList[i] = hash;
            }
            var seconds = (DateTime.Now - dateTime).TotalSeconds;

            Console.WriteLine(seconds);
            Console.WriteLine(Math.Round(hList.Length / seconds));
        }
Beispiel #3
0
        public void Md5HashCodeTest()
        {
            var set = new HashSet <Md5Hash>();

            for (int i = 0; i < int.MaxValue; i++)
            {
                Assert.IsTrue(set.Add(Md5Hash.Calculate(i.ToString())));
            }
        }
Beispiel #4
0
        public void CalcFor0()
        {
            var md5 = Md5Hash.Calculate(new byte[1] {
                0
            });
            string actual = md5.ToString();

            Assert.AreEqual("93b885adfe0da089cdf634904fd59f71", actual);
        }
Beispiel #5
0
 /// <summary>
 /// Calculates a md5 hash from a model
 /// </summary>
 public void UpdateHash()
 {
     using (var ms = new MemoryStream())
     {
         Hash = null;
         Serializer.Serialize(ms, this);
         ms.Position = 0;
         Hash        = Md5Hash.Calculate(ms);
     }
 }
Beispiel #6
0
        public void Md5ToByteArrayTest()
        {
            var    md5    = Md5Hash.Calculate(new byte[0]);
            string actual = md5.ToString();

            Assert.AreEqual("d41d8cd98f00b204e9800998ecf8427e", actual);
            var bytes     = md5.ToByteArray();
            var actualMd5 = new Md5Hash(bytes);

            Assert.AreEqual(md5, actualMd5);
        }
Beispiel #7
0
        public void Md5CompareWithNative()
        {
            const int count = 10000;
            var       bytes = new List <byte>(count);

            for (int i = 0; i < count; i++)
            {
                bytes.Add((byte)i);
                var md5       = Md5Hash.Calculate(bytes.ToArray());
                var nativeMd5 = Md5Hash.CalculateNative(bytes.ToArray());
                Assert.That(md5, Is.EqualTo(nativeMd5));
            }
        }
Beispiel #8
0
        public void CalcForEnumerable()
        {
            int count = 12345678;
            var list  = new List <byte>(count);

            for (int i = 0; i < count; i++)
            {
                list.Add((byte)i);
            }
            var    md5    = Md5Hash.Calculate(list);
            string actual = md5.ToString();

            Assert.AreEqual("c509d961f3bca4f02402c0e2798a1324", actual);
        }
        public string HashBasedWithErrorThresoldSelection(string fileToken, string[] fileSources)
        {
            var fileHash = Md5Hash.Calculate(fileToken);

            var sourcesByHash = fileSources.OrderBy(s => Md5Hash.Calculate(s) ^ fileHash).Select(s => s).ToList();

            for (int i = 0; i < sourcesByHash.Count - 1; i++)
            {
                if (GetErrorsPercent(sourcesByHash[i]) - ErrorThresold <= GetErrorsPercent(sourcesByHash[i + 1]))
                {
                    return(sourcesByHash[i]);
                }
            }

            return(sourcesByHash[sourcesByHash.Count - 1]);
        }
Beispiel #10
0
        public void CalcForFile()
        {
            var name = Path.GetTempFileName();
            var fs   = File.Open(name, FileMode.Append);
            var bw   = new BinaryWriter(fs);

            for (int i = 0; i < 12345678; i++)
            {
                bw.Write((byte)i);
            }
            fs.Dispose();
            fs = File.Open(name, FileMode.Open);
            var md5 = Md5Hash.Calculate(fs);

            fs.Dispose();
            File.Delete(name);
            string actual = md5.ToString();

            Assert.AreEqual("c509d961f3bca4f02402c0e2798a1324", actual);
        }
Beispiel #11
0
        private new void Update()
        {
            try
            {
                Status("Load update info...");
                // get update index
                var req = WebRequest.Create("http://update.utopiarealms.com/index.update");

                using (var response = req.GetResponse())
                    using (var stream = response.GetResponseStream())
                        using (var zs = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            var reader = new BinaryReader(zs);
                            _updateFile = new UpdateFile();
                            _updateFile.Load(reader);
                        }

                var indexPath = Path.Combine(_basePath, "update.index");

                UpdateFile previousUpdate = null;

                if (File.Exists(indexPath))
                {
                    using (var fs = File.OpenRead(indexPath))
                        using (var zs = new GZipStream(fs, CompressionMode.Decompress))
                        {
                            var reader = new BinaryReader(zs);
                            previousUpdate = new UpdateFile();
                            previousUpdate.Load(reader);
                        }
                }

                Status("Check files to update...");

                var files = new List <UpdateFileInfo>();

                foreach (var file in _updateFile.Files)
                {
                    var filePath = Path.Combine(_basePath, file.SystemPath);

                    if (File.Exists(filePath))
                    {
                        using (var fs = File.OpenRead(filePath))
                        {
                            var hash = Md5Hash.Calculate(fs);
                            if (hash.ToString() == file.Md5Hash && fs.Length == file.Length)
                            {
                                continue;
                            }
                        }
                    }

                    files.Add(file);
                }

                float index = 0f;
                foreach (var file in files)
                {
                    Status("Updating " + file.SystemPath, index / files.Count);
                    try
                    {
                        var filePath = Path.Combine(_basePath, file.SystemPath);
                        if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        }

                        var fileReq = WebRequest.Create(file.DownloadUri);

                        using (var response = fileReq.GetResponse())
                            using (var fs = File.OpenWrite(filePath))
                            {
                                fs.SetLength(0);
                                using (var stream = response.GetResponseStream())
                                {
                                    if (file.Compressed)
                                    {
                                        using (var zip = new GZipStream(stream, CompressionMode.Decompress))
                                        {
                                            zip.CopyTo(fs);
                                        }
                                    }
                                    else
                                    {
                                        stream.CopyTo(fs);
                                    }
                                }
                            }
                    }
                    catch (Exception x)
                    {
                        MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }

                    index++;
                }

                // delete removed files
                if (previousUpdate != null)
                {
                    foreach (var removedFile in _updateFile.GetRemovedFiles(previousUpdate))
                    {
                        var fullPath = Path.Combine(_basePath, removedFile);
                        if (File.Exists(fullPath))
                        {
                            File.Delete(fullPath);
                        }
                    }
                }

                if (File.Exists(indexPath))
                {
                    File.Delete(indexPath);
                }

                // save index for future update
                using (var fs = File.OpenWrite(indexPath))
                    using (var zs = new GZipStream(fs, CompressionMode.Compress))
                    {
                        var writer = new BinaryWriter(zs);
                        _updateFile.Save(writer);
                    }

                // save current token
                File.WriteAllText(Path.Combine(_basePath, "update.token"), _updateFile.UpdateToken);

                StartGame();
            }
            catch (Exception x)
            {
                MessageBox.Show(string.Format("Exception occured: {0}\nPlease post this information to the forum at http://utopiarealms.com", x.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #12
0
 /// <summary>
 /// Gets md5 hash of the chunk data (blocks and entities)
 /// </summary>
 /// <returns></returns>
 public Md5Hash GetMd5Hash()
 {
     return(Md5HashData ?? (Md5HashData = Md5Hash.Calculate(GetBytesForHash())));
 }