private/Get-PublisherId.ps1
<#
.SYNOPSIS Calculates and returns a Publisher ID from a given publisher string. .DESCRIPTION The Get-PublisherId function calculates a Publisher ID from the provided publisher string. It performs the following steps: .PARAMETER Publisher Specifies the Publisher string for which to calculate the Publisher ID. .EXAMPLE PS C:\> $publisherString = "Contoso Corporation" PS C:\> Get-PublisherId -Publisher $publisherString PS C:\> tvqt2t7vp707e This example calculates and returns the Publisher ID for the specified publisher string. .NOTES Function : Get-PublisherId Author : John Billekens Copyright: Copyright (c) AppVentiX Version : 1.0 #> Function Get-PublisherId { [CmdletBinding()] Param ( [Parameter(Mandatory, ValueFromPipeLine, Position = 0)] [String]$Publisher ) Begin { $encUTF16LE = [system.Text.Encoding]::Unicode $encSha256 = [System.Security.Cryptography.HashAlgorithm]::Create("SHA256") } Process { # Convert to UTF16 Little Endian $utf16LE = $encUTF16LE.GetBytes($Publisher) # Calculate SHA256 hash on UTF16LE Byte array. Store first 8 bytes in new Byte Array $bytes = @() (($encSha256.ComputeHasH($utf16LE))[0..7]) | ForEach-Object { $bytes += '{0:x2}' -f $_ } # Convert Byte Array to Binary string; Adding padding zeros on end to it has 13*5 bytes $bytesAsBinaryString = -join $bytes.ForEach{ [convert]::tostring([convert]::ToByte($_, 16), 2).padleft(8, '0') } $bytesAsBinaryString = $bytesAsBinaryString.PadRight(65, '0') # Crockford Base32 encode. Read each 5 bits; convert to decimal. Lookup position in lookup table $coded = $null For ($i = 0; $i -lt (($bytesAsBinaryString.Length)); $i += 5) { $string = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" [int]$Int = [convert]::Toint32($bytesAsBinaryString.Substring($i, 5), 2) $coded += $string.Substring($Int, 1) } Write-Output ($coded.tolower()) } End { Clear-Variable Publisher, utf16LE, bytes, bytesAsBinaryString, coded } } |