コード例 #1
0
ファイル: ChecksumFile.cs プロジェクト: PhenTse/virtualRadar
        /// <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}");
            }
        }