public static List <LogInfo> Hash(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Hash)); CodeInfo_Hash info = cmd.Info as CodeInfo_Hash; string hashTypeStr = StringEscaper.Preprocess(s, info.HashType); string filePath = StringEscaper.Preprocess(s, info.FilePath); HashType hashType; if (hashTypeStr.Equals("MD5", StringComparison.OrdinalIgnoreCase)) { hashType = HashType.MD5; } else if (hashTypeStr.Equals("SHA1", StringComparison.OrdinalIgnoreCase)) { hashType = HashType.SHA1; } else if (hashTypeStr.Equals("SHA256", StringComparison.OrdinalIgnoreCase)) { hashType = HashType.SHA256; } else if (hashTypeStr.Equals("SHA384", StringComparison.OrdinalIgnoreCase)) { hashType = HashType.SHA384; } else if (hashTypeStr.Equals("SHA512", StringComparison.OrdinalIgnoreCase)) { hashType = HashType.SHA512; } else { throw new ExecuteException($"Invalid hash type [{hashTypeStr}]"); } string digest; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { digest = HashHelper.CalcHashString(hashType, fs); } logs.Add(new LogInfo(LogState.Success, $"Hash [{hashType}] digest of [{filePath}] is [{digest}]")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, digest.ToString()); logs.AddRange(varLogs); return(logs); }
public static List <LogInfo> Hash(EngineState s, CodeCommand cmd) { List <LogInfo> logs = new List <LogInfo>(); CodeInfo_Hash info = cmd.Info.Cast <CodeInfo_Hash>(); string hashTypeStr = StringEscaper.Preprocess(s, info.HashType); string filePath = StringEscaper.Preprocess(s, info.FilePath); Debug.Assert(hashTypeStr != null, $"{nameof(hashTypeStr)} != null"); Debug.Assert(filePath != null, $"{nameof(filePath)} != null"); if (!File.Exists(filePath)) { return(LogInfo.LogErrorMessage(logs, $"File [{filePath}] does not exist")); } long reportInterval = 0; FileInfo fi = new FileInfo(filePath); long fileSize = fi.Length; // If file size is larger than 32MB, turn on progress report if (ReportThreshold <= fileSize) { // reportInterval should be times of 1MB reportInterval = NumberHelper.Round(fileSize / 32, ReportRound); // If reportInterval is larger than 128MB, limit to 128MB if (ReportIntervalMax < reportInterval) { reportInterval = ReportIntervalMax; } } string digest; HashHelper.HashType hashType = HashHelper.ParseHashType(hashTypeStr); if (0 < reportInterval) { s.MainViewModel.SetBuildCommandProgress("Hash Progress"); } try { IProgress <long> progress = new Progress <long>(pos => { double percent = (double)pos / fileSize * 100; s.MainViewModel.BuildCommandProgressValue = percent; s.MainViewModel.BuildCommandProgressText = $"Hashing {hashType}... ({percent:0.0}%)"; }); byte[] rawDigest; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (0 < reportInterval) { rawDigest = HashHelper.GetHash(hashType, fs, reportInterval, progress); } else { rawDigest = HashHelper.GetHash(hashType, fs); } } digest = StringHelper.ToHexStr(rawDigest); } finally { if (0 < reportInterval) { s.MainViewModel.ResetBuildCommandProgress(); } } logs.Add(new LogInfo(LogState.Success, $"Hash [{hashType}] digest of [{filePath}] is [{digest}]")); List <LogInfo> varLogs = Variables.SetVariable(s, info.DestVar, digest); logs.AddRange(varLogs); return(logs); }