| title | GetFileHash Task | |||
|---|---|---|---|---|
| description | Learn to use the MSBuild GetFileHash task to compute checksums of the contents of a file or set of files. | |||
| ms.date | 09/15/2023 | |||
| ms.topic | reference | |||
| dev_langs |
|
|||
| helpviewer_keywords |
|
|||
| author | ghogen | |||
| ms.author | ghogen | |||
| ms.subservice | msbuild |
Computes checksums of the contents of a file or set of files.
This task was added in 15.8, but requires a workaround to use for MSBuild versions below 16.0.
The following table describes the parameters of the GetFileHash task.
| Parameter | Description |
|---|---|
Files |
Required xref:Microsoft.Build.Framework.ITaskItem[] parameter.The files to be hashed. |
Items |
xref:Microsoft.Build.Framework.ITaskItem[] output parameter.The Files input with additional metadata set to the file hash. |
Hash |
String output parameter.The hash of the file. This output is only set if there was exactly one item passed in. |
Algorithm |
Optional String parameter.The algorithm. Allowed values: SHA256, SHA384, SHA512. Default = SHA256. |
MetadataName |
Optional String parameter.The metadata name where the hash is stored in each item. Defaults to FileHash. |
HashEncoding |
Optional String parameter.The encoding to use for generated hashes. Defaults to hex. Allowed values = hex, base64. |
The following example uses the GetFileHash task to determine and print the checksum of the FilesToHash items.
<Project>
<ItemGroup>
<FilesToHash Include="$(MSBuildThisFileDirectory)\*" />
</ItemGroup>
<Target Name="GetHash">
<GetFileHash Files="@(FilesToHash)">
<Output
TaskParameter="Items"
ItemName="FilesWithHashes" />
</GetFileHash>
<Message Importance="High"
Text="@(FilesWithHashes->'%(Identity): %(FileHash)')" />
</Target>
</Project>With a single file, you can use the Hash output parameter. The following example project is named hash-example.proj and computes a hash for itself:
<Project>
<ItemGroup>
<FileToHash Include="$(MSBuildThisFileDirectory)hash-example.proj" />
</ItemGroup>
<Target Name="GetHash">
<GetFileHash Files="@(FileToHash)">
<Output
TaskParameter="Hash"
ItemName="FileHash" />
</GetFileHash>
<Message Importance="High"
Text="File: @(FileToHash) Hash: @(FileHash)" />
</Target>
</Project>