1.\" $NetBSD: cksum.1,v 1.50 2019/12/15 04:50:28 christos Exp $ 2.\" 3.\" Copyright (c) 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the Institute of Electrical and Electronics Engineers, Inc. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.\" @(#)cksum.1 8.2 (Berkeley) 4/28/95 34.\" 35.Dd December 14, 2019 36.Dt CKSUM 1 37.Os 38.Sh NAME 39.Nm cksum , 40.Nm md2 , 41.Nm md4 , 42.Nm md5 , 43.Nm rmd160 , 44.Nm sha1 , 45.Nm sha256 , 46.Nm sha384 , 47.Nm sha512 , 48.Nm sum 49.Nd display file checksums and block counts 50.Sh SYNOPSIS 51.Nm cksum 52.Op Fl n 53.Op Fl a Ar algorithm Oo Fl pqtx Oc Oo Fl s Ar string Oc 54.Op Fl o Ar 1 Ns | Ns Ar 2 55.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 56.Nm sum 57.Op Fl n 58.Op Fl a Ar algorithm Oo Fl pqtx Oc Oo Fl s Ar string Oc 59.Op Fl o Ar 1 Ns | Ns Ar 2 60.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 61.Nm md2 62.Op Fl npqtx 63.Op Fl s Ar string 64.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 65.Nm md4 66.Op Fl npqtx 67.Op Fl s Ar string 68.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 69.Nm md5 70.Op Fl npqtx 71.Op Fl s Ar string 72.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 73.Nm rmd160 74.Op Fl npqtx 75.Op Fl s Ar string 76.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 77.Nm sha1 78.Op Fl npqtx 79.Op Fl s Ar string 80.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 81.Nm sha256 82.Op Fl npqtx 83.Op Fl s Ar string 84.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 85.Nm sha384 86.Op Fl npqtx 87.Op Fl s Ar string 88.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 89.Nm sha512 90.Op Fl npqtx 91.Op Fl s Ar string 92.Op Ar Li \&| Fl c Oo Fl w Oc Oo Ar sumfile Oc 93.Sh DESCRIPTION 94The 95.Nm 96utility writes to the standard output three whitespace separated 97fields for each input file. 98These fields are a checksum 99.Tn CRC , 100the total number of octets in the file and the file name. 101If no file name is specified, the standard input is used and no file name 102is written. 103.Pp 104The 105.Nm sum 106utility is identical to the 107.Nm 108utility, except that it defaults to using historic algorithm 1, as 109described below. 110It is provided for compatibility only. 111.Pp 112The 113.Nm md2 , 114.Nm md4 , 115.Nm md5 , 116.Nm sha1 , 117.Nm sha256 , 118.Nm sha384 , 119.Nm sha512 , 120and 121.Nm rmd160 122utilities compute cryptographic hash functions, and write to standard 123output the hexadecimal representation of the hash of their input. 124.Pp 125The options are as follows: 126.Bl -tag -width indent 127.It Fl a Ar algorithm 128When invoked as 129.Nm cksum , 130use the specified 131.Ar algorithm . 132Valid algorithms are: 133.Bl -column -offset indent ".Sy Algorithm" ".Sy Bits" ".Sy Description" 134.It Sy Algorithm Ta Sy Bits Ta Sy Description 135.It Li CRC Ta 32 Ta Default CRC algorithm 136.It Li MD2 Ta 128 Ta MD2, per Li RFC1319 137.It Li MD4 Ta 128 Ta MD4, per Li RFC1320 138.It Li MD5 Ta 128 Ta MD5, per Li RFC1321 139.It Li RMD160 Ta 160 Ta RIPEMD-160 140.It Li SHA1 Ta 160 Ta SHA-1, per Li FIPS PUB 180-1 141.It Li SHA256 Ta 256 Ta SHA-2 142.It Li SHA384 Ta 384 Ta SHA-2 143.It Li SHA512 Ta 512 Ta SHA-2 144.It Li old1 Ta 16 Ta Algorithm 1, per Fl o Ar 1 145.It Li old2 Ta 16 Ta Algorithm 2, per Fl o Ar 2 146.El 147.It Fl c Op Ar sumfile 148Verify (check) files against a list of checksums. 149The list is read from 150.Ar sumfile , 151or from stdin if no filename is given. 152E.g. first run 153.Dl Ic md5 *.tgz > MD5 154.Dl Ic sha1 *.tgz > SHA1 155to generate a list of MD5 checksums in 156.Pa MD5 , 157then use the following command to verify them: 158.Dl Ic cat MD5 SHA1 | cksum -c 159If an error is found during checksum verification, an error 160message is printed, and the program returns an error code of 1. 161.It Fl o 162Use historic algorithms instead of the (superior) default one. 163.Pp 164Algorithm 1 is the algorithm used by historic 165.Bx 166systems as the 167.Xr sum 1 168algorithm and by historic 169.At V 170systems as the 171.Xr sum 1 172algorithm when using the 173.Fl r 174option. 175This is a 16-bit checksum, with a right rotation before each addition; 176overflow is discarded. 177.Pp 178Algorithm 2 is the algorithm used by historic 179.At V 180systems as the 181default 182.Xr sum 1 183algorithm. 184This is a 32-bit checksum, and is defined as follows: 185.Bd -unfilled -offset indent 186s = sum of all bytes; 187r = s % 2^16 + (s % 2^32) / 2^16; 188cksum = (r % 2^16) + r / 2^16; 189.Ed 190.Pp 191Both algorithm 1 and 2 write to the standard output the same fields as 192the default algorithm except that the size of the file in bytes is 193replaced with the size of the file in blocks. 194For historic reasons, the block size is 1024 for algorithm 1 and 512 195for algorithm 2. 196Partial blocks are rounded up. 197.It Fl w 198Print warnings about malformed checksum files when verifying 199checksums with 200.Fl c . 201.El 202.Pp 203The following options apply only when using the one of the message 204digest algorithms: 205.Bl -tag -width indent 206.It Fl n 207Print the hash and the filename in the normal sum output form, with 208the hash at the left and the filename following on the right. 209.It Fl p 210Echo input from standard input to standard output, and append the 211selected message digest. 212.It Fl q 213Quiet mode \(em only the checksum is printed out. 214Overrides the 215.Fl n 216option. 217.It Fl s Ar string 218Print the hash of the given string 219.Ar string . 220.It Fl t 221Run a built-in message digest time trial. 222.It Fl x 223Run a built-in message digest test script. 224The tests that are run 225are supposed to encompass all the various tests in the suites that 226accompany the algorithms' descriptions with the exception of the 227last test for the SHA-1 algorithm and the RIPEMD-160 algorithm. 228The 229last test for these is one million copies of the lower letter a. 230.El 231.Pp 232The default 233.Tn CRC 234used is based on the polynomial used for 235.Tn CRC 236error checking 237in the networking standard 238.St -iso8802-3 . 239The 240.Tn CRC 241checksum encoding is defined by the generating polynomial: 242.Pp 243.Bd -unfilled -offset indent 244G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + 245 x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 246.Ed 247.Pp 248Mathematically, the 249.Tn CRC 250value corresponding to a given file is defined by 251the following procedure: 252.Bd -filled -offset indent 253The 254.Ar n 255bits to be evaluated are considered to be the coefficients of a mod 2 256polynomial M(x) of degree 257.Ar n Ns \-1 . 258These 259.Ar n 260bits are the bits from the file, with the most significant bit being the most 261significant bit of the first octet of the file and the last bit being the least 262significant bit of the last octet, padded with zero bits (if necessary) to 263achieve an integral number of octets, followed by one or more octets 264representing the length of the file as a binary value, least significant octet 265first. 266The smallest number of octets capable of representing this integer are used. 267.Pp 268M(x) is multiplied by x^32 (i.e., shifted left 32 bits) and divided by 269G(x) using mod 2 division, producing a remainder R(x) of degree \*[Le] 31. 270.Pp 271The coefficients of R(x) are considered to be a 32-bit sequence. 272.Pp 273The bit sequence is complemented and the result is the CRC. 274.Ed 275.Pp 276The 277.Nm 278and 279.Nm sum 280utilities exit 0 on success, and >0 if an error occurs. 281.Sh SEE ALSO 282.Xr openssl 1 , 283.Xr mtree 8 284.Pp 285The default calculation is identical to that given in pseudo-code 286in the following 287.Tn ACM 288article. 289.Rs 290.%T "Computation of Cyclic Redundancy Checks Via Table Lookup" 291.%A Dilip V. Sarwate 292.%J "Communications of the ACM" 293.%D "August 1988" 294.Re 295.Rs 296.%A R. Rivest 297.%T The MD2 Message-Digest Algorithm 298.%O RFC 1319 299.Re 300.Rs 301.%A R. Rivest 302.%T The MD4 Message-Digest Algorithm 303.%O RFC 1186 and RFC 1320 304.Re 305.Rs 306.%A R. Rivest 307.%T The MD5 Message-Digest Algorithm 308.%O RFC 1321 309.Re 310.Rs 311.%A U.S. DOC/NIST 312.%T Secure Hash Standard 313.%O FIPS PUB 180-1 314.Re 315.Sh STANDARDS 316The 317.Nm 318utility is expected to conform to 319.St -p1003.1-2004 . 320.Sh HISTORY 321The 322.Nm 323utility appeared in 324.Bx 4.4 . 325.Nm md5 326was added in 327.Nx 1.3 . 328The functionality for 329.Nm md2 , 330.Nm md4 , 331.Nm sha1 , 332and 333.Nm rmd160 334was added in 335.Nx 1.6 . 336Support for the SHA-2 algorithms 337.Po 338.Li SHA256 , 339.Li SHA384 , 340and 341.Li SHA512 342.Pc 343was added in 344.Nx 3.0 . 345The functionality to verify checksum stored in a file 346.Pq Fl c 347first appeared in 348.Nx 4.0 . 349Quiet mode 350.Pq Fl q 351was added in 352.Nx 7.0 . 353.\" .Pp 354.\" The 355.\" .Nm sum 356.\" utility appeared in 357.\" .Bx ?.? 358.\" and 359.\" .At V . 360