public void Chew() { // Calculalte the file hash. FileHash = FileHasher.ComputeHash(PackageFilePath); // TODO: Extract the infomation from the package. }
/// <summary> /// The main entry point of this program /// </summary> /// <param name="args">The arguments to this program</param> public static void Main(string[] args) { var path = @"D:\Test\[Doki] Blood-C - 01 (1280x720 Hi10P BD FLAC) [E21885ED].mkv"; var h = FileHasher.HashFile(path); System.Console.WriteLine(h); }
private void LoadButton_Click(object sender, EventArgs e) { OpenFileDialog.Filter = "Microsoft Word 2007|*.docx|All files (*.*)|*.*"; OpenFileDialog.FilterIndex = 1; OpenFileDialog.RestoreDirectory = true; if (OpenFileDialog.ShowDialog() == DialogResult.OK) { byte[] fileHash = FileHasher.HashMD5(OpenFileDialog.FileName); if (!fileRepository.CheckFileHash(fileHash) && fileRepository.InsertFileHashInDB(OpenFileDialog.FileName, fileHash)) { Cursor.Current = Cursors.WaitCursor; parser = new DocumentParser(OpenFileDialog.FileName); parser.ParseDocument(); pairRepository.InsertPairs(parser.ParseDocument()); Cursor.Current = Cursors.Arrow; } else { MessageBox.Show("File already exist in DB", "ScheduleApp"); } } }
private void IOnRecalculateHash(FileHasher fileHasher) { if (fileHasher.FileLocationFromDataPath.Equals("/Managed/Assembly-CSharp.dll")) { fileHasher.FileLocationFromDataPath = "/Managed/Assembly-CSharp_Original.dll"; } }
private void cmbHashType_SelectionChanged(object sender, SelectionChangedEventArgs e) { var fhasher = new FileHasher(); var item = (ComboBoxItem)e.AddedItems[0]; string text = item.Content.ToString(); if (fullFilePath == "") { //we do not have a file to hash...user must be entering a hash manually... //TODO: determine what hash they are entering. return; } if (text == "MD5") { //set to md5 hash txtBxFileHash.Text = fhasher.getFileHashMd5(fullFilePath); } if (text == "Sha256") { //set to md5 hash txtBxFileHash.Text = fhasher.getFileHashSha256(fullFilePath); } }
public void SameContentButDifferentName_GetHash_True() { FileHasher fileHasher = new FileHasher(); string hashFile1 = fileHasher.GetHash("File1.txt"); string hashFile2 = fileHasher.GetHash("File2.txt"); Assert.IsTrue(hashFile1.Equals(hashFile2)); }
public void SameFileButDifferentLocations_GetHash_True() { FileHasher fileHasher = new FileHasher(); string hashFile1 = fileHasher.GetHash("Path1//File1.txt"); string hashFile2 = fileHasher.GetHash("Path2//File1.txt"); Assert.IsTrue(hashFile1.Equals(hashFile2)); }
public void SameFileButDifferentLocationsAndContent_GetHash_False() { FileHasher fileHasher = new FileHasher(); string hashFile1 = fileHasher.GetHash("Path1//File1.txt"); string hashFile2 = fileHasher.GetHash("File1.txt"); Assert.IsFalse(hashFile1.Equals(hashFile2)); }
public ActionResult Thumbnail(string path, int width = 51) { var physialPath = Server.MapPath(path); if (!System.IO.File.Exists(physialPath)) { return(HttpNotFound()); } var info = new FileInfo(physialPath); var md5Hash = FileHasher.CalculateMD5(physialPath); var lastWrite = info.LastWriteTimeUtc; var requestedETag = Request.Headers["If-None-Match"]; if (requestedETag == md5Hash) { return(new HttpStatusCodeResult(HttpStatusCode.NotModified)); } var image = new WebImage(physialPath); if (image.Width > width) { float fH = (width * image.Height) / image.Width; var height = (int)Math.Floor(fH); image = image.Resize(width, height).Crop(1, 1); } Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); Response.Cache.SetETag(md5Hash); Response.Cache.SetLastModified(lastWrite); return(File( image.GetBytes(), MimeMapping.GetMimeMapping(info.Name), info.Name )); //Response.AddOnSendingHeaders(ctx => //{ // ctx.Response.Headers.Remove("accept-ranges"); // ctx.Response.AppendHeader("accept-ranges", "bytes"); // ctx.Response.Headers.Remove("cache-control"); // ctx.Response.AppendHeader("cache-control", "max-age=864000"); // 10 days in seconds, see config file // ctx.Response.Headers.Remove("etag"); // ctx.Response.AppendHeader("etag", md5Hash); // ctx.Response.Headers.Remove("last-modified"); // ctx.Response.AppendHeader("last-modified", $"{lastWrite} GMT"); //}); //image.Resize(width, H).Crop(1, 1).Write(); }
private static string GetCacheFileName(string fileName) { string cacheFolder = Directory .CreateDirectory(SharedConfiguration.Configuration.GetValue("CachePath", "cache")).FullName; string ext = Path.GetExtension(fileName).TrimStart('.'); string hash = FileHasher.HashFile(fileName).Substring(0, 7); return(Path.Combine(cacheFolder, $"{Path.GetFileNameWithoutExtension(fileName)}.{hash}.{ext}.cache")); }
private void UIActionHashFile_Click(object sender, EventArgs e) { string fileContent = FileManager.ReadTextFile(pathToFile); FileHasher hasher = new FileHasher(); string hashedContent = hasher.HashFile(fileContent); FileManager.WriteTextToFile("sazetak.txt", hashedContent); MessageBox.Show(hashedContent); }
public virtual bool CheckFileHash(byte[] openedFile) { foreach (var file in GetHashAllFiles()) { if (FileHasher.CompareHashes(file, openedFile)) { return(true); } } return(false); }
// 异步处理文件 async void GoHash(FileHasher task) { #if DEBUG var timer = new System.Diagnostics.Stopwatch(); timer.Start(); #endif await task.StartHashFile(); AddResult(task); #if DEBUG timer.Stop(); textBox_Stream.AppendText("耗时:" + timer.Elapsed.TotalMilliseconds + "ms" + newline + newline); #endif }
private void VerifyHash(string fileContent, FileHasher.Algorithm algorithm, string expectedHash) { var tempFile = Path.GetTempFileName(); using (var outputFile = new StreamWriter(tempFile)) { outputFile.WriteLine(fileContent); } var hash = FileHasher.GetHash(tempFile, algorithm); File.Delete(tempFile); Assert.AreEqual(expectedHash, hash); }
private void SaveEditingIgnoreFileButton_Click(object sender, RoutedEventArgs e) { var errors = new StringBuilder(); int counter = 0; var safeIgnoreFile = new CommonSaveFileDialog(); safeIgnoreFile.DefaultExtension = "repignore"; if (safeIgnoreFile.ShowDialog() == CommonFileDialogResult.Ok) { var ignoreFile = _ignoreFile ?? new IgnoreFile(); var toIgnoreFiles = editIgnoreFileTextBox.Text.Split('\n').Select(f => f.Trim(' ', '\t', '\n', '\r')); ignoreFile.Clear(); foreach (var toIgnoreFile in toIgnoreFiles) { if (string.IsNullOrWhiteSpace(toIgnoreFile)) { continue; } string hash = null; try { hash = FileHasher.GetMd5Hash(toIgnoreFile); if (hash == null) { counter++; errors.AppendLine($"{counter}. Failed to compute hash for file {toIgnoreFile}"); continue; } ignoreFile.Ignore(new Tuple <string, string>(toIgnoreFile, hash)); } catch (Exception ex) { ErrorLogger.GetInstance()?.LogError($"An error occurred while trying to compute the hash for file {toIgnoreFile}", ex: ex); counter++; errors.AppendLine($"{counter}. An error occurred while processing the file {toIgnoreFile}"); } } _ignoreFileManager.Save(ignoreFile, safeIgnoreFile.FileName, true); _replaySorterConfiguration.IgnoreFilePath = safeIgnoreFile.FileName; } if (errors.Length > 0) { MessageBox.Show($"The following replays encountered errors:{Environment.NewLine}{errors.ToString()}", "Invalid files", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); } this.Close(); }
// 输出结果 private void AddResult(FileHasher result) { string str = result.HashResult; if ((str != Hasher.STATUS.FILE_ERROR) && (str != Hasher.STATUS.HASH_INCOMPL)) { textBox_HashCode.Text = str; } // 是否显示完整的路径 bool isFullPath = checkBox_IsFullPath.IsChecked.Value; textBox_Stream.AppendText((isFullPath ? result.FilePath : result.FileName) + newline + str + newline + newline); }
private static (IReportingMultipleFileHasher, FileReadProgressReporter) BuildHasher(FileInfo decoderFile) { var fileReadProgressReporter = new FileReadProgressReporter(); var steamFactory = new IO.ProgressReportingReadStreamFactory(fileReadProgressReporter); var decoder = new IO.Audio.Flac.CmdLine.StreamDecoder( decoderFile, new ExternalProcess.ProcessRunner(processTimeoutSec)); var reader = new IO.Audio.DecodingFileReader(steamFactory, decoder); var hasher = new FileHasher(reader, new Crypto.Sha256HashComputer()); var cancellableHasher = new ReportingMultipleFileHasher( new MultipleFileHasher(hasher)); return(cancellableHasher, fileReadProgressReporter); }
public void GetFilesDiff(Action <List <FileDiffListItem>, Dictionary <String, String> > callback) { UpdateInfo latestUpdate = LatestVersion; List <FileDiffListItem> diff = new List <FileDiffListItem>(); Dictionary <String, String> items = new Dictionary <String, String>(); foreach (string curFile in Directory.GetFiles(this.ProjectFolder, "*", SearchOption.AllDirectories)) { string curChecksum = FileHasher.GetFileChecksum(curFile); string curRelativePath = AbsoluteToRelativePath(this.ProjectFolder, curFile); items.Add(curRelativePath, curChecksum); if (latestUpdate != null) { string baseCheckSum; if (latestUpdate.fileChecksums.TryGetValue(curRelativePath, out baseCheckSum)) { if (!baseCheckSum.Equals(curChecksum)) { diff.Add(new FileDiffListItem(curRelativePath, FileDiffListItem.FileState.CHANGED)); } } else { diff.Add(new FileDiffListItem(curRelativePath, FileDiffListItem.FileState.ADDED)); } } else { diff.Add(new FileDiffListItem(curRelativePath, FileDiffListItem.FileState.ADDED)); } } if (latestUpdate != null) { foreach (KeyValuePair <string, string> cur in LatestVersion.fileChecksums) { if (!File.Exists(ProjectFolder + "/" + cur.Key)) { diff.Add(new FileDiffListItem(cur.Key, FileDiffListItem.FileState.REMOVED)); } } } callback.Invoke(diff, items); }
bool isProcessing = false; // 文件大小有可能为0,不宜用于判断任务状态,虽然巧妙但容易出错 // 处理打开多个文件的逻辑控制 // 异步调用哈希算法,异步输出结果,避免死锁 // 尽量使用TAP模式,避免EAP、APM模式 // 此处void用作事件处理,其它情况异步应避免使用void,改用Task private void HashFiles(string[] files) { if ((files == null) || (files.Length <= 0)) { return; // 判断文件,不支持目录 } FirstClear(); // 添加到处理队列 foreach (string f in files) { var task = new FileHasher(hashAlgo, f); totalFileBytes += task.FileLength; hashList.Add(task); GoHash(task); } SetGUIBusy(); }
private void cmbHashType_SelectionChanged(object sender, SelectionChangedEventArgs e) { var fhasher = new FileHasher(); var item = (ComboBoxItem)e.AddedItems[0]; string text = item.Content.ToString(); if (text == "MD5") { //set to md5 hash // txtBxFileHash.Text = fhasher.getFileHashMd5(txtBxFileName.Text); } if (text == "Sha256") { //set to md5 hash // txtBxFileHash.Text = fhasher.getFileHashSha256(txtBxFileName.Text); } }
public void Chew() { // Calculalte the file hash. FileHash = FileHasher.ComputeHash(PackageFilePath); // TODO: Extract the infomation from the package. // Extract *cab files from .msu file. extractMsuPackage(PackageFilePath, DestinationDirectoryPath); // Extract the inner *.cab files. string innerCabFilePath = getInnerCabFilePath(); string innerCabDestinationDirectoryPath = getInnerCabDestinationDirectoryPath(DestinationDirectoryPath); extractInnerCabFiles(innerCabFilePath, innerCabDestinationDirectoryPath); // TODO: file information collection. }
private static bool IsValidMd5(string path) { if (path.EndsWith(".md5")) { return(true); } var md5File = path + ".md5"; if (!File.Exists(md5File)) { throw new InvalidOperationException($"No MD5 file was found for {path}"); } var fileMd5 = File.ReadAllText(md5File); var computedMd5 = FileHasher.CalculateMd5(path); return(fileMd5 == computedMd5); }
public static uint FastComputeHash(string formula, string mpqFile, String gameExe, String bnclientDll, String d2clientDll) { byte[] game = File.ReadAllBytes(gameExe); byte[] bnclient = File.ReadAllBytes(bnclientDll); byte[] d2client = File.ReadAllBytes(d2clientDll); int mpq = Convert.ToInt32(mpqFile[mpqFile.LastIndexOf('.') - 1].ToString(), 10); uint[] values = new uint[4]; IEnumerable <FormulaOp> ops = BuildFormula(formula, ref values); values[0] ^= Hashes[mpq]; FileHasher HashFile = BuildFileHasher(ops); HashFile(ref values[0], ref values[1], ref values[2], ref values[3], game); HashFile(ref values[0], ref values[1], ref values[2], ref values[3], bnclient); HashFile(ref values[0], ref values[1], ref values[2], ref values[3], d2client); return(values[2]); }
public ActionResult Thumbnail(string path, int width = 91) { var physialPath = Server.MapPath(path); if (!System.IO.File.Exists(physialPath)) { return(HttpNotFound()); } var info = new FileInfo(physialPath); var md5Hash = FileHasher.CalculateMD5(physialPath); var lastWrite = info.LastWriteTimeUtc; var requestedETag = Request.Headers["If-None-Match"]; if (requestedETag == md5Hash) { return(new HttpStatusCodeResult(HttpStatusCode.NotModified)); } var image = new WebImage(physialPath); if (image.Width > width) { float fH = (width * image.Height) / image.Width; var height = (int)Math.Floor(fH); image = image.Resize(width, height).Crop(1, 1); } Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); Response.Cache.SetETag(md5Hash); Response.Cache.SetLastModified(lastWrite); return(File( image.GetBytes(), MimeMapping.GetMimeMapping(info.Name), info.Name )); }
public FileHashDuplicatePatternMatcher(FileHasher fileHasher) { this.fileHasher = fileHasher; }
public ReplayParser.ReplaySorter.Backup.Models.Backup ToBackup() { var backup = new ReplayParser.ReplaySorter.Backup.Models.Backup(); backup.Name = Name; backup.Comment = Comment; backup.RootDirectory = RootDirectory; var rootDirectory = string.Empty; if (RootDirectory.Last() != Path.DirectorySeparatorChar || RootDirectory.Last() != Path.AltDirectorySeparatorChar) { rootDirectory = RootDirectory + Path.DirectorySeparatorChar; } ICollection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup> replayBackups = new Collection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup>(); var replayDictionary = new Dictionary <string, ReplayParser.ReplaySorter.Backup.Models.Replay>(); foreach (var file in Replays) { try { byte[] bytes = System.IO.File.ReadAllBytes(file); string hash = FileHasher.GetMd5Hash(bytes); ReplayParser.ReplaySorter.Backup.Models.Replay replay = null; if (!replayDictionary.TryGetValue(hash, out replay)) { replay = new ReplayParser.ReplaySorter.Backup.Models.Replay { Hash = hash, Bytes = bytes }; replayDictionary.Add(hash, replay); } var replayBackup = new ReplayParser.ReplaySorter.Backup.Models.ReplayBackup { Backup = backup, FileName = file.Contains(rootDirectory) ? file.Substring(rootDirectory.Length) : file, //FileName = file, Replay = replay }; if (replay.ReplayBackups == null) { replay.ReplayBackups = new Collection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup>(); } replay.ReplayBackups.Add(replayBackup); replayBackups.Add(replayBackup); } catch (Exception ex) { //TODO If a backup can't backup everything, it should fail instead... ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - Something went wrong while opening or hashing the file", ex: ex); } } backup.ReplayBackups = replayBackups; return(backup); }
public void Setup() { fileHasher = new FileHasher(); File.WriteAllText(filePath, "test"); fileHasher.SetFilePath(filePath); }
private string GetFileHash(string path) { var fhasher = new FileHasher(); return(fhasher.getFileHashSha256(path)); }
protected static FileHasher BuildFileHasher(IEnumerable <FormulaOp> ops) { Type uint_t = typeof(uint).MakeByRefType(); DynamicMethod method = new DynamicMethod("HashFile", typeof(void), new Type[] { uint_t, uint_t, uint_t, uint_t, typeof(byte[]) }); MethodInfo touint32 = typeof(BitConverter).GetMethod("ToUInt32"); ILGenerator gen = method.GetILGenerator(); Label start = gen.DefineLabel(); LocalBuilder index = gen.DeclareLocal(typeof(int)); LocalBuilder len = gen.DeclareLocal(typeof(int)); // initialize the loop counter gen.Emit(OpCodes.Ldc_I4_0); gen.Emit(OpCodes.Stloc, index); // load the length of the array into a local gen.Emit(OpCodes.Ldarg, (short)4); gen.Emit(OpCodes.Ldlen); gen.Emit(OpCodes.Conv_I4); gen.Emit(OpCodes.Stloc, len); // start of loop across the file gen.MarkLabel(start); // load the value of arg4 at index into the address of arg3 gen.Emit(OpCodes.Ldarg_3); gen.Emit(OpCodes.Ldarg_S, (byte)4); gen.Emit(OpCodes.Ldloc, index); gen.EmitCall(OpCodes.Call, touint32, null); gen.Emit(OpCodes.Stind_I4); // for each op in the formula... foreach (var op in ops) { // load the result address gen.Emit(Ldargs[op.Result]); // load the first value gen.Emit(Ldargs[op.Variable1]); gen.Emit(OpCodes.Ldind_U4); // load the second value gen.Emit(Ldargs[op.Variable2]); gen.Emit(OpCodes.Ldind_U4); // execute the operator gen.Emit(Operators[op.Operation]); // store the result in the result address gen.Emit(OpCodes.Stind_I4); } // increment the loop counter gen.Emit(OpCodes.Ldloc, index); gen.Emit(OpCodes.Ldc_I4_4); gen.Emit(OpCodes.Add); gen.Emit(OpCodes.Stloc, index); // jump back to the top of the label if the loop counter is less arg4's length gen.Emit(OpCodes.Ldloc, index); gen.Emit(OpCodes.Ldloc, len); gen.Emit(OpCodes.Blt, start); gen.Emit(OpCodes.Ret); FileHasher del = (FileHasher)method.CreateDelegate(typeof(FileHasher)); return(del); }
static int Main(string[] args) { Settings settings; try { var settingsFile = new FileInfo(settingsFileName); settings = SettingsProvider.GetSettings(settingsFile); } catch (Exception e) { Console.WriteLine($"Failure reading a settings file. {e.Message}"); return((int)ReturnValue.SettingsReadingFailure); } Parameters parameters; try { var argumentDictionary = ArgumentSplitter.GetArguments(args); parameters = ParameterReader.GetParameters(argumentDictionary); } catch (CmdLineArgNotFoundException e) { Console.Error.WriteLine(e.Message); return((int)ReturnValue.ArgumentNotFound); } catch (Exception e) { Console.Error.WriteLine(e.Message); return((int)ReturnValue.ArgumentError); } try { FileInfo decoderFile = ExecutionParameterResolver.GetDecoder(settings, parameters); string outputFomat = ExecutionParameterResolver.ResolveOutputFormat(settings, parameters); int processTimeoutSec = ExecutionParameterResolver.GetProcessTimeoutInSeconds(settings, parameters, processTimeoutSecDefault); IList <FileInfo> inputFiles = ExecutionParameterResolver.GetInputFiles(parameters); var decoder = new IO.Audio.Flac.CmdLine.FileDecoder( decoderFile, new ExternalProcess.ProcessRunner(processTimeoutSec)); var hasher = new FileHasher(decoder, new Sha256HashComputer()); var multiHasher = new MultipleFileHasher(hasher); IEnumerable <FileHashResult> hashes = multiHasher .ComputeHashes(inputFiles); // The hashes should be computed on this enumeration, and therefore will be output as they're computed foreach (var entry in hashes) { OutputHash(entry.Hash, outputFomat, entry.File); } ; } catch (ConfigurationException e) { Console.WriteLine(e.Message); return((int)ReturnValue.ConfigurationError); } catch (IO.IOException e) { Console.WriteLine(e.Message); return((int)ReturnValue.InputReadingFailure); } catch (Exception e) { Console.WriteLine(e.Message); return((int)ReturnValue.ExecutionFailure); } Console.Error.WriteLine("Done!"); return((int)ReturnValue.Success); }
void OnRecalculateHash(FileHasher fileHasher) { if (fileHasher.FileLocationFromDataPath.Equals("/Managed/Assembly-CSharp.dll")) fileHasher.FileLocationFromDataPath = "/Managed/Assembly-CSharp_Original.dll"; }