xref: /freebsd/usr.bin/cksum/cksum.1 (revision 70445a80)
19b50d902SRodney W. Grimes.\" Copyright (c) 1991, 1993
29b50d902SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
39b50d902SRodney W. Grimes.\"
49b50d902SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
59b50d902SRodney W. Grimes.\" the Institute of Electrical and Electronics Engineers, Inc.
69b50d902SRodney W. Grimes.\"
79b50d902SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes.\" modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes.\" are met:
109b50d902SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes.\"    without specific prior written permission.
189b50d902SRodney W. Grimes.\"
199b50d902SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes.\" SUCH DAMAGE.
309b50d902SRodney W. Grimes.\"
3170445a80SFernando Apesteguía.Dd January 18, 2024
329b50d902SRodney W. Grimes.Dt CKSUM 1
3362500372SRuslan Ermilov.Os
349b50d902SRodney W. Grimes.Sh NAME
35a684cccdSMike Pritchard.Nm cksum ,
36a684cccdSMike Pritchard.Nm sum
379b50d902SRodney W. Grimes.Nd display file checksums and block counts
389b50d902SRodney W. Grimes.Sh SYNOPSIS
398fe908efSRuslan Ermilov.Nm
40f4ddfb38SRuslan Ermilov.Op Fl o Ar 1 | 2 | 3
41f74fd6ecSPhilippe Charnier.Op Ar
422cbd6933SBruce Evans.Nm sum
43f74fd6ecSPhilippe Charnier.Op Ar
449b50d902SRodney W. Grimes.Sh DESCRIPTION
459b50d902SRodney W. GrimesThe
46f74fd6ecSPhilippe Charnier.Nm
479b50d902SRodney W. Grimesutility writes to the standard output three whitespace separated
489b50d902SRodney W. Grimesfields for each input file.
499b50d902SRodney W. GrimesThese fields are a checksum
509b50d902SRodney W. Grimes.Tn CRC ,
519b50d902SRodney W. Grimesthe total number of octets in the file and the file name.
529b50d902SRodney W. GrimesIf no file name is specified, the standard input is used and no file name
539b50d902SRodney W. Grimesis written.
549b50d902SRodney W. Grimes.Pp
552cbd6933SBruce EvansThe
562cbd6933SBruce Evans.Nm sum
572cbd6933SBruce Evansutility is identical to the
58f74fd6ecSPhilippe Charnier.Nm
592cbd6933SBruce Evansutility, except that it defaults to using historic algorithm 1, as
602cbd6933SBruce Evansdescribed below.
612cbd6933SBruce EvansIt is provided for compatibility only.
622cbd6933SBruce Evans.Pp
639b50d902SRodney W. GrimesThe options are as follows:
649b50d902SRodney W. Grimes.Bl -tag -width indent
659b50d902SRodney W. Grimes.It Fl o
669b50d902SRodney W. GrimesUse historic algorithms instead of the (superior) default one.
679b50d902SRodney W. Grimes.Pp
689b50d902SRodney W. GrimesAlgorithm 1 is the algorithm used by historic
699b50d902SRodney W. Grimes.Bx
709b50d902SRodney W. Grimessystems as the
719b50d902SRodney W. Grimes.Xr sum 1
729b50d902SRodney W. Grimesalgorithm and by historic
739b50d902SRodney W. Grimes.At V
749b50d902SRodney W. Grimessystems as the
75f74fd6ecSPhilippe Charnier.Xr sum 1
769b50d902SRodney W. Grimesalgorithm when using the
779b50d902SRodney W. Grimes.Fl r
789b50d902SRodney W. Grimesoption.
799b50d902SRodney W. GrimesThis is a 16-bit checksum, with a right rotation before each addition;
809b50d902SRodney W. Grimesoverflow is discarded.
819b50d902SRodney W. Grimes.Pp
829b50d902SRodney W. GrimesAlgorithm 2 is the algorithm used by historic
839b50d902SRodney W. Grimes.At V
849b50d902SRodney W. Grimessystems as the
859b50d902SRodney W. Grimesdefault
86f74fd6ecSPhilippe Charnier.Xr sum 1
879b50d902SRodney W. Grimesalgorithm.
889b50d902SRodney W. GrimesThis is a 32-bit checksum, and is defined as follows:
899b50d902SRodney W. Grimes.Bd -unfilled -offset indent
909b50d902SRodney W. Grimess = sum of all bytes;
919b50d902SRodney W. Grimesr = s % 2^16 + (s % 2^32) / 2^16;
929b50d902SRodney W. Grimescksum = (r % 2^16) + r / 2^16;
939b50d902SRodney W. Grimes.Ed
949b50d902SRodney W. Grimes.Pp
9568d8bb38SPoul-Henning KampAlgorithm 3 is what is commonly called the
9668d8bb38SPoul-Henning Kamp.Ql 32bit CRC
9768d8bb38SPoul-Henning Kampalgorithm.
9868d8bb38SPoul-Henning KampThis is a 32-bit checksum.
9968d8bb38SPoul-Henning Kamp.Pp
1009b50d902SRodney W. GrimesBoth algorithm 1 and 2 write to the standard output the same fields as
1019b50d902SRodney W. Grimesthe default algorithm except that the size of the file in bytes is
1029b50d902SRodney W. Grimesreplaced with the size of the file in blocks.
1039b50d902SRodney W. GrimesFor historic reasons, the block size is 1024 for algorithm 1 and 512
1049b50d902SRodney W. Grimesfor algorithm 2.
1059b50d902SRodney W. GrimesPartial blocks are rounded up.
1069b50d902SRodney W. Grimes.El
1079b50d902SRodney W. Grimes.Pp
1089b50d902SRodney W. GrimesThe default
1099b50d902SRodney W. Grimes.Tn CRC
1109b50d902SRodney W. Grimesused is based on the polynomial used for
1119b50d902SRodney W. Grimes.Tn CRC
1129b50d902SRodney W. Grimeserror checking
1139b50d902SRodney W. Grimesin the networking standard
114f74fd6ecSPhilippe Charnier.St -iso8802-3 .
1159b50d902SRodney W. GrimesThe
1169b50d902SRodney W. Grimes.Tn CRC
1179b50d902SRodney W. Grimeschecksum encoding is defined by the generating polynomial:
1189b50d902SRodney W. Grimes.Bd -unfilled -offset indent
1199b50d902SRodney W. GrimesG(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
1209b50d902SRodney W. Grimes     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
1219b50d902SRodney W. Grimes.Ed
1229b50d902SRodney W. Grimes.Pp
1239b50d902SRodney W. GrimesMathematically, the
1249b50d902SRodney W. Grimes.Tn CRC
1259b50d902SRodney W. Grimesvalue corresponding to a given file is defined by
1269b50d902SRodney W. Grimesthe following procedure:
12746eea498SRuslan Ermilov.Bd -ragged -offset indent
1289b50d902SRodney W. GrimesThe
1299b50d902SRodney W. Grimes.Ar n
1309b50d902SRodney W. Grimesbits to be evaluated are considered to be the coefficients of a mod 2
1319b50d902SRodney W. Grimespolynomial M(x) of degree
1329b50d902SRodney W. Grimes.Ar n Ns \-1 .
1339b50d902SRodney W. GrimesThese
1349b50d902SRodney W. Grimes.Ar n
1359b50d902SRodney W. Grimesbits are the bits from the file, with the most significant bit being the most
1369b50d902SRodney W. Grimessignificant bit of the first octet of the file and the last bit being the least
1379b50d902SRodney W. Grimessignificant bit of the last octet, padded with zero bits (if necessary) to
1389b50d902SRodney W. Grimesachieve an integral number of octets, followed by one or more octets
1399b50d902SRodney W. Grimesrepresenting the length of the file as a binary value, least significant octet
1409b50d902SRodney W. Grimesfirst.
1419b50d902SRodney W. GrimesThe smallest number of octets capable of representing this integer are used.
1429b50d902SRodney W. Grimes.Pp
1439b50d902SRodney W. GrimesM(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by
1449b50d902SRodney W. GrimesG(x) using mod 2 division, producing a remainder R(x) of degree <= 31.
1459b50d902SRodney W. Grimes.Pp
1469b50d902SRodney W. GrimesThe coefficients of R(x) are considered to be a 32-bit sequence.
1479b50d902SRodney W. Grimes.Pp
1489b50d902SRodney W. GrimesThe bit sequence is complemented and the result is the CRC.
1499b50d902SRodney W. Grimes.Ed
150a866e170SRuslan Ermilov.Sh EXIT STATUS
151d628d776SRuslan Ermilov.Ex -std cksum sum
15270445a80SFernando Apesteguía.Sh EXAMPLES
15370445a80SFernando ApesteguíaCompute the checksum for all available algorithms.
15470445a80SFernando ApesteguíaNotice the difference in the size representation (in bytes for algorithm 1 and 2
15570445a80SFernando Apesteguíaand in blocks for 3 and the default algorithm):
15670445a80SFernando Apesteguía.Bd -literal -offset indent
15770445a80SFernando Apesteguía$ echo "hello" | cksum
15870445a80SFernando Apesteguía3015617425 6
15970445a80SFernando Apesteguía$ echo "hello" | cksum -o 1
16070445a80SFernando Apesteguía36979 1
16170445a80SFernando Apesteguía$ echo "hello" | cksum -o 2
16270445a80SFernando Apesteguía542 1
16370445a80SFernando Apesteguía$ echo "hello" | cksum -o 3
16470445a80SFernando Apesteguía909783072 6
16570445a80SFernando Apesteguía.Ed
1669b50d902SRodney W. Grimes.Sh SEE ALSO
1672c0d39adSJohn-Mark Gurney.Xr md5 1
16898849115SRuslan Ermilov.Pp
1699b50d902SRodney W. GrimesThe default calculation is identical to that given in pseudo-code
1709b50d902SRodney W. Grimesin the following
1719b50d902SRodney W. Grimes.Tn ACM
1729b50d902SRodney W. Grimesarticle.
1739b50d902SRodney W. Grimes.Rs
1749b50d902SRodney W. Grimes.%T "Computation of Cyclic Redundancy Checks Via Table Lookup"
1759b50d902SRodney W. Grimes.%A Dilip V. Sarwate
1769b88faecSRuslan Ermilov.%J "Communications of the" Tn ACM
1779b50d902SRodney W. Grimes.%D "August 1988"
1789b50d902SRodney W. Grimes.Re
1799b50d902SRodney W. Grimes.Sh STANDARDS
1809b50d902SRodney W. GrimesThe
181f74fd6ecSPhilippe Charnier.Nm
182f8a42d82SPhilippe Charnierutility is expected to conform to
183f8a42d82SPhilippe Charnier.St -p1003.2-92 .
1849b50d902SRodney W. Grimes.Sh HISTORY
1859b50d902SRodney W. GrimesThe
186f74fd6ecSPhilippe Charnier.Nm
18793e0d62dSJohn-Mark Gurneyutility appeared in
1889b50d902SRodney W. Grimes.Bx 4.4 .
189