CRC-32

From Rosetta Code
Jump to: navigation, search
Task
CRC-32
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate a method of deriving the Cyclic Redundancy Check from within the language. The result should be in accordance with ISO 3309, ITU-T V.42, Gzip and PNG. Algorithms are described on Computation of CRC in Wikipedia. This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF16, and complements the final CRC.

For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string "The quick brown fox jumps over the lazy dog" (without quotes).

Contents

[edit] Ada

Works with: GNAT
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.CRC32; use GNAT.CRC32;
with Interfaces; use Interfaces;
procedure TestCRC is
package IIO is new Ada.Text_IO.Modular_IO (Unsigned_32);
crc : CRC32;
num : Unsigned_32;
str : String := "The quick brown fox jumps over the lazy dog";
begin
Initialize (crc);
Update (crc, str);
num := Get_Value (crc);
IIO.Put (num, Base => 16); New_Line;
end TestCRC;
Output:
16#414FA339#

[edit] AutoHotkey

Source: CRC32 @github by jNizM

str := "The quick brown fox jumps over the lazy dog"
MsgBox, % "String:`n" (str) "`n`nCRC32:`n" CRC32(str)
 
 
 
; CRC32 =============================================================================
CRC32(string, encoding = "UTF-8")
{
chrlength := (encoding = "CP1200" || encoding = "UTF-16") ? 2 : 1
length := (StrPut(string, encoding) - 1) * chrlength
VarSetCapacity(data, length, 0)
StrPut(string, &data, floor(length / chrlength), encoding)
SetFormat, Integer, % SubStr((A_FI := A_FormatInteger) "H", 0)
CRC32 := DllCall("NTDLL\RtlComputeCrc32", "UInt", 0, "UInt", &data, "UInt", length, "UInt")
CRC := SubStr(CRC32 | 0x1000000000, -7)
DllCall("User32.dll\CharLower", "Str", CRC)
SetFormat, Integer, %A_FI%
return CRC
}
Output:
String:    The quick brown fox jumps over the lazy dog
CRC32:     414fa339

[edit] C

[edit] Library

Using zlib's crc32:

#include <stdio.h>
#include <string.h>
#include <zlib.h>
 
int main()
{
const char *s = "The quick brown fox jumps over the lazy dog";
printf("%lX\n", crc32(0, (const void*)s, strlen(s)));
 
return 0;
}

[edit] Implementation

This code is a translation from Ruby, with an adjustment to use 32-bit integers. This code happens to resemble the examples from RFC 1952 section 8 and from PNG annex D, because those examples use an identical table.

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
 
uint32_t
rc_crc32(uint32_t crc, const char *buf, size_t len)
{
static uint32_t table[256];
static int have_table = 0;
uint32_t rem;
uint8_t octet;
int i, j;
const char *p, *q;
 
/* This check is not thread safe; there is no mutex. */
if (have_table == 0) {
/* Calculate CRC table. */
for (i = 0; i < 256; i++) {
rem = i; /* remainder from polynomial division */
for (j = 0; j < 8; j++) {
if (rem & 1) {
rem >>= 1;
rem ^= 0xedb88320;
} else
rem >>= 1;
}
table[i] = rem;
}
have_table = 1;
}
 
crc = ~crc;
q = buf + len;
for (p = buf; p < q; p++) {
octet = *p; /* Cast to unsigned octet. */
crc = (crc >> 8) ^ table[(crc & 0xff) ^ octet];
}
return ~crc;
}
 
int
main()
{
const char *s = "The quick brown fox jumps over the lazy dog";
printf("%" PRIX32 "\n", rc_crc32(0, s, strlen(s)));
 
return 0;
}

[edit] C++

Library: STL
#include <string>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <array>
#include <cstdint>
 
class CRC32
{
public:
CRC32()
{
generateTable();
}
 
template<class T>
uint32_t get( T begin, T end )
{
uint32_t nCRC = ~static_cast<uint32_t>(0);
return ~std::accumulate( begin, end, 0xFFFFFFFF, [&](uint32_t nCRC, uint32_t nVal)
{ return (nCRC >> 8) ^ m_pTable[(nCRC & 0xff) ^ nVal]; } );
}
 
private:
void generateTable()
{
int nCount = 0;
// fill the table with 0..255
std::generate( m_pTable.begin(), m_pTable.end(), [&nCount](){ return nCount++; } );
 
// calculate the crc table
for (int j = 0; j < 8; j++)
{
std::transform( m_pTable.begin(), m_pTable.end(), m_pTable.begin(),
[] ( uint32_t &nValue ) { return (nValue>>1)^((nValue&1)*0xedb88320); } );
}
}
 
private:
std::array<uint32_t, 256> m_pTable;
};
 
int main()
{
CRC32 oCrc;
std::string str( "The quick brown fox jumps over the lazy dog" );
std::cout << "Checksum: " << std::hex << oCrc.get( str.begin(), str.end() ) << std::endl;
return 0;
}
 
Library: boost
#include <boost\crc.hpp>
#include <string>
#include <iostream>
 
int main()
{
std::string str( "The quick brown fox jumps over the lazy dog" );
boost::crc_32_type crc;
crc.process_bytes( str.data(), str.size() );
 
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl;
return 0;
}
Output:
Checksum: 414fa339

[edit] C#

 
/// <summary>
/// Performs 32-bit reversed cyclic redundancy checks.
/// </summary>
public class Crc32
{
#region Constants
/// <summary>
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
/// </summary>
private const UInt32 s_generator = 0xEDB88320;
#endregion
 
#region Constructors
/// <summary>
/// Creates a new instance of the Crc32 class.
/// </summary>
public Crc32()
{
// Constructs the checksum lookup table. Used to optimize the checksum.
m_checksumTable = Enumerable.Range(0, 256).Select(i =>
{
var tableEntry = (uint)i;
for (var j = 0; j < 8; ++j)
{
tableEntry = ((tableEntry & 1) != 0)
? (s_generator ^ (tableEntry >> 1))
: (tableEntry >> 1);
}
return tableEntry;
}).ToArray();
}
#endregion
 
#region Methods
/// <summary>
/// Calculates the checksum of the byte stream.
/// </summary>
/// <param name="byteStream">The byte stream to calculate the checksum for.</param>
/// <returns>A 32-bit reversed checksum.</returns>
public UInt32 Get<T>(IEnumerable<T> byteStream)
{
try
{
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =>
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8)));
}
catch (FormatException e)
{
throw new CrcException("Could not read the stream out as bytes.", e);
}
catch (InvalidCastException e)
{
throw new CrcException("Could not read the stream out as bytes.", e);
}
catch (OverflowException e)
{
throw new CrcException("Could not read the stream out as bytes.", e);
}
}
#endregion
 
#region Fields
/// <summary>
/// Contains a cache of calculated checksum chunks.
/// </summary>
private readonly UInt32[] m_checksumTable;
 
#endregion
}
 

[edit] Clojure

Translation of: Java
(let [crc (new java.util.zip.CRC32)
str "The quick brown fox jumps over the lazy dog"]
(. crc update (. str getBytes))
(printf "CRC-32('%s') = %s\n" str (Long/toHexString (. crc getValue))))
Output:
CRC-32('The quick brown fox jumps over the lazy dog') = 414fa339

[edit] Common Lisp

Library: Ironclad
(ql:quickload :ironclad)
(defun string-to-digest (str digest)
"Return the specified digest for the ASCII string as a hex string."
(ironclad:byte-array-to-hex-string
(ironclad:digest-sequence digest
(ironclad:ascii-string-to-byte-array str))))
 
(string-to-digest "The quick brown fox jumps over the lazy dog" :crc32)
 
Output:
"414fa339"

[edit] Component Pascal

BlackBox Component Builder
Require ZLib Subsystem

 
MODULE BbtComputeCRC32;
IMPORT ZlibCrc32,StdLog;
 
PROCEDURE Do*;
VAR
s: ARRAY 128 OF SHORTCHAR;
BEGIN
s := "The quick brown fox jumps over the lazy dog";
StdLog.IntForm(ZlibCrc32.CRC32(0,s,0,LEN(s$)),16,12,'0',TRUE);
StdLog.Ln;
END Do;
END BbtComputeCRC32.
 

Execute: ^Q BbtComputeCRC32.Do
Output:

0414FA339%16

[edit] D

import std.stdio, std.digest.crc;
 
void main() {
"The quick brown fox jumps over the lazy dog"
.crc32Of().crcHexString().writeln();
}
Output:
414FA339

[edit] Erlang

Using the built-in crc32 implementation.

 
-module(crc32).
-export([test/0]).
test() ->
io:fwrite("~.16#~n",[erlang:crc32(<<"The quick brown fox jumps over the lazy dog">>)]).
 

The output is:

 
16#414FA339
 

[edit] Factor

Like SHA-1#Factor, but with crc32.

IN: scratchpad USING: checksums checksums.crc32 ;
IN: scratchpad "The quick brown fox jumps over the lazy dog" crc32
               checksum-bytes hex-string .
"414fa339"

The implementation is at core/checksums/crc32/crc32.factor.

[edit] FBSL

#APPTYPE CONSOLE
 
PRINT HEX(CHECKSUM("The quick brown fox jumps over the lazy dog"))
 
PAUSE
Output:
414FA339

Press any key to continue...

[edit] Go

[edit] Library

package main
 
import (
"fmt"
"hash/crc32"
)
 
func main() {
s := []byte("The quick brown fox jumps over the lazy dog")
result := crc32.ChecksumIEEE(s)
fmt.Printf("%X\n", result)
}

output

414FA339

[edit] Implementation

package main
 
import "fmt"
 
var table [256]uint32
 
func init() {
for i := range table {
word := uint32(i)
for j := 0; j < 8; j++ {
if word&1 == 1 {
word = (word >> 1) ^ 0xedb88320
} else {
word >>= 1
}
}
table[i] = word
}
}
 
func crc32(s string) uint32 {
crc := ^uint32(0)
for i := 0; i < len(s); i++ {
crc = table[byte(crc)^s[i]] ^ (crc >> 8)
}
return ^crc
}
 
func main() {
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog"))
}

Output:

414fa339

[edit] Groovy

def crc32(byte[] bytes) {
new java.util.zip.CRC32().with { update bytes; value }
}

Testing:

assert '414FA339' == sprintf('%04X', crc32('The quick brown fox jumps over the lazy dog'.bytes))


[edit] Haskell

Pure Haskell:

import Data.Bits
import Data.Word
import Numeric
 
crcTable :: Word32 -> Word32
crcTable i = table !! (fromIntegral i)
where
table = map (\a -> iterate xf a !! 8) [0..255]
xf r = let d = shiftR r 1 in
if r .&. 1 == 1 then xor d 0xedb88320 else d
 
charToWord :: Char -> Word32
charToWord c = (fromIntegral . fromEnum) c
 
calcCrc :: String -> Word32
calcCrc text = complement ( foldl cf (complement 0) text )
where cf crc x = xor (shiftR crc 8) (crcTable $ xor (crc .&. 0xff) (charToWord x) )
 
crc32 text = showHex ( calcCrc text ) ""
 
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"


Using the zlib C library ( compile with "ghc -lz file.hs"):

import Data.List (genericLength)
import Numeric (showHex)
import Foreign.C
 
foreign import ccall "zlib.h crc32" zlib_crc32 :: CULong -> CString -> CUInt -> CULong
 
main = do
let s = "The quick brown fox jumps over the lazy dog"
ptr <- newCString s
 
let r = zlib_crc32 0 ptr (genericLength s)
 
putStrLn $ showHex r ""

[edit] Icon and Unicon

There is no library function for this so we implement one. Icon/Unicon binary operations apply to large integers so we need to mask to the desired unsigned word size. This also only applies to full bytes.

link hexcvt,printf
 
procedure main()
s := "The quick brown fox jumps over the lazy dog"
a := "414FA339"
printf("crc(%i)=%s - implementation is %s\n",
s,r := crc32(s),if r == a then "correct" else "in error")
end
 
procedure crc32(s) #: return crc-32 (ISO 3309, ITU-T V.42, Gzip, PNG) of s
static crcL,mask
initial {
crcL := list(256) # crc table
p := [0,1,2,4,5,7,8,10,11,12,16,22,23,26] # polynomial terms
mask := 2^32-1 # word size mask
every (poly := 0) := ior(poly,ishift(1,31-p[1 to *p]))
every c := n := 0 to *crcL-1 do { # table
every 1 to 8 do
c := iand(mask,
if iand(c,1) = 1 then
ixor(poly,ishift(c,-1))
else
ishift(c,-1)
)
crcL[n+1] := c
}
}
 
crc := ixor(0,mask) # invert bits
every crc := iand(mask,
ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8)))
return hexstring(ixor(crc,mask)) # return hexstring
end

hexcvt.icnprovides hex and hexstring printf.icnprovides formatting

Output:
crc("The quick brown fox jumps over the lazy dog")=414FA339 - implementation is correct

[edit] J

   ((i.32) e. 32 26 23 22 16 12 11 10 8 7 5 4 2 1 0) 128!:3 'The quick brown fox jumps over the lazy dog'
_3199229127

Other possible representations of this result:

   (2^32x)|((i.32) e. 32 26 23 22 16 12 11 10 8 7 5 4 2 1 0) 128!:3 'The quick brown fox jumps over the lazy dog'
1095738169
require'convert'
hfd (2^32x)|((i.32) e. 32 26 23 22 16 12 11 10 8 7 5 4 2 1 0) 128!:3 'The quick brown fox jumps over the lazy dog'
414FA339

[edit] Java

import java.util.zip.* ;
 
public class CRCMaker {
public static void main( String[ ] args ) {
String toBeEncoded = new String( "The quick brown fox jumps over the lazy dog" ) ;
CRC32 myCRC = new CRC32( ) ;
myCRC.update( toBeEncoded.getBytes( ) ) ;
System.out.println( "The CRC-32 value is : " + Long.toHexString( myCRC.getValue( ) ) + " !" ) ;
}
}

Output:

The CRC-32 value is : 414fa339 !

[edit] Mathematica

IntegerString[Hash["The quick brown fox jumps over the lazy dog", "CRC32"], 16, 8]
-> "414fa339"

[edit] NetRexx

Translation of: Java
/* NetRexx */
options replace format comments java crossref symbols binary
 
import java.util.zip.CRC32
 
toBeEncoded = String("The quick brown fox jumps over the lazy dog")
myCRC = CRC32()
myCRC.update(toBeEncoded.getBytes())
say "The CRC-32 value is :" Long.toHexString(myCRC.getValue()) "!"
 
return
 

Output:

The CRC-32 value is : 414fa339 !

[edit] OCaml

Library: camlzip
let () =
let s = "The quick brown fox jumps over the lazy dog" in
let crc = Zlib.update_crc 0l s 0 (String.length s) in
Printf.printf "crc: %lX\n" crc

Running this code in interpreted mode:

$ ocaml unix.cma -I +zip zip.cma crc.ml
crc: 414FA339

[edit] Perl

#!/usr/bin/perl
use 5.010 ;
use strict ;
use warnings ;
use Digest::CRC qw( crc32 ) ;
 
my $crc = Digest::CRC->new( type => "crc32" ) ;
$crc->add ( "The quick brown fox jumps over the lazy dog" ) ;
say "The checksum is " . $crc->hexdigest( ) ;
 

Output:

The checksum is 414fa339

[edit] Perl 6

[edit] Call to native function crc32 in zlib

Library name and types are platform-dependent. As written the solution has been tested on Mac OS X 10.5.8.

Note: Buf $buf would be preferable, but NativeCall does not support Buf parameters, yet.

use NativeCall;
 
sub crc32(int32 $crc, Str $buf, int32 $len --> int32) is native('/usr/lib/libz.dylib') { * }
 
my $buf = 'The quick brown fox jumps over the lazy dog';
say crc32(0, $buf, $buf.chars).fmt('%08x');
Output:
414fa339

[edit] Pure Perl 6

A fairly generic implementation with no regard to execution speed:

sub crc(
Blob $buf,
# polynomial including leading term, default: ISO 3309/PNG/gzip
:@poly = (1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1),
:$n = @poly.end, # degree of polynomial
:@init = 1 xx $n, # initial XOR bits
:@fini = 1 xx $n, # final XOR bits
:@bitorder = 0..7, # default: eat bytes LSB-first
:@crcorder = 0..$n-1, # default: MSB of checksum is coefficient of x⁰
) {
my @bit = ($buf.list X+& (1 X+< @bitorder))».so».Int, 0 xx $n;
 
@bit[0 .. $n-1] «+^=» @init;
@bit[$_ ..$_+$n] «+^=» @poly if @bit[$_] for 0..@bit.end-$n;
@bit[*-$n.. *-1] «+^=» @fini;
 
:2[@bit[@bit.end X- @crcorder]];
}
 
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);
Output:
414FA339

[edit] PHP

PHP has a built-in function crc32.

printf("%x\n", crc32("The quick brown fox jumps over the lazy dog"));
414fa339

[edit] PL/I

*process source attributes xref or(!) nest;
crct: Proc Options(main);
/*********************************************************************
* 19.08.2013 Walter Pachl derived from REXX
*********************************************************************/

Dcl (LEFT,LENGTH,RIGHT,SUBSTR,UNSPEC) Builtin;
Dcl SYSPRINT Print;
dcl tab(0:255) Bit(32);
Call mk_tab;
Call crc_32('The quick brown fox jumps over the lazy dog');
Call crc_32('Generate CRC32 Checksum For Byte Array Example');
 
crc_32: Proc(s);
/*********************************************************************
* compute checksum for s
*********************************************************************/

Dcl s Char(*);
Dcl d Bit(32);
Dcl d1 Bit( 8);
Dcl d2 Bit(24);
Dcl cc Char(1);
Dcl ccb Bit(8);
Dcl tib Bit(8);
Dcl ti Bin Fixed(16) Unsigned;
Dcl k Bin Fixed(16) Unsigned;
d=(32)'1'b;
Do k=1 To length(s);
d1=right(d,8);
d2=left(d,24);
cc=substr(s,k,1);
ccb=unspec(cc);
tib=d1^ccb;
Unspec(ti)=tib;
d='00000000'b!!d2^tab(ti);
End;
d=d^(32)'1'b;
Put Edit(s,'CRC_32=',b2x(d))(Skip,a(50),a,a);
Put Edit('decimal ',b2d(d))(skip,x(49),a,f(10));
End;
 
b2x: proc(b) Returns(char(8));
dcl b bit(32);
dcl b4 bit(4);
dcl i Bin Fixed(31);
dcl r Char(8) Var init('');
Do i=1 To 29 By 4;
b4=substr(b,i,4);
Select(b4);
When('0000'b) r=r!!'0';
When('0001'b) r=r!!'1';
When('0010'b) r=r!!'2';
When('0011'b) r=r!!'3';
When('0100'b) r=r!!'4';
When('0101'b) r=r!!'5';
When('0110'b) r=r!!'6';
When('0111'b) r=r!!'7';
When('1000'b) r=r!!'8';
When('1001'b) r=r!!'9';
When('1010'b) r=r!!'A';
When('1011'b) r=r!!'B';
When('1100'b) r=r!!'C';
When('1101'b) r=r!!'D';
When('1110'b) r=r!!'E';
When('1111'b) r=r!!'F';
End;
End;
Return(r);
End;
 
b2d: Proc(b) Returns(Dec Fixed(15));
Dcl b Bit(32);
Dcl r Dec Fixed(15) Init(0);
Dcl i Bin Fixed(16);
Do i=1 To 32;
r=r*2
If substr(b,i,1) Then
r=r+1;
End;
Return(r);
End;
 
mk_tab: Proc;
dcl b32 bit(32);
dcl lb bit( 1);
dcl ccc bit(32) Init('edb88320'bx);
dcl (i,j) Bin Fixed(15);
Do i=0 To 255;
b32=(24)'0'b!!unspec(i);
Do j=0 To 7;
lb=right(b32,1);
b32='0'b!!left(b32,31);
If lb='1'b Then
b32=b32^ccc;
End;
tab(i)=b32;
End;
End;
End;

Output:

The quick brown fox jumps over the lazy dog       CRC_32=414FA339
                                                 decimal 1095738169
Generate CRC32 Checksum For Byte Array Example    CRC_32=D1370232
                                                 decimal 3510043186 

[edit] Python

[edit] Library

zlib.crc32 and binascii.crc32 give identical results.

>>> import zlib
>>> hex(zlib.crc32('The quick brown fox jumps over the lazy dog'))
'0x414fa339'
>>> import binascii
>>> hex(binascii.crc32('The quick brown fox jumps over the lazy dog'))
'0x414fa339'

If you have Python 2.x, these functions might return a negative integer; you would need to use & 0xffffffff to get the same answer as Python 3.x.

[edit] Racket

 
#lang racket
(define (bytes-crc32 data)
(bitwise-xor
(for/fold ([accum #xFFFFFFFF])
([byte (in-bytes data)])
(for/fold ([accum (bitwise-xor accum byte)])
([num (in-range 0 8)])
(bitwise-xor (quotient accum 2)
(* #xEDB88320 (bitwise-and accum 1)))))
#xFFFFFFFF))
 
(define (crc32 s)
(bytes-crc32 (string->bytes/utf-8 s)))
 
(format "~x" (crc32 "The quick brown fox jumps over the lazy dog"))
 

Output:

 
"414fa339"
 

[edit] REXX

/*REXX program computes the   CRC-32   (32 bit Cylic Redundancy Check), */
/* checksum for a given string [as described in ISO 3309, ITU-T V.42].*/
call show "The quick brown fox jumps over the lazy dog"
call show 'Generate CRC32 Checksum For Byte Array Example'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: procedure; parse arg Xstring; numeric digits 12; say; say
checksum=CRC_32(Xstring) /*invoke CRC_32 to create a CRC*/
say center(' input string [length of' length(Xstring) "bytes] ", 79, '─')
say Xstring /*show the string on its own line*/
say
checksum=bitxor(checksum,'ffFFffFF'x) /*final convolution for checksum.*/
say 'hex CRC-32 checksum =' c2x(checksum) left('',15),
"dec CRC-32 checksum =" c2d(checksum) /*show CRC-32 in hex & dec*/
return
/*──────────────────────────────────CRC_32 subroutine───────────────────*/
CRC_32: procedure; parse arg !,$ /*2nd arg is for multi-invokes. */
 
do i=0 for 256; z=d2c(i) /*build the 8-bit indexed table.*/
r=right(z,4,'0'x) /*insure the R is 32 bits. */
do j=0 for 8 /*handle each bit of rightmost 8.*/
rb=x2b(c2x(r)) /*convert char ──► hex ──► binary*/
_=right(rb,1) /*remember right-most bit for IF.*/
r=x2c(b2x(0 || left(rb,31))) /*shift it right (unsigned) 1 bit*/
if _\==0 then r=bitxor(r,'edb88320'x) /*bit XOR grunt-work.*/
end /*j*/
 !.z=r
end /*i*/
 
$=bitxor(word($ '0000000'x,1),'ffFFffFF'x) /*use user's CRC or default.*/
do k=1 for length(!) /*start crunching the input data.*/
 ?=bitxor(right($,1),substr(!,k,1)); $=bitxor('0'x||left($,3),!.?)
end /*k*/
return $ /*return with da money to invoker*/

output

────────────────────── input string [length of 43 bytes] ──────────────────────
The quick brown fox jumps over the lazy dog

hex CRC-32 checksum = 414FA339                 dec CRC-32 checksum = 1095738169


────────────────────── input string [length of 46 bytes] ──────────────────────
Generate CRC32 Checksum For Byte Array Example

hex CRC-32 checksum = D1370232                 dec CRC-32 checksum = 3510043186

[edit] Ruby

Use 'zlib' from standard library.

require 'zlib'
printf "0x%08x\n", Zlib.crc32('The quick brown fox jumps over the lazy dog')
# => 0x414fa339

Reimplement CRC-32 in Ruby, with comments to show the polynomials.

module CRC
# Divisor is a polynomial of degree 32 with coefficients modulo 2.
# We store Divisor in a 33-bit Integer; the polynomial is
# Divisor[32] + Divisor[31] * x + ... + Divisor[0] * x**32
Divisor = [0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26, 32] \
.inject(0) {|sum, exponent| sum + (1 << (32 - exponent))}
 
# This table gives the crc (without conditioning) of every possible
# _octet_ from 0 to 255. Each _octet_ is a polynomial of degree 7,
# octet[7] + octet[6] * x + ... + octet[0] * x**7
# Then remainder = Table[octet] is the remainder from
# _octet_ times x**32 divided by Divisor,
# remainder[31] + remainder[30] + ... + remainder[0] * x**31
Table = Array.new(256) do |octet|
# Find remainder from polynomial long division.
# octet[ 7] * x**32 + ... + octet[0] * x**39
# Divisor[32] * x**0 + ... + Divisor[0] * x**32
remainder = octet
(0..7).each do |i|
# Find next term of quotient. To simplify the code,
# we assume that Divisor[0] is 1, and we only check
# remainder[i]. We save remainder, forget quotient.
if remainder[i].zero?
# Next term of quotient is 0 * x**(7 - i).
# No change to remainder.
else
# Next term of quotient is 1 * x**(7 - i). Multiply
# this term by Divisor, then subtract from remainder.
# * Multiplication uses left shift :<< to align
# the x**(39 - i) terms.
# * Subtraction uses bitwise exclusive-or :^.
remainder ^= (Divisor << i)
end
end
remainder >> 8 # Remove x**32 to x**39 terms.
end
 
module_function
 
def crc32(string, crc = 0)
# Pre-conditioning: Flip all 32 bits. Without this step, a string
# preprended with extra "\0" would have same crc32 value.
crc ^= 0xffff_ffff
 
# Iterate octets to perform polynomial long division.
string.each_byte do |octet|
 
# Update _crc_ by continuing its polynomial long division.
# Our current remainder is old _crc_ times x**8, plus
# new _octet_ times x**32, which is
# crc[32] * x**8 + crc[31] * x**9 + ... + crc[8] * x**31 \
# + (crc[7] + octet[7]) * x**32 + ... \
# + (crc[0] + octet[0]) * x**39
#
# Our new _crc_ is the remainder from this polynomial divided by
# Divisor. We split the terms into part 1 for x**8 to x**31, and
# part 2 for x**32 to x**39, and divide each part separately.
# Then remainder 1 is trivial, and remainder 2 is in our Table.
 
remainder_1 = crc >> 8
remainder_2 = Table[(crc & 0xff) ^ octet]
 
# Our new _crc_ is sum of both remainders. (This sum never
# overflows to x**32, so is not too big for Divisor.)
crc = remainder_1 ^ remainder_2
end
 
# Post-conditioning: Flip all 32 bits. If we later update _crc_,
# this step cancels the next pre-conditioning.
crc ^ 0xffff_ffff
end
end
 
printf "0x%08x\n", CRC.crc32("The quick brown fox jumps over the lazy dog")
# => 0x414fa339

[edit] Scala

Translation of: Java
import java.util.zip.CRC32
val crc=new CRC32
crc.update("The quick brown fox jumps over the lazy dog".getBytes)
println(crc.getValue.toHexString) //> 414fa339

[edit] Seed7

$ include "seed7_05.s7i";
include "crc32.s7i";
 
const proc: main is func
begin
writeln(ord(crc32("The quick brown fox jumps over the lazy dog")) radix 16 lpad0 8);
end func;
Output:
414fa339

[edit] Smalltalk

Works with: Smalltalk/X

the CRC32Stream utility class can do it for me:

CRC32Stream hashValueOf:'The quick brown fox jumps over the lazy dog'

Output: 1095738169 "which is 16r414FA339"


[edit] Tcl

package require Tcl 8.6
 
set data "The quick brown fox jumps over the lazy dog"
puts [format "%x" [zlib crc32 $data]]

Which produces this output:

414fa339

Alternatively, with older versions of Tcl:

Library: Tcllib (Package: crc32)
package require crc32
puts [format "%x" [crc::crc32 $data]]

With the same input data, it produces identical output.

[edit] XPL0

code HexOut=27;         \intrinsic routine
string 0; \use zero-terminated strings
 
func CRC32(Str, Len); \Return CRC-32 for given string
char Str; int Len; \byte array, number of bytes
int I, J, R, C;
[R:= -1; \initialize with all 1's
for J:= 0 to Len-1 do
[C:= Str(J);
for I:= 0 to 8-1 do \for each bit in byte...
[if (R xor C) and 1 then R:= R>>1 xor $EDB88320
else R:= R>>1;
C:= C>>1;
];
];
return not R;
];
 
HexOut(0, CRC32("The quick brown fox jumps over the lazy dog", 43))
Output:
414FA339

[edit] zkl

Using zlib:

var ZL=Import("zeelib");
ZL.calcCRC32(Data(0,0,"The quick brown fox jumps over the lazy dog")).toString(16)
//-->414fa339
Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox