> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/cksum.1.md).

# cksum.1

`cksum`, `sum` — 显示文件校验和与块计数

## 名称

`cksum`, `sum`

## 概要

`cksum [-o 1 | 2 | 3] [file ...]`

`sum [file ...]`

## 描述

`cksum` 实用程序为每个输入文件向标准输出写入三个以空白分隔的字段。这些字段分别是校验和 CRC、文件的总字节数和文件名。如果未指定文件名，则使用标准输入，且不写入文件名。

`sum` 实用程序与 `cksum` 实用程序相同，区别在于它默认使用历史算法 1，如下所述。提供它仅为兼容性。

选项如下：

**`-o`** 使用历史算法替代（更优的）默认算法。算法 1 是历史 BSD 系统用作 sum(1) 算法的算法，也是历史 AT\&T System V UNIX 系统在使用 `-r` 选项时用作 sum(1) 算法的算法。这是一个 16 位校验和，每次加法前进行右旋转；溢出被丢弃。算法 2 是历史 AT\&T System V UNIX 系统用作默认 sum(1) 算法的算法。这是一个 32 位校验和，定义如下：

```sh
s = sum of all bytes;
r = s % 2^16 + (s % 2^32) / 2^16;
cksum = (r % 2^16) + r / 2^16;
```

算法 3 是通常所称的 `32bit CRC` 算法。这是一个 32 位校验和。算法 1 和 2 都向标准输出写入与默认算法相同的字段，但文件大小（以字节为单位）被替换为文件大小（以块为单位）。由于历史原因，算法 1 的块大小为 1024，算法 2 的块大小为 512。不足一块的部分向上取整。

默认使用的 CRC 基于网络标准 ISO 8802-3 中用于 CRC 错误校验的多项式。CRC 校验和编码由以下生成多项式定义：

```sh
G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 +
     x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
```

从数学上讲，给定文件对应的 CRC 值由以下过程定义：

> 待计算的 `n` 个位被视为一个次数为 `n`-1 的 mod 2 多项式 M(x) 的系数。这 `n` 个位是文件中的位，最高有效位为文件第一个字节的最高有效位，最后一位为文件最后一个字节的最低有效位，必要时用零位填充以达到整数个字节，其后跟随一个或多个表示文件长度的字节（以二进制值表示，最低有效字节在前）。使用能够表示该整数的最少字节数。
>
> M(x) 乘以 x^32（即左移 32 位），然后使用 mod 2 除法除以 G(x)，产生次数 <= 31 的余数 R(x)。R(x) 的系数被视为一个 32 位序列。该位序列取反，结果即为 CRC。

## 退出状态

`cksum` 和 `sum` 实用程序成功时退出值为 0，发生错误时大于 0。

## 实例

计算所有可用算法的校验和。注意大小表示的差异（算法 1 和 2 以字节为单位，算法 3 和默认算法以块为单位）：

```sh
$ echo "hello" | cksum
3015617425 6
$ echo "hello" | cksum -o 1
36979 1
$ echo "hello" | cksum -o 2
542 1
$ echo "hello" | cksum -o 3
909783072 6
```

## 参见

[md5(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/md5.1.md)

默认计算与以下 ACM 文章中给出的伪代码相同。

> Dilip V. Sarwate, "Computation of Cyclic Redundancy Checks Via Table Lookup", *Communications of the ACM*, August 1988.

## 标准

`cksum` 实用程序预期符合 ISO/IEC 9945-2:1993 ("POSIX.2") 规范。

## 历史

`cksum` 实用程序首次出现于 4.4BSD。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/cksum.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
