File: src\CreateMD5SumsFile.cs
Web Access
Project: src\src\Microsoft.DotNet.Build.Tasks.Installers\Microsoft.DotNet.Build.Tasks.Installers.csproj (Microsoft.DotNet.Build.Tasks.Installers)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Build.Framework;
 
namespace Microsoft.DotNet.Build.Tasks.Installers
{
    /// <summary>
    /// Produce an md5sums file for a set of files.
    /// </summary>
    /// <remarks>
    /// Emits a file of the format specified by https://manpages.debian.org/bookworm/dpkg-dev/deb-md5sums.5.en.html
    /// </remarks>
    public sealed class CreateMD5SumsFile : BuildTask
    {
        [Required]
        public string RootDirectory { get; set; }
        [Required]
        public ITaskItem[] Files { get; set; }
        [Required]
        public string OutputFile { get; set; }
 
        [Output]
        public string InstalledSize { get; set; }
 
        public override bool Execute()
        {
            using FileStream outputFile = File.OpenWrite(OutputFile);
            using StreamWriter writer = new(outputFile, Encoding.ASCII);
            ulong installedSize = 0;
            foreach (ITaskItem file in Files)
            {
                using MD5 md5 = MD5.Create();
                using FileStream fileStream = File.OpenRead(file.ItemSpec);
                installedSize += (ulong)fileStream.Length;
                byte[] hash = md5.ComputeHash(fileStream);
                string relativePath = file.ItemSpec.Substring(RootDirectory.Length).TrimStart(Path.DirectorySeparatorChar);
#if NET
                writer.WriteLine($"{Convert.ToHexString(hash)} {relativePath}");
#else
                writer.WriteLine($"{BitConverter.ToString(hash).Replace("-", "")} {relativePath}");
#endif
            }
 
            InstalledSize = installedSize.ToString();
 
            return true;
        }
    }
}