Private/Share/ConvertTo-SecretSharingBigIntegerFromByte.ps1
|
function ConvertTo-SecretSharingBigIntegerFromByte { <# .SYNOPSIS Converts a big-endian, unsigned byte array to a BigInteger. .DESCRIPTION [System.Numerics.BigInteger]'s only constructor available on both Windows PowerShell 5.1 (.NET Framework) and PowerShell 7 (.NET) is little-endian and signed - the isBigEndian/isUnsigned overloads were only added in .NET Core 3.0. This reverses the byte order and appends a zero byte when the most-significant input byte's high bit is set, so the value is always read back as the intended non-negative magnitude on either runtime. #> [CmdletBinding()] [OutputType([System.Numerics.BigInteger])] param( [Parameter(Mandatory)] [AllowEmptyCollection()] [byte[]]$Byte ) if ($Byte.Count -eq 0) { return [System.Numerics.BigInteger]::Zero } $littleEndian = [byte[]]$Byte.Clone() [array]::Reverse($littleEndian) if (($littleEndian[$littleEndian.Count - 1] -band 0x80) -ne 0) { $littleEndian = [byte[]]($littleEndian + [byte[]](0x00)) } return [System.Numerics.BigInteger]::new($littleEndian) } |