Results 1 to 2 of 2

Thread: VB.Net: CRC32 HashAlgorithm

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    VB.Net: CRC32 HashAlgorithm

    After searching around for a few hours (looking for different things at the same time), I gave up on trying to find a CRC32 implementation that Inherited from System.Security.Cryptography.HashAlgorithm

    So I wrote one based on one of the other examples, too lazy to figure out the rfc documentation.

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Security.Cryptography
    5.  
    6. Namespace xous.Security.Cryptography
    7.  
    8.     Public Class CRC32
    9.         Inherits HashAlgorithm
    10.  
    11.         Private Const _DefaultPolynomial As Integer = &HEDB88320
    12.  
    13. #Region " Member Variables "
    14.         Private _Table() As Integer
    15.         Private _CRC32 As Integer = &HFFFFFFFF
    16.         Private _Polynomial As Integer
    17. #End Region
    18.  
    19. #Region " Contructors "
    20.         Public Sub New()
    21.  
    22.             Me.HashSizeValue = 32 ' CRC32 is a 32bit hash
    23.  
    24.             _Polynomial = _DefaultPolynomial
    25.  
    26.             Initialize()
    27.  
    28.         End Sub
    29.  
    30.         Public Sub New(ByVal Polynomial As Integer)
    31.             _Polynomial = Polynomial
    32.         End Sub
    33.  
    34. #End Region
    35.  
    36. #Region " HashAlgorithm "
    37.  
    38.         Protected Overrides Sub HashCore(ByVal array() As Byte, ByVal ibStart As Integer, ByVal cbSize As Integer)
    39.  
    40.             Dim intLookup As Integer
    41.             For i As Integer = 0 To cbSize - 1
    42.                 intLookup = (_CRC32 And &HFF) Xor array(i)
    43.                 'This is a workaround for a right bit-shift because vb.net
    44.                 'does not support unsigned Integers, so _CRC32 >> 8
    45.                 'gives the wrong value (any better fixes?)
    46.                 _CRC32 = ((_CRC32 And &HFFFFFF00) \ &H100) And &HFFFFFF
    47.                 _CRC32 = _CRC32 Xor _Table(intLookup)
    48.             Next i
    49.         End Sub
    50.  
    51.         Protected Overrides Function HashFinal() As Byte()
    52.             Return BitConverter.GetBytes(Not _CRC32)
    53.         End Function
    54.  
    55.         Public Overrides Sub Initialize()
    56.             _CRC32 = &HFFFFFFFF
    57.             _Table = BuildTable(_Polynomial)
    58.         End Sub
    59.  
    60.  
    61. #End Region
    62.  
    63. #Region " Helper Methods "
    64.         ''' <summary>
    65.         ''' Generates the CRC32 Table
    66.         ''' </summary>
    67.         ''' <param name="Polynomial">A polynomial that should be used to generate the table.</param>
    68.         ''' <returns>The CRC32 Table based on the Polynomial given</returns>
    69.         Private Shared Function BuildTable(ByVal Polynomial As Integer) As Integer()
    70.             Dim Table(255) As Integer
    71.  
    72.             Dim Value As Integer
    73.  
    74.             For I As Integer = 0 To 255
    75.                 Value = I
    76.                 For X As Integer = 0 To 7
    77.                     If (Value And 1) = 1 Then
    78.                         'This is a workaround for a right bit-shift because vb.net
    79.                         'does not support unsigned Integers, so _CRC32 >> 1
    80.                         'gives the wrong value (any better fixes?)
    81.                         Value = Convert.ToInt32(((Value And &HFFFFFFFE) \ 2&) And &H7FFFFFFF)
    82.                         Value = Value Xor Polynomial
    83.                     Else
    84.                         'Same as above.
    85.                         Value = Convert.ToInt32(((Value And &HFFFFFFFE) \ 2&) And &H7FFFFFFF)
    86.                     End If
    87.                 Next
    88.                 Table(I) = Value
    89.             Next
    90.  
    91.             Return Table
    92.         End Function
    93.  
    94. #End Region
    95.  
    96.     End Class
    97.  
    98. End Namespace

    Sample usage:

    VB Code:
    1. Dim crc As New xous.Security.Cryptography.CRC32
    2.  
    3.         Dim fs As New FileStream("A FILE", FileMode.Open)
    4.  
    5.         Dim CRC32 As Integer = BitConverter.ToInt32(crc.ComputeHash(CType(fs, Stream)), 0)
    6.  
    7.         fs.Close()
    8.  
    9.         MessageBox.Show(String.Format("The CRC is: {0:X8}", CRC32))
    Last edited by <ABX; Jul 17th, 2005 at 11:11 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  2. #2
    Hyperactive Member imadthemad's Avatar
    Join Date
    May 2005
    Posts
    344

    Re: VB.Net: CRC32 HashAlgorithm

    Wow thanks for this its just what i needed!
    searching for a while
    Basketball-NBA Dream status - quit
    Programming-Next Bill Gates Dream status - quit
    Becoming a doctor dream - LIVING IT: Med 2\4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width