Calculate CRC32 of an String or Byte Array [duplicate] - vb.net

This question already has answers here:
How do I calculate CRC32 of a string
(3 answers)
Closed 9 years ago.
Is there any function or example for VB.NET to calculate CRC32 of an string or Byte Array?

Use this:
Private Sub Main()
Crc32.ComputeChecksum(Encoding.UTF8.GetBytes("Some string")).Dump()
End Sub
Public Class Crc32
Shared table As UInteger()
Shared Sub New()
Dim poly As UInteger = &Hedb88320UI
table = New UInteger(255) {}
Dim temp As UInteger = 0
For i As UInteger = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1) = 1 Then
temp = CUInt((temp >> 1) Xor poly)
Else
temp >>= 1
End If
Next
table(i) = temp
Next
End Sub
Public Shared Function ComputeChecksum(bytes As Byte()) As UInteger
Dim crc As UInteger = &HffffffffUI
For i As Integer = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &Hff) Xor bytes(i))
crc = CUInt((crc >> 8) Xor table(index))
Next
Return Not crc
End Function
End Class

Related

CRC64 calculation in VB

I'm using the code I found [here][1] tot calculate a CRC32 checksum.
I would also like to calculate a CRC64 checksum. But I can't figure out how to do this.
Any help would be appreciated!
Below the code from "Magnus" I'm using for CRC32.
Private Sub Main()
Crc32.ComputeChecksum(Encoding.UTF8.GetBytes("Some string")).Dump()
End Sub
Public Class Crc32
Shared table As UInteger()
Shared Sub New()
Dim poly As UInteger = &Hedb88320UI
table = New UInteger(255) {}
Dim temp As UInteger = 0
For i As UInteger = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1) = 1 Then
temp = CUInt((temp >> 1) Xor poly)
Else
temp >>= 1
End If
Next
table(i) = temp
Next
End Sub
Public Shared Function ComputeChecksum(bytes As Byte()) As UInteger
Dim crc As UInteger = &HffffffffUI
For i As Integer = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &Hff) Xor bytes(i))
crc = CUInt((crc >> 8) Xor table(index))
Next
Return Not crc
End Function
End Class
Thanks to Mark Adler I got the code working!
The code bellow produces the following result:
CRC64: 995DC9BBDF1939FA
Private Sub Main()
Try
MessageBox.Show("CRC64: " & UCase(Hex(CRC64.ComputeChecksum(System.Text.Encoding.UTF8.GetBytes("123456789")))))
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Class CRC64
Shared table As ULong()
Shared Sub New()
Dim poly As ULong = &Hc96c5795d7870f42UL
table = New ULong(255) {}
Dim temp As ULong = 0
For i As ULong = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1UL) = 1 Then
temp = CULng((temp >> 1) Xor poly)
Else
temp >>= 1
End If
Next
table(i) = temp
Next
End Sub
Public Shared Function ComputeChecksum(bytes As Byte()) As ULong
Dim crc As ULong = &HffffffffffffffffUL
Dim i As Integer
For i = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &HffUL) Xor bytes(i))
crc = CULng((crc >> 8) Xor table(index))
Next i
Return Not crc
End Function
End Class
[1]: https://stackoverflow.com/questions/15553697/calculate-crc32-of-an-string-or-byte-array
First you need to find a CRC-64 description to implement. You don't just want to pick a random polynomial. This one, CRC-64/XZ, from here would be easiest to convert your implementation to:
width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0x995dc9bbdf1939fa residue=0x49958c9abd7d353f name="CRC-64/XZ"
You need to take that poly and reverse the 64 bits to use as your poly. You need to use ULong instead of UInteger types in the calculation to hold 64 bits. You need to have twice as many fs for that all-ones value in 64 bits. Then you should be good to go.
Check your code by replacing "Some string" with "123456789" and see if you get the check= value in the CRC definition above.
Private Sub Main()
Try
MessageBox.Show("CRC64: " & UCase(Hex(CRC64.ComputeChecksum(System.Text.Encoding.UTF8.GetBytes("123456789")))))
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Class CRC64
Shared table As ULong()
Shared Sub New()
Dim poly As ULong = &Hc96c5795d7870f42UL
table = New ULong(255) {}
Dim temp As ULong = 0
For i As ULong = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1UL) = 1 Then
temp = CULng((temp >> 1) Xor poly)
Else
temp >>= 1
End If
Next
table(i) = temp
Next
End Sub
Public Shared Function ComputeChecksum(bytes As Byte()) As ULong
Dim crc As ULong = &HffffffffffffffffUL
Dim i As Integer
For i = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &HffUL) Xor bytes(i))
crc = CULng((crc >> 8) Xor table(index))
Next i
Return Not crc
End Function
End Class

crc16 ccitt from a byte array

I need some help with creating checksum for tcp packet from an array
of byte.
This is code is working as I've tested it with
string.
Problem is I've to pass an array of byte to this
function.
At this moment I've created it simple by using hard
embedded time serial number otherwise it is very long code to login
to a device.
Value of this is b1:68:de:3a:15:cd:5b:07 checked with wireshark and CRC should be E2B6 checked with online crc this
Now, coming to main point I just can't call this method as this method is expecting some string values and I've to pass hex values. How I can do that.
Private Sub ConnectTCP_Click(sender As Object, e As EventArgs) Handles ConnectTCP.Click
Dim Serial As UInt32 = "123456789"
Dim time As UInt32 = "987654321"
Dim buffer2() As Byte = BitConverter.GetBytes(time)
Dim buffer3() As Byte = BitConverter.GetBytes(Serial)
Dim array4(buffer2.Length + buffer3.Length - 1) As Byte
Array.Copy(buffer2, array4, buffer2.Length)
Array.Copy(buffer3, 0, array4, buffer2.Length, buffer3.Length)
getCRC16(array4) 'What I need to do here
end sub
"CRC16 CCITT function""
Public Function getCRC16(ByVal strInput As String)
Dim lngCheck As Long
Dim Power(7) As Integer
Dim I As Integer
Dim J As Integer
Dim Poly As Long
Dim CRC As Long
Dim TestBit As Boolean
Dim TestBit1 As Boolean
Dim TestBit2 As Boolean
Poly = &H1021
CRC = &HFFFF
For J = 0 To 7
Power(J) = 2 ^ J
Next J
For I = 1 To Len(strInput)
lngCheck = Asc(Mid$(strInput, I, 1))
For J = 7 To 0 Step -1
If (CRC And 32768) = 32768 Then
TestBit1 = True
Else
TestBit1 = False
End If
If (lngCheck And Power(J)) = Power(J) Then
TestBit2 = True
Else
TestBit2 = False
End If
TestBit = TestBit1 Xor TestBit2
CRC = (CRC And 32767) * 2
If TestBit = True Then
CRC = CRC Xor Poly
End If
Next J
Next I
Dim tmp As String
tmp = Hex(CRC)
CRCTCP.Text = tmp
getCRC16 = tmp
End Function
Finally, Found It.
Public Class CRC16CCITT
Public Enum InitialCRCValue
Zeroes = 0
NonZero1 = &HFFFF
NonZero2 = &H1D0F
'NonZero3 = &H0
End Enum
Private Const poly As UShort = &H1021 'polynomial
Dim table(255) As UShort
Dim intValue As UShort = 0
Public Function ComputeCheckSum(ByVal bytes As Byte()) As UShort
Dim crc As UShort = Me.intValue
'Dim x As String
For i As Integer = 0 To bytes.Length - 1
crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (&HFF And bytes(i))))))
'crc = (crc << 8) ^ (x << 15) ^ (x << 2) ^ x
Next
Return crc
End Function
Public Function ComputeCheckSumBytes(ByVal bytes As Byte()) As Byte()
Dim crc As UShort = ComputeCheckSum(bytes)
Return BitConverter.GetBytes(crc)
End Function
Public Sub New(ByVal initialvalue As InitialCRCValue)
Me.intValue = CUShort(initialvalue)
Dim temp, a As UShort
For i As Integer = 0 To table.Length - 1
temp = 0
a = CUShort(i << 8)
For j As Integer = 0 To 7
If ((temp Xor a) And &H8000) <> 0 Then
temp = CUShort((temp << 1) Xor poly)
Else
temp <<= 1
End If
a <<= 1
Next
table(i) = temp
Next
End Sub
End Class

Calcule the CRC16 whit POLYNOMIAL 0x8408 and initial crc 0xFFFF

Hi I need get the CRC16 with polynomial 0x8408 and initial 0xFFFF, in this post I found a equal case
C# CRC-16-CCITT 0x8408 Polynomial. Help needed
but I am working in VB 2013 and I intend write the same code but in this language
Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
Dim data As UShort = 0
Dim crc As UShort = &HFFFF
Dim pru() As Byte = {&H5, &H0, &H4, &HFB, &H4A, &H43}
For j As UInteger = 0 To pru.Length - 1
crc = CUShort(crc ^ pru(j))
For i As UInteger = 0 To 7
If ((crc & &H1) = 1) Then
crc = CUShort((crc >> 1) ^ &H8408)
Else
crc >>= 1
End If
Next
Next
crc = CUShort(Not crc)
data = crc
crc = CUShort((crc << 8) ^ (data >> 8 & &HFF))
MsgBox(crc)
End Sub
But when I execute this code get a overflow in crc = CUShort(crc ^ pru(j))
Can any help me.
Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
Dim data As UShort
Dim PRESET_VALUE As UShort = &HFFFF
Dim POLYNOMIAL As UShort = &H8408
Dim pru() As Byte = {&H4, &H0, &H1, &HDB, &H4B} ' the two last bytes are the CRC16
Dim pru2() As Byte = {&H5, &H0, &H1, &HFB, &HF2, &H3D}
Dim ucX As Integer = pru.Length - 3
Dim uiCrcValue As UShort = PRESET_VALUE
For j As Integer = 0 To ucX
uiCrcValue = uiCrcValue Xor pru(j)
For i As Integer = 0 To 7
If (uiCrcValue And 1) Then
uiCrcValue = (uiCrcValue >> 1) Xor POLYNOMIAL
Else
uiCrcValue = uiCrcValue >> 1
End If
Next
Next
'MsgBox(uiCrcValue)
data = uiCrcValue
uiCrcValue = CUShort((uiCrcValue << 8) Xor ((data >> 8) And &HFF))
MsgBox(uiCrcValue)
End Sub
Thanks, the code is working
In C# the ^ operator is xor. In VB, the ^ operator is exponentiation.
Just change each ^ to Xor.

example for VB.NET to calculate CRC16 of an string or Byte Array

With reference to this link
Calculate CRC32 of an String or Byte Array
I modified the code in order to calculate the CRC16 instead of CRC32, however I am getting wrong result, can some one point me where is the mistake?
Private Sub Main()
Crc16.ComputeChecksum(Encoding.UTF8.GetBytes("Some string"))
End Sub
Public Class CRC16
Shared table As UShort()
Shared Sub New()
Dim poly As UShort = &HA001US 'calculates CRC-16 using A001 polynomial (modbus)
table = New UShort(255) {}
Dim temp As UShort = 0
For i As UShort = 0 To table.Length - 1
temp = i
For j As Integer = 8 To 1 Step -1
If (temp And 1) = 1 Then
temp = CUShort((temp >> 1) Xor poly)
Else
temp >>= 1
End If
Next
table(i) = temp
Next
End Sub
Public Shared Function ComputeChecksum(ByVal bytes As Byte()) As UShort
Dim crc As UShort = &H0US ' The calculation start with 0x00
For i As Integer = 0 To bytes.Length - 1
Dim index As Byte = CByte(((crc) And &HFF) Xor bytes(i))
crc = CUShort((crc >> 8) Xor table(index))
Next
Return Not crc
End Function
End Class
Try this, it's working VB6 code for Instrument control. (sCommand is a temp string which contains all Bytes, Result is added to sCommand, Modbus is using LSB first, TextToString and StringToAscii are functions to convert a readable string "FF EE" into ASCII and back, thus they are not of interest here.):
Private Sub cmdCRC16_Click()
Dim sCommand As String
Dim x As Long
Dim y As Long
Dim lCRC As Long
sCommand = TextToString(txtASCII)
'Initial value
lCRC = 65535 '(&HFFFF results in Integer -1)
For x = 1 To Len(sCommand)
lCRC = lCRC Xor Asc(Mid(sCommand, x, 1))
For y = 1 To 8
If (lCRC Mod 2) > 0 Then
lCRC = (lCRC And 65534) / 2
lCRC = lCRC Xor 40961 '(&HA001 results in whatever negative integer)
Else
lCRC = (lCRC And 65534) / 2
End If
Next y
Next x
'Add CRC with LSB first
sCommand = sCommand + Chr(lCRC And 255)
sCommand = sCommand + Chr((lCRC And 65280) / 256)
txtASCII = StringToASCII(sCommand)
End Sub
I just came accross the same issue. Simple solution is to omit negation at the end, so just change your "Return Not crc" to "Return crc" and you be fine.
There are various variants of CRC-16, where "CRC-16" normally refers to the IBM variant, also called "ARC". It uses an XorOut value of zero. See Catalogue of parametrised CRC algorithms with 16 bits.

How can I go about adding a ProgressBar to this code which calculates CRC32 checksum in VB.NET?

Thanks for reading - I am using the class below to calculate the CRC32 checksum of a specified file.
My question is how would I go about reporting the progress of file completion (in %) to a progressbar on a different form. I have tried (i / count) * 100 under the New() sub but I am not having any luck, or setting the progress bar with it for that matter. Can anyone help?
Thanks in advance
Steve
Public Class CRC32
Private crc32Table() As Integer
Private Const BUFFER_SIZE As Integer = 1024
Public Function GetCrc32(ByRef stream As System.IO.Stream) As Integer
Dim crc32Result As Integer
crc32Result = &HFFFFFFFF
Dim buffer(BUFFER_SIZE) As Byte
Dim readSize As Integer = BUFFER_SIZE
Dim count As Integer = stream.Read(buffer, 0, readSize)
Dim i As Integer
Dim iLookup As Integer
Do While (count > 0)
For i = 0 To count - 1
iLookup = (crc32Result And &HFF) Xor buffer(i)
crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF
crc32Result = crc32Result Xor crc32Table(iLookup)
Next i
count = stream.Read(buffer, 0, readSize)
Loop
GetCrc32 = Not (crc32Result)
End Function
Public Sub New()
Dim dwPolynomial As Integer = &HEDB88320
Dim i As Integer, j As Integer
ReDim crc32Table(256)
Dim dwCrc As Integer
For i = 0 To 255
Form1.CRCWorker.ReportProgress((i / 255) * 100) 'Report Progress
dwCrc = i
For j = 8 To 1 Step -1
If (dwCrc And 1) Then
dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
dwCrc = dwCrc Xor dwPolynomial
Else
dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
End If
Next j
crc32Table(i) = dwCrc
Next i
'file complete
End Sub
End Class
'------------- END CRC32 CLASS--------------
'-------------- START FORM1 --------------------------
Private Sub CRCWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles CRCWorker.DoWork
For i = CurrentInt To dgv.Rows.Count - 1
CRCWorker.ReportProgress(0, i & "/" & Total_Files)
Current_File_Num = (i + 1)
SetControlText(lblCurrentFile, Str(Current_File_Num) & "/" & Total_Files)
result = CheckFile(SFV_Parent_Directory & "\" & dgv.Rows(i).Cells(0).Value, dgv.Rows(i).Cells(1).Value)
Select Case result
Case 0 ' missing file
UpdateRow(i, 2, "MISSING")
'dgv.Rows(i).Cells(2).Value = "MISSING"
Missing_Files = Missing_Files + 1
SetControlText(lblMissingFiles, Str(Missing_Files))
Case 1 ' crc match
UpdateRow(i, 2, "OK")
' dgv.Rows(i).Cells(2).Value = "OK"
Good_Files = Good_Files + 1
SetControlText(lblGoodFiles, Str(Good_Files))
Case 2 'crc bad
UpdateRow(i, 2, "BAD")
' dgv.Rows(i).Cells(2).Value = "BAD"
Bad_Files = Bad_Files + 1
SetControlText(lblBadFiles, Str(Bad_Files))
End Select
If CRCWorker.CancellationPending = True Then
e.Cancel = True
Exit Sub
End If
Next
End Sub
Private Sub CRCWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles CRCWorker.ProgressChanged
Dim val As Integer = e.ProgressPercentage
ProgressBar2.Maximum = 100
ProgressBar2.Value = e.ProgressPercentage
Debug.Print(val)
End Sub
Function CheckFile(ByVal tocheck_filepath As String, ByVal expected_crc As String) As Integer 'returns result of a file check 0 = missing 1 = good 2 = bad
If File.Exists(tocheck_filepath) = False Then
Return 0 'return file missing
End If
Dim f As FileStream = New FileStream(tocheck_filepath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim c As New CRC32()
crc = c.GetCrc32(f)
Dim crcResult As String = "00000000"
crcResult = String.Format("{0:X8}", crc)
f.Close()
End Function
It appears your .ReportProgress() call is in the New() subroutine, which is the part that makes the lookup table for the CRC calculation. The New() subroutine is called once, before the main CRC routine. The main CRC routine is the one that takes up all the time and needs the progress bar.
Shouldn't the progress bar updating be in the GetCrc32() function? Something like this:
Public Function GetCrc32(ByRef stream As System.IO.Stream, _
Optional prbr As ProgressBar = Nothing) As UInteger
Dim crc As UInteger = Not CUInt(0)
Dim buffer(BUFFER_SIZE) As Byte
Dim readSize As Integer = BUFFER_SIZE
Dim left As Long = stream.Length
If Not (prbr Is Nothing) Then ' ProgressBar setup for counting down amount left.
prbr.Maximum = 100
prbr.Minimum = 0
prbr.Value = 100
End If
Dim count As Integer : Do
count = stream.Read(buffer, 0, readSize)
For i As Integer = 0 To count - 1
crc = (crc >> 8) Xor tbl((crc And 255) Xor buffer(i))
Next
If Not (prbr Is Nothing) Then ' ProgressBar updated here
left -= count
prbr.Value = CInt(left * 100 \ stream.Length)
prbr.Refresh()
End If
Loop While count > 0
Return Not crc
End Function
In Windows Forms BackgroundWorker Class is often used to run intensive tasks in another thread and update progress bar without blocking the interface.
Example of using BackgroundWorker in VB.Net
The problem is when you use use the form in your code without instantiating it Form1.CRCWorker.ReportProgress((i / 255) * 100) there is a kind of hidden "auto-instantiation" happening and new instance of Form1 is created each time.