checksum

package
v0.48.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package checksum provides functions for working with checksums. It uses crc32 for the checksum algorithm, resulting in checksum values like "3af3aaad".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rand

func Rand() string

Rand returns a random checksum.

func Read

func Read(r io.Reader) (map[string]Checksum, error)

Read reads checksums lines from r, returning a map of checksums keyed by name. Empty lines, and lines beginning with "#" (comments) are ignored. This function is the inverse of Write.

func ReadFile

func ReadFile(path string) (map[string]Checksum, error)

ReadFile reads a checksum file from path.

See Read for details.

func Sum

func Sum(b []byte) string

Sum returns the hash of b as a hex string. If b is empty, empty string is returned.

func SumAll

func SumAll[T ~string](a T, b ...T) string

SumAll returns the hash of a, and all the elements of b, as a hex string.

func Write

func Write(w io.Writer, sum Checksum, name string) error

Write appends a checksum line to w, including a newline. The typical format is:

<sum>     <name>
3610a686  file.txt

However, the checksum be any string value. Use ForFile to calculate a checksum, and Read to read this format.

func WriteFile

func WriteFile(path string, sum Checksum, name string) error

WriteFile writes a single {checksum,name} to path, overwriting the previous contents.

See: Write.

Types

type Checksum

type Checksum string

Checksum is a checksum value.

func ForFile

func ForFile(path string) (Checksum, error)

ForFile returns a checksum of the file at path. The checksum is based on the file's name, size, mode, and modification time. File contents are not read.

func ForHTTPHeader deprecated

func ForHTTPHeader(u string, header http.Header) Checksum

ForHTTPHeader returns a checksum generated from URL u and the contents of header. If the header contains an Etag, that is used as the primary element. Otherwise, other values such as Content-Length and Last-Modified are considered.

Deprecated: use ForHTTPResponse instead.

func ForHTTPResponse

func ForHTTPResponse(resp *http.Response) Checksum

ForHTTPResponse returns a checksum generated from the response's request URL and the contents of the response's header. If the header contains an Etag, that is used as the primary element. Otherwise, other values such as Content-Length and Last-Modified are considered.

There's some trickiness with Etag. Note that by default, stdlib http.Client will set sneakily set the "Accept-Encoding: gzip" header on GET requests. However, this doesn't happen for HEAD requests. So, comparing a GET and HEAD response for the same URL may result in different checksums, because the server will likely return a different Etag for the gzipped response.

# With gzip
Etag: "069dbf690a12d5eb853feb8e04aeb49e-ssl-df"

# Without gzip
Etag: "069dbf690a12d5eb853feb8e04aeb49e-ssl"

Note the "-ssl-df" suffix on the gzipped response. The "df" suffix is for "deflate".

The solution here might be to always explicitly set the gzip header on all requests. However, when gzip is not explicitly set, the stdlib client transparently handles gzip compression, including on the body read end. So, ideally, we wouldn't change that part, so that we don't have to code for both compressed and uncompressed responses.

Our hack for now it to trim the "-df" suffix from the Etag.

REVISIT: ForHTTPResponse is no longer used. It should be removed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL