Exemple #1
0
                public void SetFileName(int index, string newName)
                {
                    HashedFile item = Data.items[index];

                    item.oldFileName = item.FileName;
                    item.FileName    = newName;
                }
Exemple #2
0
                public void AddActual(string name, byte[] storedActualHash, HashMode hashStyle = HashMode.Binary)
                {
                    var item = new HashedFile(name, storedActualHash, hashStyle, storedActualHash);

                    Data.items.Add(item);
                    item.IsMatch = storedActualHash == null? (bool?)null : true;
                }
Exemple #3
0
                public void SetOldFileName(int index, string newOldName)
                {
                    HashedFile item = Data.items[index];

                    if (item.oldFileName != newOldName)
                    {
                        item.oldFileName = newOldName;
                        item.RaisePropertyChanged(null);
                    }
                }
Exemple #4
0
                public void SetStoredHash(int index, byte[] newHash)
                {
                    HashedFile item = Data.items[index];

                    if (!newHash.SequenceEqual(item.storedHash))
                    {
                        item.storedHash = new byte[newHash.Length];
                        Array.Copy(newHash, item.storedHash, newHash.Length);
                        SetIsMatch(index, newHash.SequenceEqual(item.actualHash));
                        item.RaisePropertyChanged(null);
                    }
                }
Exemple #5
0
            public string GetPath(int index)
            {
                HashedFile item = items[index];

                if (item.IsRelative == true)
                {
                    return(BaseDir + item.FileName);
                }
                else
                {
                    return(item.FileName);
                }
            }
Exemple #6
0
                public void SetActualHash(int index, byte[] newHash)
                {
                    HashedFile item = Data.items[index];

                    item.actualHash = newHash;
                    if (newHash == null)
                    {
                        SetIsMatch(index, false);
                    }
                    else
                    {
                        SetIsMatch(index, item.StoredHash != null && item.actualHash.SequenceEqual(item.StoredHash));
                    }
                }
Exemple #7
0
                public void SetIsMatch(int index, bool newValue)
                {
                    HashedFile item = Data.items[index];

                    if (newValue != item.IsMatch)
                    {
                        if (item.IsMatch == true)
                        {
                            --Data.MatchCount;
                        }
                        if (newValue == true)
                        {
                            ++Data.MatchCount;
                        }
                        item.IsMatch = newValue;
                    }
                }
Exemple #8
0
                public void SetIsFound(int index, bool newValue)
                {
                    HashedFile item = Data.items[index];

                    if (newValue != item.IsFound)
                    {
                        if (item.IsFound == true)
                        {
                            --Data.FoundCount;
                        }
                        if (newValue == true)
                        {
                            ++Data.FoundCount;
                        }
                        else if (newValue == false)
                        {
                            item.actualHash = null;
                        }
                        item.IsFound = newValue;
                    }
                }
Exemple #9
0
                public void SetStoredHashToActual(int index)
                {
                    HashedFile item = Data.items[index];

                    if (item.actualHash == null)
                    {
                        if (item.storedHash != null)
                        {
                            item.IsMatch    = item.actualHash != null? false : (bool?)null;
                            item.storedHash = null;
                            item.RaisePropertyChanged(null);
                        }
                    }
                    else if (!item.actualHash.SequenceEqual(item.storedHash))
                    {
                        item.storedHash = new byte[item.actualHash.Length];
                        Array.Copy(item.actualHash, item.storedHash, item.storedHash.Length);
                        SetIsMatch(index, true);
                        item.RaisePropertyChanged(null);
                    }
                }
Exemple #10
0
            protected void ComputeContentHashes(CryptoHasher hasher, Hashes mediaHash = Hashes.None)
            {
                System.Diagnostics.Debug.Assert(Data.HashedFiles.HashLength == hasher.HashLength);

                for (int index = 0; index < Data.HashedFiles.Items.Count; ++index)
                {
                    HashedFile item       = Data.HashedFiles.Items[index];
                    string     err        = null;
                    var        targetName = Data.HashedFiles.GetPath(index);

                    try
                    {
                        using (var tfs = new FileStream(targetName, FileMode.Open, FileAccess.Read))
                        {
                            HashedModel.SetIsFound(index, true);
                            byte[] hash = null;

                            if (item.Mode == HashMode.Text || item.Mode == HashMode.Binary)
                            {
                                hasher.Append(tfs);
                                hash = hasher.GetHashAndReset();
                            }
                            else if (!(hasher is Sha1Hasher))
                            {
                                IssueModel.Add("Nonfile hashes not supported for this type.");
                            }
                            else
                            {
                                var h0       = item.Mode == HashMode.Meta ? Hashes.MetaSHA1 : Hashes.MediaSHA1;
                                var fmtModel = FormatBase.Model.Create(tfs, targetName, h0);
                                if (fmtModel == null)
                                {
                                    IssueModel.Add($"Unknown file format item #{index+1}.");
                                }
                                else
                                {
                                    hash = item.Mode == HashMode.Meta ? fmtModel.Data.MetaSHA1 : fmtModel.Data.MediaSHA1;
                                }
                            }

                            HashedModel.SetActualHash(index, hash);
                            if (item.IsMatch == false)
                            {
                                IssueModel.Add($"{Data.HasherName} mismatch on '{item.FileName}'.");
                            }
                        }
                    }
                    catch (Exception ex) when(ex is FileNotFoundException || ex is IOException || ex is UnauthorizedAccessException)
                    {
                        err = ex.Message.TrimEnd(null);
                    }

                    if (err != null)
                    {
                        HashedModel.SetIsFound(index, false);
                        IssueModel.Add(err);
                    }
                }

                string tx = $"{Data.HasherName} validation of {Data.HashedFiles.Items.Count} file";

                if (Data.HashedFiles.Items.Count != 1)
                {
                    tx += "s";
                }
                if (base.Data.Issues.MaxSeverity < Severity.Error)
                {
                    tx += " successful.";
                }
                else
                {
                    tx += $" failed with {Data.HashedFiles.FoundCount} found and {Data.HashedFiles.MatchCount} matched.";
                }

                IssueModel.Add(tx, Severity.Advisory);
            }