/// <summary> /// Tests the content checksum for validity, throws an exception if it is invalid. /// </summary> /// <param name="lines"></param> private static void EnforceContentChecksum(List <string> lines) { if (lines.Count == 0) { throw new InvalidOperationException("The checksums file is empty"); } var contentLine = lines[0]; var contentEntry = CreateFromLine(contentLine); if (contentEntry == null || contentEntry.FileName != "\\**CONTENT CHECKSUM**") { throw new InvalidOperationException($@"""{contentLine}"" is not a valid content checksum line"); } lines.RemoveAt(0); var checksumBytes = Encoding.UTF8.GetBytes(String.Concat(lines)); var checksum = ChecksumFileEntry.GenerateChecksum(checksumBytes); var checksumSize = lines.Sum(r => r.Length); if (contentEntry.FileSize != checksumSize) { throw new InvalidOperationException($"The lines in the checksums file have been altered - was expecting {contentEntry.FileSize} bytes, the lines added up to {checksumSize}"); } if (contentEntry.Checksum != checksum) { throw new InvalidOperationException($"The checksums file has been altered - was expecting a checksum of {contentEntry.Checksum}, it was actually {checksum}"); } }
private static ChecksumFileEntry CreateFromLine(string line) { ChecksumFileEntry result = null; var slashPosn = line.IndexOf('\\'); if (slashPosn != -1) { var chunks = line.Substring(0, slashPosn).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (chunks.Length == 2) { result = new ChecksumFileEntry() { Checksum = chunks[0], FileSize = long.Parse(chunks[1]), FileName = line.Substring(slashPosn) }; } } return(result); }