usr/Get-Entropy.ps1

Set-Alias -Name ent -Value Get-Entropy
function Get-Entropy {
  <#
    .SYNOPSIS
        Computes entropy of a file or bytes sequence.
    .DESCRIPTION
        The function is primarily focused on calculating the entropy of huge files,
        but can be used for same purpose for bytes.
    .PARAMETER Path
        Specifies an item path.
    .PARAMETER Bytes
        Specifies a set of bytes.
    .EXAMPLE
        Get-Entropy C:\samples\target.bin
    .EXAMPLE
        Get-Entropy -Bytes ([IO.File]::ReadAllBytes('C:\samples\target.bin'))
    .INPUTS
        System.String
        System.Array
    .OUTPUTS
        System.Double
  #>

  [CmdletBinding(DefaultParameterSetName='Path')]
  param(
    [Parameter(Mandatory, Position=0, ParameterSetName='Path')]
    [ValidateScript({!!($script:file = Convert-Path -Path $_ -ErrorAction 0)})]
    [ValidateNotNullOrEmpty()]
    [String]$Path,

    [Parameter(Mandatory, Position=0, ParameterSetName='Bytes')]
    [ValidateNotNullOrEmpty()]
    [Byte[]]$Bytes
  )

  end {
    $x = New-ILMethod -Noun $MyInvocation.MyCommand.Noun -Variables {
      Int32  $i
      Int32* $rng
      Int32* $pi
      Double $ent
      Double $src
    } -ReturnType ([Double]) -Parameters ([Byte[]]) -Code @'
      ldc_i4, 0x100
      conv_u
      ldc_i4_4
      mul_ovf_un
      localloc
      stloc_1
      ldloc_1
      ldc_i4, 0x400
      conv_i
      add
      stloc_2
      ldc_r8, 0.0
      stloc_3
      ldarg_0
      ldlen
      conv_i4
      dup
      stloc_0
      conv_r8
      stloc_s, $src
      br_s, :L_0
      :L_1 # 0x28
      ldloc_1
      ldarg_0
      ldloc_0
      ldelem_u1
      conv_i
      ldc_i4_4
      mul
      add
      dup
      ldind_i4
      ldc_i4_1
      add
      stind_i4
      :L_0 # 0x35
      ldloc_0
      ldc_i4_1
      sub
      dup
      stloc_0
      ldc_i4_0
      bge_s, :L_1
      br_s, :L_2
      :L_3 # 0x3f
      ldloc_2
      ldind_i4
      ldc_i4_0
      ble_s, :L_2
      ldloc_3
      ldloc_2
      ldind_i4
      conv_r8
      ldloc_2
      ldind_i4
      conv_r8
      ldloc_s, $src
      div
      ldc_r8, 2.
      call, [Math].GetMethod('Log', [Type[]]([Double], [Double]))
      mul
      add
      stloc_3
      :L_2 # 0x5f
      ldloc_2
      ldc_i4_4
      conv_i
      sub
      dup
      stloc_2
      ldloc_1
      bge_un_s, :L_3
      ldloc_3
      neg
      ldloc_s, $src
      div
      ret
'@

    try {
      $x.Invoke($PSCmdlet.ParameterSetName -eq 'Path' ?
        [IO.File]::ReadAllBytes(($Path = $file)) : $Bytes
      )
    }
    catch { Write-Verbose $_ }
  }
}

Export-ModuleMember -Alias ent -Function Get-Entropy