Guest User

Powershell CRC32 String

a guest
Nov 6th, 2015
1,835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -TypeDefinition @"
  2. // Copyright (c) Damien Guard.  All rights reserved.
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  5. // Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Security.Cryptography;
  10.  
  11. /// <summary>
  12. /// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
  13. /// </summary>
  14. /// <remarks>
  15. /// Crc32 should only be used for backward compatibility with older file formats
  16. /// and algorithms. It is not secure enough for new applications.
  17. /// If you need to call multiple times for the same data either use the HashAlgorithm
  18. /// interface or remember that the result of one Compute call needs to be ~ (XOR) before
  19. /// being passed in as the seed for the next Compute call.
  20. /// </remarks>
  21. public sealed class Crc32 : HashAlgorithm
  22. {
  23.    public const UInt32 DefaultPolynomial = 0xedb88320u;
  24.    public const UInt32 DefaultSeed = 0xffffffffu;
  25.  
  26.    static UInt32[] defaultTable;
  27.  
  28.    readonly UInt32 seed;
  29.    readonly UInt32[] table;
  30.    UInt32 hash;
  31.  
  32.    public Crc32()
  33.        : this(DefaultPolynomial, DefaultSeed)
  34.    {
  35.    }
  36.  
  37.    public Crc32(UInt32 polynomial, UInt32 seed)
  38.    {
  39.        table = InitializeTable(polynomial);
  40.        this.seed = hash = seed;
  41.    }
  42.  
  43.    public override void Initialize()
  44.    {
  45.        hash = seed;
  46.    }
  47.  
  48.    protected override void HashCore(byte[] array, int ibStart, int cbSize)
  49.    {
  50.        hash = CalculateHash(table, hash, array, ibStart, cbSize);
  51.    }
  52.  
  53.    protected override byte[] HashFinal()
  54.    {
  55.        var hashBuffer = UInt32ToBigEndianBytes(~hash);
  56.        HashValue = hashBuffer;
  57.        return hashBuffer;
  58.    }
  59.  
  60.    public override int HashSize { get { return 32; } }
  61.        
  62.    public static UInt32 Compute(byte[] buffer)
  63.    {
  64.        return Compute(DefaultSeed, buffer);
  65.    }
  66.  
  67.    public static UInt32 Compute(UInt32 seed, byte[] buffer)
  68.    {
  69.        return Compute(DefaultPolynomial, seed, buffer);
  70.    }
  71.  
  72.    public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  73.    {
  74.        return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  75.    }
  76.  
  77.    static UInt32[] InitializeTable(UInt32 polynomial)
  78.    {
  79.        if (polynomial == DefaultPolynomial && defaultTable != null)
  80.            return defaultTable;
  81.  
  82.        var createTable = new UInt32[256];
  83.        for (var i = 0; i < 256; i++)
  84.        {
  85.            var entry = (UInt32)i;
  86.            for (var j = 0; j < 8; j++)
  87.                if ((entry & 1) == 1)
  88.                    entry = (entry >> 1) ^ polynomial;
  89.                else
  90.                    entry = entry >> 1;
  91.            createTable[i] = entry;
  92.        }
  93.  
  94.        if (polynomial == DefaultPolynomial)
  95.            defaultTable = createTable;
  96.  
  97.        return createTable;
  98.    }
  99.  
  100.    static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
  101.    {
  102.        var crc = seed;
  103.        for (var i = start; i < size - start; i++)
  104.            crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
  105.        return crc;
  106.    }
  107.  
  108.    static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
  109.    {
  110.        var result = BitConverter.GetBytes(uint32);
  111.  
  112.        if (BitConverter.IsLittleEndian)
  113.            Array.Reverse(result);
  114.  
  115.        return result;
  116.    }
  117. }
  118. "@ -PassThru | Out-Null
  119.  
  120. Function Get-Crc32
  121. {
  122.     Param (
  123.         [Parameter(Mandatory=$true)][String]$String
  124.     )
  125.     $crc32 = New-Object Crc32
  126.     $encoding = [System.Text.Encoding]::UTF8
  127.  
  128.     $bytes = $encoding.GetBytes($String)
  129.    
  130.     $hash = [String]::Empty
  131.     foreach ($byte in $crc32.ComputeHash($bytes))
  132.     {
  133.         $hash += $byte.toString('x2').toLower()
  134.     }
  135.  
  136.     $hash
  137. }
Advertisement
Add Comment
Please, Sign In to add comment