Feedback

VB - CRC32-Hash eines Strings

Veröffentlicht von am 10/22/2008
(2 Bewertungen)
Dieses Snipptet beschreibt eine Klasse zur Berechnung des CRC32-Hashes eines Strings.


Dim testText As String = "abcdefgh"
Dim hash As String = gfoidl.HashFunktionen.CRC32.CRC32OfString(testText)
Imports System
Imports System.Text
Imports System.Security.Cryptography

Namespace gfoidl.HashFunktionen
	Public Class CRC32
		Inherits HashAlgorithm
		Public Shared Function CRC32OfString(ByVal s As String) As String
			Dim original As Byte()
			Dim encoded As Byte()

			Dim crc32 As New CRC32()
			original = Encoding.[Default].GetBytes(s)
			encoded = crc32.ComputeHash(original)

			Dim sbEncoded As New StringBuilder()
			For i As Integer = 0 To encoded.Length - 1
				sbEncoded.Append(encoded(i).ToString("x2"))
			Next

			Return sbEncoded.ToString()
		End Function

		#Region "Konstanten"
		Public Const DefaultPolynomial As UInt32 = 3988292384
		Public Const DefaultSeed As UInt32 = 4294967295
		#End Region

		#Region "Private Variablen"
		Private _hash As UInt32
		Private _seed As UInt32
		Private _table As UInt32()
		#End Region

		#Region "Kontruktoren"
		Public Sub New()
			_table = InitializeTable(DefaultPolynomial)
			_seed = DefaultSeed
			Initialize()
		End Sub

		Public Sub New(ByVal polynomial As UInt32, ByVal seed As UInt32)
			_table = InitializeTable(polynomial)
			Me._seed = seed
			Initialize()
		End Sub
		#End Region

		#Region "Überschreibungen"
		Public Overloads Overrides Sub Initialize()
			_hash = _seed
		End Sub

		Protected Overloads Overrides Sub HashCore(ByVal buffer As Byte(), ByVal start As Integer, ByVal length As Integer)
			_hash = CalculateHash(_table, _hash, buffer, start, length)
		End Sub

		Protected Overloads Overrides Function HashFinal() As Byte()
			Dim hashBuffer As Byte() = UInt32ToBigEndianBytes(_hash)
			Me.HashValue = hashBuffer

			Return hashBuffer
		End Function

		Public Overloads Overrides ReadOnly Property HashSize() As Integer
			Get
				Return 32
			End Get
		End Property
		#End Region

		#Region "Implementierung"
		Public Shared Function Compute(ByVal polynomial As UInt32, ByVal seed As UInt32, ByVal buffer As Byte()) As UInt32
			Return CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length)
		End Function

		Private Shared Function InitializeTable(ByVal polynomial As UInt32) As UInt32()
			Dim createTable As UInt32() = New UInt32(255) {}
			For i As Integer = 0 To 255

				Dim entry As UInt32 = i
				For j As Integer = 0 To 7
					If (entry And 1) = 1 Then
						entry = (entry >> 1) Xor polynomial
					Else
						entry = entry >> 1
					End If
				Next

				createTable(i) = entry
			Next

			Return createTable
		End Function

		Private Shared Function CalculateHash(ByVal table As UInt32(), ByVal seed As UInt32, ByVal buffer As Byte(), ByVal start As Integer, ByVal size As Integer) As UInt32
			Dim crc As UInt32 = seed
			For i As Integer = start To size - 1
				crc = (crc >> 8) Xor table(buffer(i) Xor crc And 255)
			Next

			Return Not crc
		End Function

		Private Function UInt32ToBigEndianBytes(ByVal x As UInt32) As Byte()
			Return New Byte() {CByte(((x >> 24) And 255)), CByte(((x >> 16) And 255)), CByte(((x >> 8) And 255)), CByte((x And 255))}
		End Function
		#End Region
	End Class
End Namespace
Abgelegt unter CRC, CRC32, Hash, String.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!