xref: /freebsd/usr.bin/cksum/cksum.1 (revision 2cbd69330dae924bec2e53d2b1b2ab9fd9a6b608)
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.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"	@(#)cksum.1	8.2 (Berkeley) 4/28/95
36.\"	$Id: cksum.1,v 1.4 1997/06/25 07:02:00 charnier Exp $
37.\"
38.Dd April 28, 1995
39.Dt CKSUM 1
40.Os BSD 4.4
41.Sh NAME
42.Nm cksum
43.Nd display file checksums and block counts
44.Sh SYNOPSIS
45.Nm cksum
46.Op Fl o Ar \&1 No \&| Ar \&2
47.Op Ar file ...
48.Nm sum
49.Op Ar file ...
50.Sh DESCRIPTION
51The
52.Nm cksum
53utility writes to the standard output three whitespace separated
54fields for each input file.
55These fields are a checksum
56.Tn CRC ,
57the total number of octets in the file and the file name.
58If no file name is specified, the standard input is used and no file name
59is written.
60.Pp
61The
62.Nm sum
63utility is identical to the
64.Nm cksum
65utility, except that it defaults to using historic algorithm 1, as
66described below.
67It is provided for compatibility only.
68.Pp
69The options are as follows:
70.Bl -tag -width indent
71.It Fl o
72Use historic algorithms instead of the (superior) default one.
73.Pp
74Algorithm 1 is the algorithm used by historic
75.Bx
76systems as the
77.Xr sum 1
78algorithm and by historic
79.At V
80systems as the
81.Xr sum
82algorithm when using the
83.Fl r
84option.
85This is a 16-bit checksum, with a right rotation before each addition;
86overflow is discarded.
87.Pp
88Algorithm 2 is the algorithm used by historic
89.At V
90systems as the
91default
92.Xr sum
93algorithm.
94This is a 32-bit checksum, and is defined as follows:
95.Bd -unfilled -offset indent
96s = sum of all bytes;
97r = s % 2^16 + (s % 2^32) / 2^16;
98cksum = (r % 2^16) + r / 2^16;
99.Ed
100.Pp
101Both algorithm 1 and 2 write to the standard output the same fields as
102the default algorithm except that the size of the file in bytes is
103replaced with the size of the file in blocks.
104For historic reasons, the block size is 1024 for algorithm 1 and 512
105for algorithm 2.
106Partial blocks are rounded up.
107.El
108.Pp
109The default
110.Tn CRC
111used is based on the polynomial used for
112.Tn CRC
113error checking
114in the networking standard
115.St -iso8802-3
116The
117.Tn CRC
118checksum encoding is defined by the generating polynomial:
119.Pp
120.Bd -unfilled -offset indent
121G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
122     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
123.Ed
124.Pp
125Mathematically, the
126.Tn CRC
127value corresponding to a given file is defined by
128the following procedure:
129.Bd -filled -offset indent
130The
131.Ar n
132bits to be evaluated are considered to be the coefficients of a mod 2
133polynomial M(x) of degree
134.Ar n Ns \-1 .
135These
136.Ar n
137bits are the bits from the file, with the most significant bit being the most
138significant bit of the first octet of the file and the last bit being the least
139significant bit of the last octet, padded with zero bits (if necessary) to
140achieve an integral number of octets, followed by one or more octets
141representing the length of the file as a binary value, least significant octet
142first.
143The smallest number of octets capable of representing this integer are used.
144.Pp
145M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
146G(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
147.Pp
148The coefficients of R(x) are considered to be a 32-bit sequence.
149.Pp
150The bit sequence is complemented and the result is the CRC.
151.Ed
152.Pp
153The
154.Nm cksum
155and
156.Nm sum
157utilities exit 0 on success, and >0 if an error occurs.
158.Sh SEE ALSO
159.Xr md5 1
160.Rs
161The default calculation is identical to that given in pseudo-code
162in the following
163.Tn ACM
164article.
165.Rs
166.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
167.%A Dilip V. Sarwate
168.%J "Communications of the \\*(tNACM\\*(sP"
169.%D "August 1988"
170.Re
171.Sh STANDARDS
172The
173.Nm cksum
174utility is expected to conform to
175.St -p1003.2-92 .
176.Sh HISTORY
177The
178.Nm cksum
179utility appeared in
180.Bx 4.4 .
181