sum (Unix)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Template:Short description

Script error: No such module "Infobox".Template:Template other Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters". Template:Mono is a legacy utility available on some Unix and Unix-like operating systems. This utility outputs a 16-bit checksum of each argument file, as well as the number of blocks they take on disk.[1] Two different checksum algorithms are in use. POSIX abandoned sum in favor of cksum.

Overview

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

The default algorithm on FreeBSD and GNU implementations is the 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 above algorithm appeared in Seventh Edition Unix.

The System V sum, -s in GNU sum and -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 Template:Mono utility is invoked from the command line according to the following syntax:

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

with the possible option parameters being:

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

Example of use:

$ echo Hello > testfile
$ sum testfile
36978     1

Example of -s use in GNU sum:

$ echo Hello > testfile
$ sum -s testfile
510 1 testfile

Example of using standard input, -r and printf to avoid newline:

$ printf Hello | sum -r
08401     1

See also

References

<templatestyles src="Reflist/styles.css" />

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

Script error: No such module "Check for unknown parameters".

External links

Script error: No such module "Navbox". Template:Core Utilities commands