Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# gkostoulias(at)gmail.com
- 'ps.binhash2.ps1' Powershell BYTE/ACSII Hashing Function #>
- Add-Type -TypeDefinition @"
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- public sealed class Crc32 : HashAlgorithm
- {
- public const UInt32 DefaultPolynomial = 0xedb88320u;
- public const UInt32 DefaultSeed = 0xffffffffu;
- static UInt32[] defaultTable;
- readonly UInt32 seed;
- readonly UInt32[] table;
- UInt32 hash;
- public Crc32()
- : this(DefaultPolynomial, DefaultSeed)
- {
- }
- public Crc32(UInt32 polynomial, UInt32 seed)
- {
- table = InitializeTable(polynomial);
- this.seed = hash = seed;
- }
- public override void Initialize()
- {
- hash = seed;
- }
- protected override void HashCore(byte[] array, int ibStart, int cbSize)
- {
- hash = CalculateHash(table, hash, array, ibStart, cbSize);
- }
- protected override byte[] HashFinal()
- {
- var hashBuffer = UInt32ToBigEndianBytes(~hash);
- HashValue = hashBuffer;
- return hashBuffer;
- }
- public override int HashSize { get { return 32; } }
- public static UInt32 Compute(byte[] buffer)
- {
- return Compute(DefaultSeed, buffer);
- }
- public static UInt32 Compute(UInt32 seed, byte[] buffer)
- {
- return Compute(DefaultPolynomial, seed, buffer);
- }
- public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
- {
- return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
- }
- static UInt32[] InitializeTable(UInt32 polynomial)
- {
- if (polynomial == DefaultPolynomial && defaultTable != null)
- return defaultTable;
- var createTable = new UInt32[256];
- for (var i = 0; i < 256; i++)
- {
- var entry = (UInt32)i;
- for (var j = 0; j < 8; j++)
- if ((entry & 1) == 1)
- entry = (entry >> 1) ^ polynomial;
- else
- entry = entry >> 1;
- createTable[i] = entry;
- }
- if (polynomial == DefaultPolynomial)
- defaultTable = createTable;
- return createTable;
- }
- static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
- {
- var crc = seed;
- for (var i = start; i < size - start; i++)
- crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
- return crc;
- }
- static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
- {
- var result = BitConverter.GetBytes(uint32);
- if (BitConverter.IsLittleEndian)
- Array.Reverse(result);
- return result;
- }
- }
- "@ -PassThru | out-null
- function ntlmHash
- {
- [CmdletBinding()]
- Param ([Parameter(Mandatory=$True, ValueFromPipeline=$False)]
- [Byte[]]$DataToHash)
- END
- {
- Set-StrictMode -Version Latest
- Add-Type -TypeDefinition @'
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
- public class BCrypt
- {
- [DllImport("bcrypt.dll", CharSet = CharSet.Auto)]
- public static extern NTStatus BCryptOpenAlgorithmProvider(
- [Out] out IntPtr phAlgorithm,
- [In] string pszAlgId,
- [In, Optional] string pszImplementation,
- [In] UInt32 dwFlags);
- [DllImport("bcrypt.dll")]
- public static extern NTStatus BCryptCloseAlgorithmProvider(
- [In, Out] IntPtr hAlgorithm,
- [In] UInt32 dwFlags);
- [DllImport("bcrypt.dll", CharSet = CharSet.Auto)]
- public static extern NTStatus BCryptCreateHash(
- [In, Out] IntPtr hAlgorithm,
- [Out] out IntPtr phHash,
- [Out] IntPtr pbHashObject,
- [In, Optional] UInt32 cbHashObject,
- [In, Optional] IntPtr pbSecret,
- [In] UInt32 cbSecret,
- [In] UInt32 dwFlags);
- [DllImport("bcrypt.dll")]
- public static extern NTStatus BCryptDestroyHash(
- [In, Out] IntPtr hHash);
- [DllImport("bcrypt.dll")]
- public static extern NTStatus BCryptHashData(
- [In, Out] IntPtr hHash,
- [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput,
- [In] int cbInput,
- [In] UInt32 dwFlags);
- [DllImport("bcrypt.dll")]
- public static extern NTStatus BCryptFinishHash(
- [In, Out] IntPtr hHash,
- [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput,
- [In] int cbInput,
- [In] UInt32 dwFlags);
- [Flags]
- public enum AlgOpsFlags : uint
- {
- BCRYPT_PROV_DISPATCH = 0x00000001,
- BCRYPT_ALG_HANDLE_HMAC_FLAG = 0x00000008,
- BCRYPT_HASH_REUSABLE_FLAG = 0x00000020
- }
- public enum NTStatus : uint
- {
- STATUS_SUCCESS = 0x00000000
- }
- }
- '@
- [Byte[]]$HashBytes = New-Object Byte[] 16
- [IntPtr]$PHAlgorithm = [IntPtr]::Zero
- [IntPtr]$PHHash = [IntPtr]::Zero
- $NTStatus = [BCrypt]::BCryptOpenAlgorithmProvider([Ref] $PHAlgorithm, 'MD4', $Null, 0)
- if ($NTStatus -NE 0)
- {
- write-warning "BCryptOpenAlgorithmProvider failed with NTSTATUS $NTStatus"
- if ($PHAlgorithm -NE [IntPtr]::Zero)
- {
- $NTStatus = [BCrypt]::BCryptCloseAlgorithmProvider($PHAlgorithm, 0)
- }
- return
- }
- $NTStatus = [BCrypt]::BCryptCreateHash($PHAlgorithm, [Ref] $PHHash, [IntPtr]::Zero, 0, [IntPtr]::Zero, 0, 0)
- if ($NTStatus -NE 0)
- {
- write-warning "BCryptCreateHash failed with NTSTATUS $NTStatus"
- if ($PHHash -NE [IntPtr]::Zero)
- {
- $NTStatus = [BCrypt]::BCryptDestroyHash($PHHash)
- }
- if ($PHAlgorithm -NE [IntPtr]::Zero)
- {
- $NTStatus = [BCrypt]::BCryptCloseAlgorithmProvider($PHAlgorithm, 0)
- }
- return
- }
- $NTStatus = [BCrypt]::BCryptHashData($PHHash, $DataToHash, $DataToHash.Length, 0)
- $NTStatus = [BCrypt]::BCryptFinishHash($PHHash, $HashBytes, $HashBytes.Length, 0)
- if ($phhash -ne [intptr]::zero)
- {
- $ntstatus = [bcrypt]::bcryptdestroyhash($phhash)
- }
- if ($phalgorithm -ne [intptr]::zero)
- {
- $ntstatus = [bcrypt]::bcryptclosealgorithmprovider($phalgorithm, 0)
- }
- $hashstring = new-object system.text.stringbuilder
- foreach ($byte in $hashbytes)
- {
- [void]$hashstring.append($byte.tostring("x2"))
- }
- return ($hashstring.tostring()).tolower()
- }
- }
- function crc32Hash
- {
- [cmdletbinding()]
- param (
- [parameter( mandatory=$true, valuefrompipeline=$true )]
- [string]$1
- )
- $crc32 = new-object crc32
- $encoding = [system.text.encoding]::utf8
- $bytes = $encoding.getbytes($1)
- $hash = [string]::empty
- foreach ($byte in $crc32.computehash($bytes))
- {
- $hash += $byte.tostring('x2').tolower()
- }
- return ($hash)
- }
- function BinHash
- {
- [cmdletbinding()]
- param (
- [parameter( mandatory=$true, valuefrompipeline=$true )]
- [string]$1,
- [parameter( mandatory=$true, valuefrompipeline=$true )]
- [string]$2
- )
- $algorithm = @(
- "MD5",
- "RIPEMD160",
- "SHA1",
- "SHA256",
- "SHA384",
- "SHA512"
- )
- if ($algorithm -notcontains $1)
- {
- write-warning "Algorithm $1 not supported."
- exit
- }
- try {
- $bin =@()
- $bin += $2 -split "(..)" | where-object {$_} | foreach {[byte][char]([convert]::toint16($_,16))}
- }
- catch {
- $bin =@()
- $bin += ([system.text.encoding]::ascii.getbytes($2))
- }
- $x = new-object system.text.stringbuilder
- [system.security.cryptography.hashalgorithm]::create($1).computehash($bin)|%{[void]$x.append($_.tostring("x2"))
- }
- $bin.clear()
- return ($x.tostring())
- }
- if ($args.count -lt 2 -OR $args -match '\?')
- {
- write `n`0'Usage: binhash [CRC32/MD5/NTLMv1/SHA(1,256,384,512)/RIPEMD160] ["string"] (iterations)'
- write-host `t'bihhash will treat all hex strings as int16 byte arrays' -f darkyellow
- exit
- }
- if (!($args[2]))
- {
- if ($args[0] -eq 'ntlm')
- {
- $x = $args[1]
- ntlmHash -datatohash $([text.encoding]::unicode.getbytes("$x"))
- exit
- }
- if ($args[0] -eq 'crc32')
- {
- $args[1] | crc32Hash
- exit
- }
- $args[1] | BinHash $args[0]
- }
- else {
- $x = $args[1]
- while ($i -lt $args[2])
- {
- $i++
- $x = BinHash $args[0] $x
- write-host "$x x$i" `r -n;
- }
- write `r
- }
- remove-variable * -erroraction silentlycontinue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement