Documentation ¶
Overview ¶
Package digest provides a generalized representation for digests computed with cryptographic hash functions. It provides an efficient in-memory representation as well as serialization.
Index ¶
- Constants
- Variables
- func WriteDigest(w io.Writer, d Digest) (n int, err error)
- type Digest
- func (d Digest) Bytes() []byte
- func (d Digest) Expands(e Digest) bool
- func (d *Digest) GobDecode(p []byte) error
- func (d Digest) GobEncode() ([]byte, error)
- func (d Digest) Hash() crypto.Hash
- func (d Digest) Hex() string
- func (d Digest) HexN(n int) string
- func (d Digest) IsAbbrev() bool
- func (d Digest) IsShort() bool
- func (d Digest) IsZero() bool
- func (d Digest) Less(e Digest) bool
- func (d Digest) MarshalJSON() ([]byte, error)
- func (d *Digest) Mix(e Digest)
- func (d Digest) NPrefix() int
- func (d Digest) Name() string
- func (d Digest) Short() string
- func (d Digest) ShortString(n int) string
- func (d Digest) String() string
- func (d *Digest) Truncate(n int)
- func (d *Digest) UnmarshalJSON(b []byte) error
- type Digester
- func (d Digester) FromBytes(p []byte) Digest
- func (d Digester) FromDigests(digests ...Digest) Digest
- func (d Digester) FromString(s string) Digest
- func (d Digester) MarshalJSON() ([]byte, error)
- func (d Digester) New(b []byte) Digest
- func (d Digester) NewReader(source io.Reader) Reader
- func (d Digester) NewWriter() Writer
- func (d Digester) NewWriterAt(ctx context.Context, target io.WriterAt) *WriterAt
- func (d Digester) NewWriterShort() Writer
- func (d Digester) Parse(s string) (Digest, error)
- func (d Digester) Rand(r *mathrand.Rand) Digest
- func (d *Digester) UnmarshalJSON(b []byte) error
- type Reader
- type Writer
- type WriterAt
Constants ¶
const ( MD4 digestHash = 1 + iota // crypto.MD4 MD5 // crypto.MD5 SHA1 // crypto.SHA1 SHA224 // crypto.SHA224 SHA256 // crypto.SHA256 SHA384 // crypto.SHA384 SHA512 // crypto.SHA512 MD5SHA1 // crypto.MD5SHA1 RIPEMD160 // crypto.RIPEMD160 SHA3_224 // crypto.SHA3_224 SHA3_256 // crypto.SHA3_256 SHA3_384 // crypto.SHA3_384 SHA3_512 // crypto.SHA3_512 SHA512_224 // crypto.SHA512_224 SHA512_256 // crypto.SHA512_256 BLAKE2s_256 // crypto.BLAKE2s_256 BLAKE2b_256 // crypto.BLAKE2b_256 BLAKE2b_384 // crypto.BLAKE2b_384 BLAKE2b_512 // crypto.BLAKE2b_512 )
Variables ¶
var ( // An attempt was made to parse an invalid digest ErrInvalidDigest = errors.New("invalid digest") ErrHashUnavailable = errors.New("the requested hash function is not available") // The Digest's hash did not match the hash of the Digester. ErrWrongHash = errors.New("wrong hash") // An EOF was encountered while attempting to read a Digest. ErrShortRead = errors.New("short read") )
Functions ¶
func WriteDigest ¶
WriteDigest is a convenience function to write a (binary) Digest to an io.Writer. Its format is two bytes representing the hash function, followed by the hash value itself.
Writing a zero digest is disallowed; WriteDigest panics in this case.
Types ¶
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest represents a digest computed with a cryptographic hash function. It uses a fixed-size representation and is directly comparable.
func ReadDigest ¶
ReadDigest is a convenience function to read a (binary) Digest from an io.Reader, as written by WriteDigest.
func (Digest) HexN ¶
HexN returns the padded hexadecimal representation of the digest's first n bytes. N must be smaller or equal to the digest's size, or else it panics.
func (Digest) IsAbbrev ¶
IsAbbrev tells whether d is an "abbreviated" digest, comprising no more than half of the digest bytes.
func (Digest) IsShort ¶
IsShort tells whether d is a "short" digest, comprising only the initial 4 bytes.
func (Digest) Less ¶
Less defines an order of digests of the same hash. panics if two Less digests with different hashes are compared.
func (Digest) MarshalJSON ¶
MarshalJSON marshals the Digest into JSON format.
func (Digest) NPrefix ¶
NPrefix returns the number of nonzero leading bytes in the digest, after which the remaining bytes are zero.
func (Digest) Short ¶
Short returns a short (prefix) version of the Digest's hexadecimal representation.
func (Digest) ShortString ¶
ShortString returns a short representation of the digest, comprising the digest name and its first n bytes.
func (Digest) String ¶
String returns the full string representation of the digest: the digest name, followed by ":", followed by its hexadecimal value.
func (*Digest) Truncate ¶
Truncate truncates digest d to n bytes. Truncate panics if n is greater than the digest's hash size.
func (*Digest) UnmarshalJSON ¶
UnmarshalJSON unmarshals a digest from JSON data.
type Digester ¶
Digester computes digests based on a cryptographic hash function.
func (Digester) FromDigests ¶
FromDigests computes a Digest over other Digests.
func (Digester) FromString ¶
FromString computes a Digest from a string.
func (Digester) MarshalJSON ¶
MarshalJSON generates a JSON format byte slice from a Digester.
func (Digester) New ¶
New returns a new digest with the provided literal contents. New panics if the digest size does not match the hash function's length.
func (Digester) NewWriter ¶
NewWriter returns a Writer that can be used to compute Digests of long inputs.
func (Digester) NewWriterAt ¶
NewWriterAt creates a new WriterAt. The provided context is used to fail pending writes upon cancellation.
func (Digester) NewWriterShort ¶ added in v0.0.11
NewWriterShort returns a Writer that can be used to compute Digests of short inputs (ie, order of KBs)
func (Digester) Parse ¶
Parse parses a string into a Digest with the cryptographic hash of Digester. The input string is in the form of Digest.String, except that the hash name may be omitted--it is then instead assumed to be the hash function associated with the Digester.
func (Digester) Rand ¶
Rand returns a random digest generated by the random provided generator. If no generator is provided (r is nil), Rand uses the system's cryptographically secure random number generator.
func (*Digester) UnmarshalJSON ¶
UnmarshalJSON converts from a JSON format byte slice to a Digester.
type Reader ¶
Reader can be used to calculate the digest of a file as it is being read. It uses back pressure to stall reads when a block is missing. This can cause deadlock if the application doesn't retry immediately.
The s3manager uploader differentiates between two kinds of readers to improve upload performance: Simple Readers and "ReaderSeekers" for performance. This implementation creates either a simpleReaderAt or a readerAtSeeker depending on the underlying ReaderAt.
Expects the reads to be complete and non-overlapping.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer provides an io.Writer to which digested bytes are written and from which a Digest is produced.
type WriterAt ¶
type WriterAt struct {
// contains filtered or unexported fields
}
WriterAt can be used to calculate the digest of a file as it is being written. It uses back pressure to stall writes when a block is missing. WriterAt must be written to sequentially (otherwise a deadlock is possible); but it accepts re-writes of past regions so that the user can retry failures.
In particular, this matches the semantics for the S3 download manager from github.com/aws/aws-sdk-go; thus WriterAt can be used to speed up simultaneous download and integrity checking for objects stored in S3.