Advertisement
TizzyT

Custom Adler16 implementation -TizzyT

Mar 3rd, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 0.97 KB | None | 0 0
  1.     ' Was looking for a good checksum algorithm which was faster than CRC16 and stumbled upon Adler32
  2.     ' Adler32 unfortunately was 32 bits, so I thought I could throw a 16 bit version together given some info
  3.     ' from the wikipedia page: https://en.wikipedia.org/wiki/Adler-32
  4.     ' NOTE: This is completely my attempt at implementing Adler32 at 16bits, use at your on risk.
  5.     ' On a good note, this implementation doesn't have any collisions (when input is also 16bits)
  6.  
  7.     ' Fast Version
  8.     Public Shared Function Adler16fast(ByVal Data() As Byte) As UShort
  9.         Dim A As UShort = 1, B As ULong = 0
  10.         For i As Integer = 0 To Data.Length - 1
  11.             A = A Xor Data(i)
  12.             B += A
  13.         Next
  14.         Return (CUShort(B Mod 256) << 8) Or A
  15.     End Function
  16.  
  17.     ' Original POC Version
  18.     Public Shared Function Adler16(ByVal Data() As Byte) As UShort
  19.         Dim A As UShort = 1, B As UShort = 0
  20.         For Each D As Byte In Data
  21.             A = (A + D) Mod 256
  22.             B = (A + B) Mod 256
  23.         Next
  24.         Return (B << 8) Or A
  25.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement