Advertisement
keem1

System.Security.Cryptography.Crc32.cs

Jun 27th, 2018
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4.  
  5. namespace System.Security.Cryptography
  6. {
  7.         /// <summary>
  8.         /// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
  9.         /// </summary>
  10.         /// <remarks>
  11.         /// Crc32 should only be used for backward compatibility with older file formats
  12.         /// and algorithms. It is not secure enough for new applications.
  13.         /// If you need to call multiple times for the same data either use the HashAlgorithm
  14.         /// interface or remember that the result of one Compute call needs to be ~ (XOR) before
  15.         /// being passed in as the seed for the next Compute call.
  16.         /// </remarks>
  17.         public sealed class Crc32 : HashAlgorithm
  18.         {
  19.                 public const UInt32 DefaultPolynomial = 0xedb88320u;
  20.                 public const UInt32 DefaultSeed = 0xffffffffu;
  21.  
  22.                 static UInt32[] defaultTable;
  23.  
  24.                 readonly UInt32 seed;
  25.                 readonly UInt32[] table;
  26.                 UInt32 hash;
  27.  
  28.                 public Crc32()
  29.                         : this(DefaultPolynomial, DefaultSeed)
  30.                 {
  31.                 }
  32.  
  33.                 public Crc32(UInt32 polynomial, UInt32 seed)
  34.                 {
  35.                         if (!BitConverter.IsLittleEndian)
  36.                                 throw new PlatformNotSupportedException("Not supported on Big Endian processors");
  37.  
  38.                         table = InitializeTable(polynomial);
  39.                         this.seed = hash = seed;
  40.                 }
  41.  
  42.                 public override void Initialize()
  43.                 {
  44.                         hash = seed;
  45.                 }
  46.  
  47.                 protected override void HashCore(byte[] array, int ibStart, int cbSize)
  48.                 {
  49.                         hash = CalculateHash(table, hash, array, ibStart, cbSize);
  50.                 }
  51.  
  52.                 protected override byte[] HashFinal()
  53.                 {
  54.                         var hashBuffer = UInt32ToBigEndianBytes(~hash);
  55.                         HashValue = hashBuffer;
  56.                         return hashBuffer;
  57.                 }
  58.  
  59.                 public override int HashSize { get { return 32; } }
  60.  
  61.                 public static UInt32 Compute(byte[] buffer)
  62.                 {
  63.                         return Compute(DefaultSeed, buffer);
  64.                 }
  65.  
  66.                 public static UInt32 Compute(UInt32 seed, byte[] buffer)
  67.                 {
  68.                         return Compute(DefaultPolynomial, seed, buffer);
  69.                 }
  70.  
  71.                 public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
  72.                 {
  73.                         return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
  74.                 }
  75.  
  76.                 static UInt32[] InitializeTable(UInt32 polynomial)
  77.                 {
  78.                         if (polynomial == DefaultPolynomial && defaultTable != null)
  79.                                 return defaultTable;
  80.  
  81.                         var createTable = new UInt32[256];
  82.                         for (var i = 0; i < 256; i++)
  83.                         {
  84.                                 var entry = (UInt32)i;
  85.                                 for (var j = 0; j < 8; j++)
  86.                                         if ((entry & 1) == 1)
  87.                                                 entry = (entry >> 1) ^ polynomial;
  88.                                         else
  89.                                                 entry = entry >> 1;
  90.                                 createTable[i] = entry;
  91.                         }
  92.  
  93.                         if (polynomial == DefaultPolynomial)
  94.                                 defaultTable = createTable;
  95.  
  96.                         return createTable;
  97.                 }
  98.  
  99.                 static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
  100.                 {
  101.                         var hash = seed;
  102.                         for (var i = start; i < start + size; i++)
  103.                                 hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
  104.                         return hash;
  105.                 }
  106.  
  107.                 static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
  108.                 {
  109.                         var result = BitConverter.GetBytes(uint32);
  110.  
  111.                         if (BitConverter.IsLittleEndian)
  112.                                 Array.Reverse(result);
  113.  
  114.                         return result;
  115.                 }
  116.         }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement