hash

package
v0.0.0-...-a6a3a47 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const MinIOMultipartChecksum = "x-minio-multipart-checksum"

MinIOMultipartChecksum is as metadata on multipart uploads to indicate checksum type.

Variables

View Source
var ErrInvalidChecksum = errors.New("invalid checksum")

ErrInvalidChecksum is returned when an invalid checksum is provided in headers.

Functions

func AddChecksumHeader

func AddChecksumHeader(w http.ResponseWriter, c map[string]string)

AddChecksumHeader will transfer any checksum value that has been checked.

func IsChecksumMismatch

func IsChecksumMismatch(err error) bool

IsChecksumMismatch matches if 'err' is hash.ChecksumMismatch

func ReadCheckSums

func ReadCheckSums(b []byte, part int) map[string]string

ReadCheckSums will read checksums from b and return them.

func TransferChecksumHeader

func TransferChecksumHeader(w http.ResponseWriter, r *http.Request)

TransferChecksumHeader will transfer any checksum value that has been checked. If checksum was trailing, they must have been added to r.Trailer.

Types

type BadDigest

type BadDigest struct {
	ExpectedMD5   string
	CalculatedMD5 string
}

BadDigest - Content-MD5 you specified did not match what we received.

func (BadDigest) Error

func (e BadDigest) Error() string

type Checksum

type Checksum struct {
	Type    ChecksumType
	Encoded string
	Raw     []byte
}

Checksum is a type and base 64 encoded value.

func GetContentChecksum

func GetContentChecksum(h http.Header) (*Checksum, error)

GetContentChecksum returns content checksum. Returns ErrInvalidChecksum if so. Returns nil, nil if no checksum.

func NewChecksumFromData

func NewChecksumFromData(t ChecksumType, data []byte) *Checksum

NewChecksumFromData returns a new checksum from specified algorithm and base64 encoded value.

func NewChecksumString

func NewChecksumString(alg, value string) *Checksum

NewChecksumString returns a new checksum from specified algorithm and base64 encoded value.

func NewChecksumWithType

func NewChecksumWithType(alg ChecksumType, value string) *Checksum

NewChecksumWithType is similar to NewChecksumString but expects input algo of ChecksumType.

func (*Checksum) AppendTo

func (c *Checksum) AppendTo(b []byte, parts []byte) []byte

AppendTo will append the checksum to b. 'parts' is used when checksum has ChecksumMultipart set. ReadCheckSums reads the values back.

func (*Checksum) AsMap

func (c *Checksum) AsMap() map[string]string

AsMap returns the

func (Checksum) Matches

func (c Checksum) Matches(content []byte) error

Matches returns whether given content matches c.

func (Checksum) Valid

func (c Checksum) Valid() bool

Valid returns whether checksum is valid.

type ChecksumMismatch

type ChecksumMismatch struct {
	Want string
	Got  string
}

ChecksumMismatch - when content checksum does not match with what was sent from client.

func (ChecksumMismatch) Error

func (e ChecksumMismatch) Error() string

type ChecksumType

type ChecksumType uint32

ChecksumType contains information about the checksum type.

const (

	// ChecksumTrailing indicates the checksum will be sent in the trailing header.
	// Another checksum type will be set.
	ChecksumTrailing ChecksumType = 1 << iota

	// ChecksumSHA256 indicates a SHA256 checksum.
	ChecksumSHA256
	// ChecksumSHA1 indicates a SHA-1 checksum.
	ChecksumSHA1
	// ChecksumCRC32 indicates a CRC32 checksum with IEEE table.
	ChecksumCRC32
	// ChecksumCRC32C indicates a CRC32 checksum with Castagnoli table.
	ChecksumCRC32C
	// ChecksumInvalid indicates an invalid checksum.
	ChecksumInvalid
	// ChecksumMultipart indicates the checksum is from a multipart upload.
	ChecksumMultipart
	// ChecksumIncludesMultipart indicates the checksum also contains part checksums.
	ChecksumIncludesMultipart

	// ChecksumNone indicates no checksum.
	ChecksumNone ChecksumType = 0
)

func NewChecksumType

func NewChecksumType(alg string) ChecksumType

NewChecksumType returns a checksum type based on the algorithm string.

func (ChecksumType) Hasher

func (c ChecksumType) Hasher() hash.Hash

Hasher returns a hasher corresponding to the checksum type. Returns nil if no checksum.

func (ChecksumType) Is

func (c ChecksumType) Is(t ChecksumType) bool

Is returns if c is all of t.

func (ChecksumType) IsSet

func (c ChecksumType) IsSet() bool

IsSet returns whether the type is valid and known.

func (ChecksumType) Key

func (c ChecksumType) Key() string

Key returns the header key. returns empty string if invalid or none.

func (ChecksumType) RawByteLen

func (c ChecksumType) RawByteLen() int

RawByteLen returns the size of the un-encoded checksum.

func (ChecksumType) String

func (c ChecksumType) String() string

String returns the type as a string.

func (ChecksumType) Trailing

func (c ChecksumType) Trailing() bool

Trailing return whether the checksum is traling.

type Options

type Options struct {
	MD5Hex     string
	SHA256Hex  string
	Size       int64
	ActualSize int64
	DisableMD5 bool
}

Options are optional arguments to NewReaderWithOpts, Options simply converts positional arguments to NewReader() into a more flexible way to provide optional inputs. This is currently used by the FanOut API call mostly to disable expensive md5sum calculation repeatedly under hash.Reader.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader wraps an io.Reader and computes the MD5 checksum of the read content as ETag. Optionally, it also computes the SHA256 checksum of the content.

If the reference values for the ETag and content SHA26 are not empty then it will check whether the computed match the reference values.

func NewReader

func NewReader(src io.Reader, size int64, md5Hex, sha256Hex string, actualSize int64) (*Reader, error)

NewReader returns a new Reader that wraps src and computes MD5 checksum of everything it reads as ETag.

It also computes the SHA256 checksum of everything it reads if sha256Hex is not the empty string.

If size resp. actualSize is unknown at the time of calling NewReader then it should be set to -1. When size is >=0 it *must* match the amount of data provided by r.

NewReader may try merge the given size, MD5 and SHA256 values into src - if src is a Reader - to avoid computing the same checksums multiple times. NewReader enforces S3 compatibility strictly by ensuring caller does not send more content than specified size.

func NewReaderWithOpts

func NewReaderWithOpts(src io.Reader, opts Options) (*Reader, error)

NewReaderWithOpts is like NewReader but takes `Options` as argument, allowing callers to indicate if they want to disable md5sum checksum.

func (*Reader) ActualSize

func (r *Reader) ActualSize() int64

ActualSize returns the pre-modified size of the object. DecompressedSize - For compressed objects.

func (*Reader) AddChecksum

func (r *Reader) AddChecksum(req *http.Request, ignoreValue bool) error

AddChecksum will add checksum checks as specified in https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html Returns ErrInvalidChecksum if a problem with the checksum is found.

func (*Reader) AddChecksumNoTrailer

func (r *Reader) AddChecksumNoTrailer(headers http.Header, ignoreValue bool) error

AddChecksumNoTrailer will add checksum checks as specified in https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html Returns ErrInvalidChecksum if a problem with the checksum is found.

func (*Reader) AddNonTrailingChecksum

func (r *Reader) AddNonTrailingChecksum(cs *Checksum, ignoreValue bool) error

AddNonTrailingChecksum will add a checksum to the reader. The checksum cannot be trailing.

func (*Reader) Close

func (r *Reader) Close() error

Close and release resources.

func (*Reader) ContentCRC

func (r *Reader) ContentCRC() map[string]string

ContentCRC returns the content crc if set.

func (*Reader) ContentCRCType

func (r *Reader) ContentCRCType() ChecksumType

ContentCRCType returns the content checksum type.

func (*Reader) ETag

func (r *Reader) ETag() etag.ETag

ETag returns the ETag computed by an underlying etag.Tagger. If the underlying io.Reader does not implement etag.Tagger it returns nil.

func (*Reader) MD5Current

func (r *Reader) MD5Current() []byte

MD5Current returns the MD5 checksum of the content that has been read so far.

Calling MD5Current again after reading more data may result in a different checksum.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

func (*Reader) SHA256

func (r *Reader) SHA256() []byte

SHA256 returns the SHA256 checksum set as reference value.

It corresponds to the checksum that is expected and not the actual SHA256 checksum of the content.

func (*Reader) SHA256HexString

func (r *Reader) SHA256HexString() string

SHA256HexString returns a hex representation of the SHA256.

func (*Reader) SetExpectedMax

func (r *Reader) SetExpectedMax(expectedMax int64)

SetExpectedMax set expected max data expected from reader

func (*Reader) SetExpectedMin

func (r *Reader) SetExpectedMin(expectedMin int64)

SetExpectedMin set expected minimum data expected from reader

func (*Reader) Size

func (r *Reader) Size() int64

Size returns the absolute number of bytes the Reader will return during reading. It returns -1 for unlimited data.

type SHA256Mismatch

type SHA256Mismatch struct {
	ExpectedSHA256   string
	CalculatedSHA256 string
}

SHA256Mismatch - when content sha256 does not match with what was sent from client.

func (SHA256Mismatch) Error

func (e SHA256Mismatch) Error() string

type SizeMismatch

type SizeMismatch struct {
	Want int64
	Got  int64
}

SizeMismatch error size mismatch

func (SizeMismatch) Error

func (e SizeMismatch) Error() string

type SizeTooLarge

type SizeTooLarge struct {
	Want int64
	Got  int64
}

SizeTooLarge reader size too large

func (SizeTooLarge) Error

func (e SizeTooLarge) Error() string

type SizeTooSmall

type SizeTooSmall struct {
	Want int64
	Got  int64
}

SizeTooSmall reader size too small

func (SizeTooSmall) Error

func (e SizeTooSmall) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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