Jump to content

sum (Unix)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dan Polansky (talk | contribs) at 19:01, 8 July 2022 (state the two algorithms; provide more links, including to SUS and man pages). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
sum
Original author(s)Ken Thompson
Developer(s)AT&T Bell Laboratories
Initial releaseNovember 3, 1971; 53 years ago (1971-11-03)
Operating systemUnix, Unix-like, Inferno
PlatformCross-platform
TypeCommand
Licensecoreutils: GPLv3+

sum is a legacy utility available on some Unix and Unix-like operating systems. This utility outputs the checksum of each argument file, as well as the number of blocks they take on disk.[1]

Overview

The sum program is generally only useful for historical interest. It is not part of POSIX. Two algorithms are typically available: a 16-bit BSD checksum and a 32-bit SYSV checksum. Both are weaker than the (already weak) CRC32 used by cksum.[2]

The default algorithm on FreeBSD and GNU implementations is the weaker BSD checksum. Switching between the two algorithms is done via command line options.[2][1]

The two commonly used algorithms are as follows.

The BSD sum, -r in GNU sum and -o1 in FreeBSD cksum:

  • Initialize checksum to 0
  • For each byte of the input stream
    • Perform 16-bit bitwise right rotation by 1 bit on the checksum
    • Add the byte to the checksum, and apply modulo 2 ^ 16 to the result, thereby keeping it within 16 bits
  • The result is a 16-bit checksum

The System V sum, -s in GNU sum -o2 in FreeBSD cksum:

  • checksum0 = sum of all bytes of the input stream modulo 2 ^ 32
  • checksum1 = checksum0 modulo 2 ^ 16 + checksum0 / 2 ^ 16
  • checksum = checksum1 modulo 2 ^16 + checksum1 / 2 ^ 16
  • The result is a 16-bit checksum calculated from the initial 32-bit plain byte sum

Syntax

The sum utility is invoked from the command line according to the following syntax:

sum [OPTION]... [FILE]...

with the possible option parameters being:

  • -r
  • -s, --sysv
  • --help
    • display the help screen and exit
  • --version
    • output version information and exit

When no file parameter is given, or when FILE is -, the standard input is used as input file.

See also

References

  1. ^ a b sum(1) — manual pages from GNU coreutils
  2. ^ a b sum(1) – FreeBSD General Commands Manual