| 1 |
# (c) 2025 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
using namespace System.IO |
| 5 |
using namespace System.Reflection |
| 6 |
using namespace System.Security.Cryptography |
| 7 |
|
| 8 |
[CmdletBinding()] |
| 9 |
param ( |
| 10 |
[Parameter(Mandatory = $true)] |
| 11 |
[string] |
| 12 |
$Path, |
| 13 |
|
| 14 |
[Parameter(Mandatory, ValueFromPipeline)] |
| 15 |
[AllowEmptyString()] |
| 16 |
[string] |
| 17 |
$InputObject |
| 18 |
) |
| 19 |
|
| 20 |
begin { |
| 21 |
$fd = [File]::Create($Path) |
| 22 |
$algo = [SHA1]::Create() |
| 23 |
$bytes = @() |
| 24 |
|
| 25 |
$bindingFlags = [BindingFlags]'NonPublic, Instance' |
| 26 |
Function Get-Property { |
| 27 |
<# |
| 28 |
.SYNOPSIS |
| 29 |
Gets the private/internal property specified of the object passed in. |
| 30 |
#> |
| 31 |
Param ( |
| 32 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 33 |
[Object] |
| 34 |
$Object, |
| 35 |
|
| 36 |
[Parameter(Mandatory = $true, Position = 1)] |
| 37 |
[String] |
| 38 |
$Name |
| 39 |
) |
| 40 |
|
| 41 |
process { |
| 42 |
$Object.GetType().GetProperty($Name, $bindingFlags).GetValue($Object, $null) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
Function Set-Property { |
| 47 |
<# |
| 48 |
.SYNOPSIS |
| 49 |
Sets the private/internal property specified on the object passed in. |
| 50 |
#> |
| 51 |
Param ( |
| 52 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 53 |
[Object] |
| 54 |
$Object, |
| 55 |
|
| 56 |
[Parameter(Mandatory = $true, Position = 1)] |
| 57 |
[String] |
| 58 |
$Name, |
| 59 |
|
| 60 |
[Parameter(Mandatory = $true, Position = 2)] |
| 61 |
[AllowNull()] |
| 62 |
[Object] |
| 63 |
$Value |
| 64 |
) |
| 65 |
|
| 66 |
process { |
| 67 |
$Object.GetType().GetProperty($Name, $bindingFlags).SetValue($Object, $Value, $null) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
Function Get-Field { |
| 72 |
<# |
| 73 |
.SYNOPSIS |
| 74 |
Gets the private/internal field specified of the object passed in. |
| 75 |
#> |
| 76 |
Param ( |
| 77 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 78 |
[Object] |
| 79 |
$Object, |
| 80 |
|
| 81 |
[Parameter(Mandatory = $true, Position = 1)] |
| 82 |
[String] |
| 83 |
$Name |
| 84 |
) |
| 85 |
|
| 86 |
process { |
| 87 |
$Object.GetType().GetField($Name, $bindingFlags).GetValue($Object) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
# MaximumAllowedMemory is required to be set to so we can send input data that exceeds the limit on a PS |
| 92 |
# Runspace. We use reflection to access/set this property as it is not accessible publicly. This is not ideal |
| 93 |
# but works on all PowerShell versions I've tested with. We originally used WinRS to send the raw bytes to the |
| 94 |
# host but this falls flat if someone is using a custom PS configuration name so this is a workaround. This |
| 95 |
# isn't required for smaller files so if it fails we ignore the error and hope it wasn't needed. |
| 96 |
# https://github.com/PowerShell/PowerShell/blob/c8e72d1e664b1ee04a14f226adf655cced24e5f0/src/System.Management.Automation/engine/serialization.cs#L325 |
| 97 |
try { |
| 98 |
$Host | Get-Property 'ExternalHost' | |
| 99 |
Get-Field '_transportManager' | |
| 100 |
Get-Property 'Fragmentor' | |
| 101 |
Get-Property 'DeserializationContext' | |
| 102 |
Set-Property 'MaximumAllowedMemory' $null |
| 103 |
} |
| 104 |
catch { |
| 105 |
# Satisfy pslint, we purposefully ignore this error as it is not critical it works. |
| 106 |
$null = $null |
| 107 |
} |
| 108 |
} |
| 109 |
process { |
| 110 |
if ($InputObject) { |
| 111 |
$bytes = [Convert]::FromBase64String($InputObject) |
| 112 |
$algo.TransformBlock($bytes, 0, $bytes.Length, $bytes, 0) > $null |
| 113 |
$fd.Write($bytes, 0, $bytes.Length) |
| 114 |
} |
| 115 |
} |
| 116 |
end { |
| 117 |
$fd.Close() |
| 118 |
|
| 119 |
$algo.TransformFinalBlock($bytes, 0, 0) > $null |
| 120 |
$hash = [BitConverter]::ToString($algo.Hash).Replace('-', '').ToLowerInvariant() |
| 121 |
"{`"sha1`":`"$hash`"}" |
| 122 |
} |