コード例 #1
0
        public override void Execute()
        {
            Logger.LogInformation("Calculate checksum for file : '{0}'", TargetCollection);

            var eventModel = CurrentActionProperties(TargetCollection, this.GetType().Name);

            OnTrigger(new PreingestEventArgs {
                Description = String.Format("Start calculate checksum for container '{0}'.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Started, PreingestAction = eventModel
            });

            var    anyMessages        = new List <String>();
            string currentCalculation = string.Empty;
            bool   isSuccess          = false;

            try
            {
                /**
                 * checksum-sha224
                 * checksum-sha384
                 **/

                switch (Checksum.ToUpperInvariant())
                {
                case "MD5":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Calculate checksum for container '{0}' with MD5.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-md5");
                    currentCalculation = ChecksumHelper.CreateMD5Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                case "SHA1":
                case "SHA-1":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Start calculate checksum for container '{0}' with SHA1.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-sha1");
                    currentCalculation = ChecksumHelper.CreateSHA1Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                case "SHA224":
                case "SHA-224":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Start calculate checksum for container '{0}' with SHA224.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-sha224");
                    currentCalculation = ChecksumHelper.CreateSHA224Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                case "SHA256":
                case "SHA-256":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Start calculate checksum for container '{0}' with SHA256.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-sha256");
                    currentCalculation = ChecksumHelper.CreateSHA256Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                case "SHA384":
                case "SHA-384":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Start calculate checksum for container '{0}' with SHA384.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-sha384");
                    currentCalculation = ChecksumHelper.CreateSHA384Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                case "SHA512":
                case "SHA-512":
                {
                    OnTrigger(new PreingestEventArgs {
                            Description = String.Format("Start calculate checksum for container '{0}' with SHA512.", TargetCollection), Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Executing, PreingestAction = eventModel
                        });
                    string url = String.Format("http://{0}:{1}/hooks/{3}?file={2}",
                                               ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, TargetCollection, "checksum-sha512");
                    currentCalculation = ChecksumHelper.CreateSHA512Checksum(new FileInfo(TargetCollection), url);
                }
                break;

                default:
                {
                    anyMessages.Add(String.Format("Checksum {0} not defined. No calculation available.", Checksum));
                    Logger.LogWarning(String.Format("Checksum {0} not defined. No calculation available.", Checksum));
                }
                break;
                }
                //failed, no value
                if (String.IsNullOrEmpty(currentCalculation))
                {
                    throw new ApplicationException("Calculation returned nothing or empty value!");
                }

                var fileInformation = new FileInfo(TargetCollection);
                anyMessages.Add(String.Concat("Name : ", fileInformation.Name));
                anyMessages.Add(String.Concat("Extension : ", fileInformation.Extension));
                anyMessages.Add(String.Concat("Size : ", fileInformation.Length));
                anyMessages.Add(String.Concat("CreationTime : ", fileInformation.CreationTimeUtc));
                anyMessages.Add(String.Concat("LastAccessTime : ", fileInformation.LastAccessTimeUtc));
                anyMessages.Add(String.Concat("LastWriteTime : ", fileInformation.LastWriteTimeUtc));
                eventModel.Properties.Messages = anyMessages.ToArray();

                var data = new List <String>();
                data.Add(Checksum);
                data.Add(currentCalculation);

                isSuccess = !String.IsNullOrEmpty(currentCalculation);

                eventModel.ActionResult.ResultValue = PreingestActionResults.Success;
                eventModel.Summary.Processed        = 1;
                eventModel.Summary.Accepted         = 1;
                eventModel.Summary.Rejected         = 0;

                if (!String.IsNullOrEmpty(DeliveredChecksumValue))
                {
                    bool isTheSame = DeliveredChecksumValue.Equals(currentCalculation, StringComparison.InvariantCultureIgnoreCase);
                    data.Add(String.Format("{0} {1} {2}", DeliveredChecksumValue, isTheSame ? "=" : "≠", currentCalculation));

                    eventModel.Properties.Messages = anyMessages.ToArray();
                    if (!isTheSame)
                    {
                        eventModel.ActionResult.ResultValue = PreingestActionResults.Error;
                        eventModel.Summary.Processed        = 1;
                        eventModel.Summary.Accepted         = 0;
                        eventModel.Summary.Rejected         = 1;
                    }
                }

                eventModel.ActionData = data.ToArray();
            }
            catch (Exception e)
            {
                isSuccess = false;

                anyMessages.Clear();
                anyMessages.Add(String.Format("Calculation checksum from file : '{0}' failed!", TargetCollection));
                anyMessages.Add(e.Message);
                anyMessages.Add(e.StackTrace);

                Logger.LogError(e, "Calculation checksum from file : '{0}' failed!", TargetCollection);

                eventModel.Properties.Messages      = anyMessages.ToArray();
                eventModel.ActionResult.ResultValue = PreingestActionResults.Failed;
                eventModel.Summary.Processed        = -1;
                eventModel.Summary.Accepted         = -1;
                eventModel.Summary.Rejected         = -1;

                OnTrigger(new PreingestEventArgs {
                    Description = "An exception occured while calculating the checksum!", Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Failed, PreingestAction = eventModel
                });
            }
            finally
            {
                if (isSuccess)
                {
                    OnTrigger(new PreingestEventArgs {
                        Description = "Checksum calculation is done.", Initiate = DateTimeOffset.Now, ActionType = PreingestActionStates.Completed, PreingestAction = eventModel
                    });
                }
            }
        }
コード例 #2
0
        private List <ResultType> RunFixityCheck(FileInfo binaryFiles, AlgorithmTypes useAlgorithm, string valueToCompare = null)
        {
            List <ResultType> checkResult = new List <ResultType>();

            string currentCalculation = string.Empty, url = string.Empty, urlPart = string.Empty;

            try
            {
                switch (useAlgorithm)
                {
                default:
                case AlgorithmTypes.MD5:
                {
                    urlPart            = "checksum-md5";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateMD5Checksum(binaryFiles, url);
                }
                break;

                case AlgorithmTypes.SHA1:
                {
                    urlPart            = "checksum-sha1";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateSHA1Checksum(binaryFiles, url);
                }
                break;

                case AlgorithmTypes.SHA224:
                {
                    urlPart            = "checksum-sha224";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateSHA224Checksum(binaryFiles, url);
                }
                break;

                case AlgorithmTypes.SHA256:
                {
                    urlPart            = "checksum-sha256";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateSHA256Checksum(binaryFiles, url);
                }
                break;

                case AlgorithmTypes.SHA384:
                {
                    urlPart            = "checksum-sha384";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateSHA384Checksum(binaryFiles, url);
                }
                break;

                case AlgorithmTypes.SHA512:
                {
                    urlPart            = "checksum-sha512";
                    url                = String.Format("http://{0}:{1}/hooks/{3}?file={2}", ApplicationSettings.ChecksumServerName, ApplicationSettings.ChecksumServerPort, binaryFiles.FullName, urlPart);
                    currentCalculation = ChecksumHelper.CreateSHA512Checksum(binaryFiles, url);
                }
                break;
                }
            }
            catch (Exception e)
            {
                checkResult.Add(new ResultType
                {
                    Type = ResultType.InfoType.Message,
                    Data = new string[] { binaryFiles.FullName, "Failed to run fixity checksum!", e.Message, e.StackTrace }
                });
            }
            finally {
                if (!String.IsNullOrEmpty(currentCalculation) && HashType.ProcessingMode == ExecutionMode.OnlyCalculate)
                {
                    checkResult.Add(new ResultType
                    {
                        Type = ResultType.InfoType.Action,
                        Data = new string[] { binaryFiles.FullName, currentCalculation }
                    });
                }

                if (!String.IsNullOrEmpty(currentCalculation) && HashType.ProcessingMode == ExecutionMode.CalculateAndCompare)
                {
                    if (currentCalculation.Equals(valueToCompare, StringComparison.InvariantCultureIgnoreCase))
                    {
                        checkResult.Add(new ResultType
                        {
                            Type = ResultType.InfoType.Action,
                            Data = new string[] { binaryFiles.FullName,
                                                  useAlgorithm.ToString(),
                                                  string.Format("Our calculation : {0}", currentCalculation),
                                                  string.Format("Provided value : {0}", valueToCompare), }
                        });
                    }
                    else
                    {
                        checkResult.Add(new ResultType
                        {
                            Type = ResultType.InfoType.Message,
                            Data = new string[] { String.Format("Fixity comparison with file '{0}' using algorithm '{1}' failed!{2}Our calculation: {3}{2}Provided value:{4}  ", binaryFiles.FullName, useAlgorithm, Environment.NewLine, currentCalculation, valueToCompare) }
                        });
                    }
                }
            }

            return(checkResult);
        }