public static void GetHashList(UpdateProgressCallback callback, bool forceUpdate = false) { Thread t = new Thread(() => { LocalHashFile = Path.GetTempFileName(); using (WebClient hashFileDL = new WebClient()) { hashFileDL.DownloadProgressChanged += (sender, e) => { callback(e.ProgressPercentage, "Getting Hashlist..."); }; hashFileDL.DownloadFile(Settings.HashlistURL, LocalHashFile); if (Properties.Settings.Default.Hashlist == MD5Lib.GetHash(LocalHashFile) && !forceUpdate) { // Hashlist hasnt changed callback(100, "Your client is up-to-date", true); return; } } CheckFiles(callback); }); t.Start(); }
public static void CheckFiles(UpdateProgressCallback callback) { string line = null; string[] file; int lineCount = 0; int totalLines = TotalLines(LocalHashFile); int existingCount = 0; using (StreamReader hashReader = new StreamReader(LocalHashFile)) { while ((line = hashReader.ReadLine()) != null) { lineCount++; file = line.Split(new char[] { ':' }); if (file.Length != 2) { throw new FormatException("Hashlist got wrong format!"); } callback((int)((float)((float)lineCount / (float)totalLines) * 100), "Checking files..."); if (!File.Exists(file[0])) { DownloadManager.Add(file[0]); } else { existingCount++; if (MD5Lib.GetHash(file[0]) != file[1]) { DownloadManager.Add(file[0]); } } } } string Hashlist = MD5Lib.GetHash(LocalHashFile); DeleteLocalHashfile(); if (existingCount == 0) { string Message = String.Format("Would you like to install Cosmic Ascension to \"{0}\"?\nIf not, move this file to your preferred folder and start it again!", Application.StartupPath); if (MessageBox.Show(Message, "Install", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { callback(100, "Cancelled Installation"); return; } } DownloadManager.StartDownload(callback, Hashlist); }
static void Main(string[] args) { //args[0];//@"D:\JK\InzOpr\HashProgram\Pliki\pliktekstowy8.txt"; try { DirectoryInfo d = new DirectoryInfo(args[0]); foreach (var file in d.GetFiles("*.txt")) { string fileName = file.FullName; Console.WriteLine("\n\n\n\nHash of file: " + fileName); Console.WriteLine(); Console.WriteLine("---------------------------------------------------------------"); var watchMD2 = Stopwatch.StartNew(); MD2Own md2 = new MD2Own(); Console.WriteLine("The MD2 OWN hash is: " + md2.GetHash(File.ReadAllBytes(fileName)).ToUpper()); watchMD2.Stop(); Console.WriteLine("Time to execute: " + watchMD2.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchMD4 = Stopwatch.StartNew(); MD4Own md4 = new MD4Own(); Console.WriteLine("The MD4 OWN hash is: " + md4.Md4Hash(File.ReadAllBytes(fileName)).ToUpper()); watchMD4.Stop(); Console.WriteLine("Time to execute: " + watchMD4.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchMD5 = Stopwatch.StartNew(); MD5Lib md5 = new MD5Lib(); Console.WriteLine("The MD5 LIB hash is: " + md5.Compute(fileName).ToUpper() + "."); watchMD5.Stop(); Console.WriteLine("Time to execute: " + watchMD5.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchMD5Own = Stopwatch.StartNew(); MD5Own md = new MD5Own(fileName); Console.WriteLine("The MD5 OWN hash is: " + md.FingerPrint.ToUpper() + "."); watchMD5Own.Stop(); Console.WriteLine("Time to execute: " + watchMD5Own.ElapsedMilliseconds + "ms"); Console.WriteLine("---------------------------------------------------------------"); if (md5.Compute(fileName).ToUpper() == md.FingerPrint.ToUpper()) { Console.WriteLine("MD5 LIB and OWN are same"); } else { Console.WriteLine("MD5 LIB and OWN are not the same"); } Console.WriteLine("---------------------------------------------------------------"); SHA1Lib SHA1 = new SHA1Lib(fileName); var watchSHA1 = Stopwatch.StartNew(); Console.WriteLine("The SHA1 LIB hash is: " + SHA1.Compute(fileName) + "."); watchSHA1.Stop(); Console.WriteLine("Time to execute: " + watchSHA1.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchSHA1Own = Stopwatch.StartNew(); SHA1Own sha1Own = new SHA1Own(); Console.WriteLine("The SHA1 OWN hash is: " + sha1Own.glowna(File.ReadAllBytes(fileName)) + "."); watchSHA1Own.Stop(); Console.WriteLine("Time to execute: " + watchSHA1Own.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); if (SHA1.Compute(fileName) == sha1Own.glowna(File.ReadAllBytes(fileName))) { Console.WriteLine("SHA1 LIB and OWN are same"); } else { Console.WriteLine("SHA1 LIB and OWN are not the same"); } Console.WriteLine("---------------------------------------------------------------"); SHA256Lib SHA256 = new SHA256Lib(fileName); var watchSHA256 = Stopwatch.StartNew(); Console.WriteLine("The SHA256 LIB hash is: " + SHA256.Compute(fileName) + "."); watchSHA256.Stop(); Console.WriteLine("Time to execute: " + watchSHA256.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchSHA256Own = Stopwatch.StartNew(); SHA256Own SHA256Own = new SHA256Own(); Console.WriteLine("The SHA256 OWN hash is: " + SHA256Own.Compute(fileName).ToUpper() + "."); watchSHA256Own.Stop(); Console.WriteLine("Time to execute: " + watchSHA256Own.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); if (SHA256Own.Compute(fileName).ToUpper() == SHA256.Compute(fileName)) { Console.WriteLine("SHA256 LIB and OWN are same"); } else { Console.WriteLine("SHA256 LIB and OWN are not the same"); } Console.WriteLine("---------------------------------------------------------------"); SHA512Lib SHA512 = new SHA512Lib(fileName); var watchSHA512 = Stopwatch.StartNew(); Console.WriteLine("The SHA512 LIB hash is: " + SHA512.Compute(fileName) + "."); watchSHA512.Stop(); Console.WriteLine("Time to execute: " + watchSHA512.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchSHA512Own = Stopwatch.StartNew(); SHA512Own SHA512Owna = new SHA512Own(); var hash512 = String.Empty; byte[] a = SHA512Owna.Compute(File.ReadAllBytes(fileName), 512); foreach (byte b in SHA512Owna.Compute(File.ReadAllBytes(fileName), 512)) { hash512 += b.ToString("x2").ToUpper(); } Console.WriteLine("The SHA512 OWN hash is: {0}", hash512); Console.WriteLine("."); watchSHA512Own.Stop(); Console.WriteLine("Time to execute: " + watchSHA512Own.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); if (SHA512.Compute(fileName) == hash512) { Console.WriteLine("SHA512 LIB and OWN are same"); } else { Console.WriteLine("SHA512 LIB and OWN are not the same"); } Console.WriteLine("---------------------------------------------------------------"); var watchRIPEMD160 = Stopwatch.StartNew(); RIPEMD160Lib RIPEMD160 = new RIPEMD160Lib(fileName); Console.WriteLine("The RIPEMD160 LIB hash is: " + RIPEMD160.Compute(fileName) + "."); watchRIPEMD160.Stop(); Console.WriteLine("Time to execute: " + watchRIPEMD160.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchRIPEMD160Own = Stopwatch.StartNew(); RIPEMD160Own ripemd160Own = new RIPEMD160Own(); ripemd160Own.HashCore(File.ReadAllBytes(fileName), 0, File.ReadAllBytes(fileName).Length); var hashRMD = ""; using (var fs = File.Open(fileName, FileMode.Open)) foreach (byte b in ripemd160Own.HashFinal()) { hashRMD += b.ToString("x2").ToUpper(); } Console.WriteLine("The RIPEMD160 OWN hash is {0}", hashRMD.ToUpper()); watchRIPEMD160Own.Stop(); Console.WriteLine("Time to execute: " + watchRIPEMD160Own.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); if (RIPEMD160.Compute(fileName) == hashRMD) { Console.WriteLine("RIPEMD160 LIB and OWN are same"); } else { Console.WriteLine("RIPEMD160 LIB and OWN are not the same"); } Console.WriteLine("---------------------------------------------------------------"); var watchcrc32 = Stopwatch.StartNew(); CRC32Own crc32 = new CRC32Own(); var hash = String.Empty; using (var fs = File.Open(fileName, FileMode.Open)) foreach (byte b in crc32.ComputeHash(fs)) { hash += b.ToString("x2").ToUpper(); } watchcrc32.Stop(); Console.WriteLine("CRC 32 is {0}", hash); Console.WriteLine("Time to execute: " + watchcrc32.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchcrc64 = Stopwatch.StartNew(); CRC64Own crc64 = new CRC64Own(); Console.WriteLine("CRC 64 is {0}", (crc64.Compute(File.ReadAllBytes(fileName), 0, 3) - 1).ToString("x2")); watchcrc64.Stop(); Console.WriteLine("Time to execute: " + watchcrc64.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("---------------------------------------------------------------"); var watchADLER32 = Stopwatch.StartNew(); ADLER32Own ADLER32 = new ADLER32Own(); ADLER32.reset(); ADLER32.update(File.ReadAllBytes(fileName)); watchADLER32.Stop(); Console.WriteLine("ADLER32 is: " + ADLER32.getValue() + "."); Console.WriteLine("Time to execute: " + watchADLER32.ElapsedMilliseconds + "ms\n"); Console.WriteLine("---------------------------------------------------------------"); Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }