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