daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# gkostoulias(at)gmail.com
  2. 'ps.binhash2.ps1' Powershell BYTE/ACSII Hashing Function #>
  3.  
  4.  
  5.  Add-Type -TypeDefinition @"
  6.  using System;
  7.  using System.Collections.Generic;
  8.  using System.Security.Cryptography;
  9.  
  10.  public sealed class Crc32 : HashAlgorithm
  11.  {
  12.     public const UInt32 DefaultPolynomial = 0xedb88320u;
  13.     public const UInt32 DefaultSeed = 0xffffffffu;
  14.  
  15.     static UInt32[] defaultTable;
  16.  
  17.     readonly UInt32 seed;
  18.     readonly UInt32[] table;
  19.     UInt32 hash;
  20.  
  21.     public Crc32()
  22.         : this(DefaultPolynomial, DefaultSeed)
  23.     {
  24.     }
  25.     public Crc32(UInt32 polynomial, UInt32 seed)
  26.     {
  27.         table = InitializeTable(polynomial);
  28.         this.seed = hash = seed;
  29.     }
  30.     public override void Initialize()
  31.     {
  32.         hash = seed;
  33.     }
  34.     protected override void HashCore(byte[] array, int ibStart, int cbSize)
  35.     {
  36.         hash = CalculateHash(table, hash, array, ibStart, cbSize);
  37.     }
  38.     protected override byte[] HashFinal()
  39.     {
  40.         var hashBuffer = UInt32ToBigEndianBytes(~hash);
  41.         HashValue = hashBuffer;
  42.         return hashBuffer;
  43.     }
  44.     public override int HashSize { get { return 32; } }
  45.        
  46.     public static UInt32 Compute(byte[] buffer)
  47.     {
  48.         return Compute(DefaultSeed, buffer);
  49.     }
  50.     public static UInt32 Compute(UInt32 seed, byte[] buffer)
  51.     {
  52.         return Compute(DefaultPolynomial, seed, buffer);
  53.     }
  54.     public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  55.     {
  56.         return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  57.     }
  58.     static UInt32[] InitializeTable(UInt32 polynomial)
  59.     {
  60.         if (polynomial == DefaultPolynomial && defaultTable != null)
  61.             return defaultTable;
  62.  
  63.         var createTable = new UInt32[256];
  64.         for (var i = 0; i < 256; i++)
  65.         {
  66.             var entry = (UInt32)i;
  67.             for (var j = 0; j < 8; j++)
  68.                 if ((entry & 1) == 1)
  69.                     entry = (entry >> 1) ^ polynomial;
  70.                 else
  71.                     entry = entry >> 1;
  72.             createTable[i] = entry;
  73.         }
  74.         if (polynomial == DefaultPolynomial)
  75.             defaultTable = createTable;
  76.  
  77.         return createTable;
  78.     }
  79.     static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
  80.     {
  81.         var crc = seed;
  82.         for (var i = start; i < size - start; i++)
  83.             crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
  84.         return crc;
  85.     }
  86.     static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
  87.     {
  88.         var result = BitConverter.GetBytes(uint32);
  89.  
  90.         if (BitConverter.IsLittleEndian)
  91.             Array.Reverse(result);
  92.  
  93.         return result;
  94.     }
  95.  }
  96. "@ -PassThru | out-null
  97.  
  98.  
  99. function ntlmHash
  100.     {
  101.     [CmdletBinding()]
  102.     Param ([Parameter(Mandatory=$True, ValueFromPipeline=$False)]          
  103.     [Byte[]]$DataToHash)
  104.     END
  105.     {        
  106.         Set-StrictMode -Version Latest
  107.         Add-Type -TypeDefinition @'
  108.         using System;
  109.         using System.Text;
  110.         using System.Runtime.InteropServices;
  111.         public class BCrypt
  112.     {
  113.     [DllImport("bcrypt.dll", CharSet = CharSet.Auto)]
  114.     public static extern NTStatus BCryptOpenAlgorithmProvider(
  115.         [Out] out IntPtr phAlgorithm,
  116.         [In] string pszAlgId,
  117.         [In, Optional] string pszImplementation,
  118.         [In] UInt32 dwFlags);
  119.  
  120.     [DllImport("bcrypt.dll")]
  121.     public static extern NTStatus BCryptCloseAlgorithmProvider(
  122.         [In, Out] IntPtr hAlgorithm,
  123.         [In] UInt32 dwFlags);
  124.  
  125.     [DllImport("bcrypt.dll", CharSet = CharSet.Auto)]
  126.     public static extern NTStatus BCryptCreateHash(
  127.         [In, Out] IntPtr hAlgorithm,
  128.         [Out] out IntPtr phHash,
  129.         [Out] IntPtr pbHashObject,
  130.         [In, Optional] UInt32 cbHashObject,
  131.         [In, Optional] IntPtr pbSecret,
  132.         [In] UInt32 cbSecret,
  133.         [In] UInt32 dwFlags);
  134.  
  135.     [DllImport("bcrypt.dll")]
  136.     public static extern NTStatus BCryptDestroyHash(
  137.         [In, Out] IntPtr hHash);
  138.  
  139.     [DllImport("bcrypt.dll")]
  140.     public static extern NTStatus BCryptHashData(
  141.         [In, Out] IntPtr hHash,
  142.         [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput,
  143.         [In] int cbInput,
  144.         [In] UInt32 dwFlags);
  145.  
  146.     [DllImport("bcrypt.dll")]
  147.     public static extern NTStatus BCryptFinishHash(
  148.         [In, Out] IntPtr hHash,
  149.         [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput,
  150.         [In] int cbInput,
  151.         [In] UInt32 dwFlags);
  152.  
  153.     [Flags]
  154.         public enum AlgOpsFlags : uint
  155.         {            
  156.         BCRYPT_PROV_DISPATCH = 0x00000001,
  157.         BCRYPT_ALG_HANDLE_HMAC_FLAG = 0x00000008,
  158.         BCRYPT_HASH_REUSABLE_FLAG = 0x00000020
  159.     }
  160.     public enum NTStatus : uint
  161.     {
  162.     STATUS_SUCCESS = 0x00000000
  163.     }
  164.  }
  165. '@
  166.  
  167.     [Byte[]]$HashBytes   = New-Object Byte[] 16
  168.     [IntPtr]$PHAlgorithm = [IntPtr]::Zero
  169.     [IntPtr]$PHHash      = [IntPtr]::Zero
  170.  
  171.     $NTStatus = [BCrypt]::BCryptOpenAlgorithmProvider([Ref] $PHAlgorithm, 'MD4', $Null, 0)
  172.     if ($NTStatus -NE 0)
  173.         {
  174.         write-warning "BCryptOpenAlgorithmProvider failed with NTSTATUS $NTStatus"
  175.         if ($PHAlgorithm -NE [IntPtr]::Zero)
  176.             {
  177.             $NTStatus = [BCrypt]::BCryptCloseAlgorithmProvider($PHAlgorithm, 0)
  178.         }
  179.  
  180.     return
  181.     }
  182.  
  183.     $NTStatus = [BCrypt]::BCryptCreateHash($PHAlgorithm, [Ref] $PHHash, [IntPtr]::Zero, 0, [IntPtr]::Zero, 0, 0)
  184.     if ($NTStatus -NE 0)
  185.         {
  186.         write-warning "BCryptCreateHash failed with NTSTATUS $NTStatus"
  187.         if ($PHHash -NE [IntPtr]::Zero)
  188.             {
  189.             $NTStatus = [BCrypt]::BCryptDestroyHash($PHHash)                
  190.         }
  191.         if ($PHAlgorithm -NE [IntPtr]::Zero)
  192.             {
  193.             $NTStatus = [BCrypt]::BCryptCloseAlgorithmProvider($PHAlgorithm, 0)
  194.         }
  195.  
  196.     return
  197.     }
  198.  
  199.     $NTStatus = [BCrypt]::BCryptHashData($PHHash, $DataToHash, $DataToHash.Length, 0)
  200.     $NTStatus = [BCrypt]::BCryptFinishHash($PHHash, $HashBytes, $HashBytes.Length, 0)
  201.  
  202.     if ($phhash -ne [intptr]::zero)
  203.         {
  204.         $ntstatus = [bcrypt]::bcryptdestroyhash($phhash)
  205.     }
  206.     if ($phalgorithm -ne [intptr]::zero)
  207.         {
  208.         $ntstatus = [bcrypt]::bcryptclosealgorithmprovider($phalgorithm, 0)
  209.     }
  210.  
  211.     $hashstring = new-object system.text.stringbuilder
  212.  
  213.     foreach ($byte in $hashbytes)
  214.         {
  215.         [void]$hashstring.append($byte.tostring("x2"))
  216.     }
  217.  
  218.     return ($hashstring.tostring()).tolower()
  219.     }
  220.  }
  221.  
  222.  
  223.  function crc32Hash
  224.     {
  225.     [cmdletbinding()]
  226.     param (
  227.         [parameter( mandatory=$true, valuefrompipeline=$true )]
  228.         [string]$1
  229.     )
  230.     $crc32 = new-object crc32
  231.     $encoding = [system.text.encoding]::utf8
  232.     $bytes = $encoding.getbytes($1)
  233.  
  234.     $hash = [string]::empty
  235.     foreach ($byte in $crc32.computehash($bytes))
  236.         {
  237.         $hash += $byte.tostring('x2').tolower()
  238.     }
  239.  
  240.  return ($hash)
  241.  }
  242.  
  243.  
  244.  function BinHash
  245.     {
  246.     [cmdletbinding()]
  247.     param (
  248.         [parameter( mandatory=$true, valuefrompipeline=$true )]
  249.         [string]$1,
  250.         [parameter( mandatory=$true, valuefrompipeline=$true )]
  251.         [string]$2
  252.     )
  253.     $algorithm = @(
  254.         "MD5",
  255.         "RIPEMD160",
  256.         "SHA1",
  257.         "SHA256",
  258.         "SHA384",
  259.         "SHA512"
  260.     )
  261.     if ($algorithm -notcontains $1)
  262.         {
  263.         write-warning "Algorithm $1 not supported."
  264.         exit
  265.     }
  266.     try {
  267.         $bin =@()
  268.         $bin += $2 -split "(..)" | where-object {$_} | foreach {[byte][char]([convert]::toint16($_,16))}
  269.     }
  270.     catch {
  271.         $bin =@()
  272.         $bin += ([system.text.encoding]::ascii.getbytes($2))
  273.     }
  274.     $x = new-object system.text.stringbuilder
  275.     [system.security.cryptography.hashalgorithm]::create($1).computehash($bin)|%{[void]$x.append($_.tostring("x2"))
  276.     }
  277.     $bin.clear()
  278.  
  279.  return ($x.tostring())
  280.  }
  281.  
  282.  
  283.  if ($args.count -lt 2 -OR $args -match '\?')
  284.     {
  285.     write `n`0'Usage: binhash [CRC32/MD5/NTLMv1/SHA(1,256,384,512)/RIPEMD160] ["string"] (iterations)'
  286.     write-host `t'bihhash will treat all hex strings as int16 byte arrays' -f darkyellow
  287.     exit
  288.  }
  289.  
  290.  if (!($args[2]))
  291.     {
  292.     if ($args[0] -eq 'ntlm')
  293.         {
  294.         $x = $args[1]
  295.         ntlmHash -datatohash $([text.encoding]::unicode.getbytes("$x"))
  296.         exit
  297.     }
  298.     if ($args[0] -eq 'crc32')
  299.         {
  300.         $args[1] | crc32Hash
  301.         exit
  302.     }
  303.     $args[1] | BinHash $args[0]
  304.  }
  305.  else {
  306.     $x = $args[1]
  307.     while ($i -lt $args[2])
  308.         {
  309.         $i++
  310.         $x = BinHash $args[0] $x
  311.         write-host "$x x$i" `r -n;
  312.     }
  313.  write `r
  314.  }
  315.  
  316.  remove-variable * -erroraction silentlycontinue
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top