kbfshash

package
v5.3.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2020 License: BSD-3-Clause, BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 0

README

kbfshash

The Keybase hash type. The package is named kbfshash rather than hash for two reasons:

  1. To avoid conflicting with the hash package.
  2. Variables may also be named hash.

All errors returned by this package are wrapped with pkg/errors, and so need errors.Cause() to unwrap them.

Documentation

Index

Constants

View Source
const (
	// MinHashByteLength is the minimum number of bytes a valid
	// keybase hash can be, including the 1 byte for the type.
	MinHashByteLength = 33

	// DefaultHashByteLength is the number of bytes in a default
	// keybase hash.
	DefaultHashByteLength = 1 + sha256.Size

	// MaxHashByteLength is the maximum number of bytes a valid
	// keybase hash can be, including the 1 byte for the type.
	MaxHashByteLength = 129

	// MinHashStringLength is the minimum number of characters in
	// the string representation (hex encoding) of a valid keybase
	// hash.
	MinHashStringLength = 2 * MinHashByteLength

	// DefaultHashStringLength is the number of characters in the
	// string representation of a default keybase hash.
	DefaultHashStringLength = 2 * DefaultHashByteLength

	// MaxHashStringLength is the maximum number of characters the
	// string representation of a valid keybase hash can be.
	MaxHashStringLength = 2 * MaxHashByteLength
)

Variables

View Source
var DefaultHashNew = sha256.New

DefaultHashNew is a function that creates a new hash.Hash object with the default hash.

View Source
var MaxDefaultHash = RawDefaultHash{
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}

MaxDefaultHash is the maximum value of RawDefaultHash

Functions

func DoRawDefaultHash

func DoRawDefaultHash(p []byte) (HashType, RawDefaultHash)

DoRawDefaultHash computes the default keybase hash of the given data, and returns the type and the raw hash bytes.

Types

type HMAC

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

HMAC is the type of a keybase hash that is an HMAC.

All the constants for Hash also apply to HMAC.

func DefaultHMAC

func DefaultHMAC(key, buf []byte) (HMAC, error)

DefaultHMAC computes the HMAC with the given key of the given data using the default hash.

func (HMAC) Bytes

func (hmac HMAC) Bytes() []byte

Bytes returns the bytes of the HMAC.

func (HMAC) IsValid

func (hmac HMAC) IsValid() bool

IsValid returns whether the HMAC is valid. Note that an HMAC with an unknown version is still valid.

func (HMAC) MarshalBinary

func (hmac HMAC) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface for HMAC. Returns an error if the HMAC is invalid and not the zero HMAC.

func (HMAC) MarshalText

func (hmac HMAC) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for HMAC.

func (HMAC) String

func (hmac HMAC) String() string

func (*HMAC) UnmarshalBinary

func (hmac *HMAC) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for HMAC. Returns an error if the given byte array is non-empty and the HMAC is invalid.

func (*HMAC) UnmarshalText

func (hmac *HMAC) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for HMAC.

func (HMAC) Verify

func (hmac HMAC) Verify(key, buf []byte) error

Verify makes sure that the HMAC matches the given data.

type Hash

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

Hash is the type of a keybase hash.

func DefaultHash

func DefaultHash(buf []byte) (Hash, error)

DefaultHash computes the hash of the given data with the default hash type.

func DoHash

func DoHash(buf []byte, ht HashType) (Hash, error)

DoHash computes the hash of the given data with the given hash type.

func HashFromBytes

func HashFromBytes(data []byte) (Hash, error)

HashFromBytes creates a hash from the given byte array. If the returned error is nil, the returned Hash is valid.

func HashFromRaw

func HashFromRaw(hashType HashType, rawHash []byte) (Hash, error)

HashFromRaw creates a hash from a type and raw hash data. If the returned error is nil, the returned Hash is valid.

func HashFromString

func HashFromString(dataStr string) (Hash, error)

HashFromString creates a hash from the given string. If the returned error is nil, the returned Hash is valid.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes returns the bytes of the hash.

func (Hash) GetHashType

func (h Hash) GetHashType() HashType

GetHashType returns the type of this hash.

func (Hash) IsValid

func (h Hash) IsValid() bool

IsValid returns whether the hash is valid. Note that a hash with an unknown version is still valid.

func (Hash) MarshalBinary

func (h Hash) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface for Hash. Returns an error if the hash is invalid and not the zero hash.

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Hash.

func (*Hash) Size

func (h *Hash) Size() int

Size implements the cache.Measurable interface.

func (Hash) String

func (h Hash) String() string

func (*Hash) UnmarshalBinary

func (h *Hash) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for Hash. Returns an error if the given byte array is non-empty and the hash is invalid.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for Hash.

func (Hash) Verify

func (h Hash) Verify(buf []byte) error

Verify makes sure that the hash matches the given data and returns an error otherwise.

type HashMismatchError

type HashMismatchError struct {
	ExpectedH Hash
	ActualH   Hash
}

HashMismatchError is returned whenever a hash mismatch is detected.

func (HashMismatchError) Error

func (e HashMismatchError) Error() string

type HashType

type HashType byte

HashType is the type of a keybase hash.

const (
	// InvalidHash is the zero HashType value, which is invalid.
	InvalidHash HashType = 0
	// SHA256Hash is the type of a SHA256 hash.
	SHA256Hash HashType = 1
	// SHA256HashV2 is the type of a SHA256 hash over V2-encrypted data.
	SHA256HashV2 HashType = 2

	// MaxHashType is the highest-supported hash type.
	MaxHashType HashType = SHA256HashV2

	// TemporaryHashType is a hash type to be used for random
	// byte-strings that can be used in place of real hashes.
	TemporaryHashType HashType = 0xff
)
const DefaultHashType HashType = SHA256Hash

DefaultHashType is the current default keybase hash type.

func (HashType) String

func (t HashType) String() string

type InvalidHashError

type InvalidHashError struct {
	H Hash
}

InvalidHashError is returned whenever an invalid hash is detected.

func (InvalidHashError) Error

func (e InvalidHashError) Error() string

type RawDefaultHash

type RawDefaultHash [sha256.Size]byte

RawDefaultHash is the type for the raw bytes of a default keybase hash. This is exposed for use as in-memory keys.

func (*RawDefaultHash) Copy

func (rdh *RawDefaultHash) Copy() *RawDefaultHash

Copy returns a copied RawDefaultHash

type UnknownHashTypeError

type UnknownHashTypeError struct {
	T HashType
}

UnknownHashTypeError is returned whenever a hash with an unknown hash type is attempted to be used for verification.

func (UnknownHashTypeError) Error

func (e UnknownHashTypeError) Error() string

Jump to

Keyboard shortcuts

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