1.\" Copyright (c) 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the Institute of Electrical and Electronics Engineers, Inc. 6.\" 7.\" %sccs.include.redist.roff% 8.\" 9.\" @(#)cksum.1 8.1 (Berkeley) 06/29/93 10.\" 11.Dd 12.Dt CKSUM 1 13.Os BSD 4.4 14.Sh NAME 15.Nm cksum 16.Nd display file checksums and block counts 17.Sh SYNOPSIS 18.Nm cksum 19.Op Fl o Op \&1 \&| \&2 20.Op Ar file ... 21.Sh DESCRIPTION 22The 23.Nm cksum 24utility writes to the standard output three whitespace separated 25fields for each input file. 26These fields are a checksum 27.Tn CRC , 28the total number of octets in the file and the file name. 29If no file name is specified, the standard input is used and no file name 30is written. 31.Pp 32The options are as follows: 33.Bl -tag -width indent 34.It Fl o 35Use historic algorithms instead of the (superior) default one. 36.Pp 37Algorithm 1 is the algorithm used by historic 38.Bx 39systems as the 40.Xr sum 1 41algorithm and by historic 42.At V 43systems as the 44.Xr sum 45algorithm when using the 46.Fl r 47option. 48This is a 16-bit checksum, with a right rotation before each addition; 49overflow is discarded. 50.Pp 51Algorithm 2 is the algorithm used by historic 52.At V 53systems as the 54default 55.Xr sum 56algorithm. 57This is a 32-bit checksum, and is defined as follows: 58.Bd -unfilled -offset indent 59s = sum of all bytes; 60r = s % 2^16 + (s % 2^32) / 2^16; 61cksum = (r % 2^16) + r / 2^16; 62.Ed 63.Pp 64Both algorithm 1 and 2 write to the standard output the same fields as 65the default algorithm except that the size of the file in bytes is 66replaced with the size of the file in blocks. 67For historic reasons, the block size is 1024 for algorithm 1 and 512 68for algorithm 2. 69Partial blocks are rounded up. 70.El 71.Pp 72The default 73.Tn CRC 74used is based on the polynomial used for 75.Tn CRC 76error checking 77in the networking standard 78.St -iso8802-3 79The 80.Tn CRC 81checksum encoding is defined by the generating polynomial: 82.Pp 83.Bd -unfilled -offset indent 84G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + 85 x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 86.Ed 87.Pp 88Mathematically, the 89.Tn CRC 90value corresponding to a given file is defined by 91the following procedure: 92.Bd -filled -offset indent 93The 94.Ar n 95bits to be evaluated are considered to be the coefficients of a mod 2 96polynomial M(x) of degree 97.Ar n Ns \-1 . 98These 99.Ar n 100bits are the bits from the file, with the most significant bit being the most 101significant bit of the first octet of the file and the last bit being the least 102significant bit of the last octet, padded with zero bits (if necessary) to 103achieve an integral number of octets, followed by one or more octets 104representing the length of the file as a binary value, least significant octet 105first. 106The smallest number of octets capable of representing this integer are used. 107.Pp 108M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by 109G(x) using mod 2 division, producing a remainder R(x) of degree <= 31. 110.Pp 111The coefficients of R(x) are considered to be a 32-bit sequence. 112.Pp 113The bit sequence is complemented and the result is the CRC. 114.Ed 115.Pp 116The 117.Nm cksum 118utility exits 0 on success, and >0 if an error occurs. 119.Sh SEE ALSO 120The default calculation is identical to that given in pseudo-code 121in the following 122.Tn ACM 123article. 124.Rs 125.%T "Computation of Cyclic Redundancy Checks Via Table Lookup" 126.%A Dilip V. Sarwate 127.%J "Communications of the \\*(tNACM\\*(sP" 128.%D "August 1988" 129.Re 130.Sh STANDARDS 131The 132.Nm cksum 133utility is expected to be POSIX 1003.2 compatible. 134.Sh HISTORY 135The 136.Nm cksum 137utility appears in 138.Bx 4.4 . 139