hash

package
v0.0.0-...-160d89c Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package hash implements the hash function used throughout Noms.

Noms serialization from version 4-onward uses the first 20 bytes of sha-512 for hashes.

sha-512 was chosen because:

- sha-1 is no longer recommended. - sha-3 is brand new, not a lot of platform support. - blake is not commonly used, not a lot of platform support. - within sha-2, sha-512 is faster than sha-256 on 64 bit.

Our specific truncation scheme (first 20 bytes) was chosen because:

- The "standard" truncation schemes are not widely supported. For example, at time of writing, there is no fast native implementation of sha512/256 on Node. - The smallest standard truncation of sha512 is 28 bytes, but we don't need this many. And because we are a database, the size of the hashes matters. Bigger hashes mean less data in each chunk, which means less tree fan-out, which means slower iteration and searching. 20 bytes is a good balance between collision resistance and wide trees. - 20 bytes leads to a nice round number of base32 digits: 32.

The textual serialization of hashes uses big-endian base32 with the alphabet {0-9,a-v}. This scheme was chosen because:

- It's easy to convert to and from base32 without bignum arithemetic. - No special chars: you can double-click to select in GUIs. - Sorted hashes will be sorted textually, making it easy to scan for humans.

In Noms, the hash function is a component of the serialization version, which is constant over the entire lifetime of a single database. So clients do not need to worry about encountering multiple hash functions in the same database.

Index

Constants

View Source
const (
	ByteLen   = 20
	StringLen = 32 // 20 * 8 / log2(32)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Digest

type Digest [ByteLen]byte

type Hash

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

func FromData

func FromData(data []byte) Hash

func FromSlice

func FromSlice(data []byte) Hash

FromSlice creates a new Hash backed by data, ensuring that data is an acceptable length.

func MaybeParse

func MaybeParse(s string) (Hash, bool)

func New

func New(digest Digest) Hash

func Parse

func Parse(s string) Hash

func (Hash) Digest

func (r Hash) Digest() Digest

Digest returns a *copy* of the digest that backs Hash.

func (Hash) DigestSlice

func (r Hash) DigestSlice() []byte

DigestSlice returns a slice of the digest that backs A NEW COPY of Hash, because the receiver of this method is not a pointer.

func (Hash) Greater

func (r Hash) Greater(other Hash) bool

func (Hash) IsEmpty

func (r Hash) IsEmpty() bool

func (Hash) Less

func (r Hash) Less(other Hash) bool

func (Hash) String

func (r Hash) String() string

type HashSet

type HashSet map[Hash]struct{}

func (HashSet) Has

func (hs HashSet) Has(hash Hash) (has bool)

func (HashSet) Insert

func (hs HashSet) Insert(hash Hash)

func (HashSet) Remove

func (hs HashSet) Remove(hash Hash)

type HashSlice

type HashSlice []Hash

func (HashSlice) Equals

func (rs HashSlice) Equals(other HashSlice) bool

func (HashSlice) Len

func (rs HashSlice) Len() int

func (HashSlice) Less

func (rs HashSlice) Less(i, j int) bool

func (HashSlice) Swap

func (rs HashSlice) Swap(i, j int)

Jump to

Keyboard shortcuts

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