nixhash

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package nixhash provides methods to serialize and deserialize some of the hashes used in nix code and .narinfo files.

Nix uses different representation of hashes depending on the context and history of the project. This package provides the utilities to handle them.

Index

Constants

View Source
const (

	// All the algorithms that Nix understands
	MD5    = Algorithm(iota)
	SHA1   = Algorithm(iota)
	SHA256 = Algorithm(iota)
	SHA512 = Algorithm(iota)
)
View Source
const (

	// All the encodings that Nix understands
	Base16    = Encoding(iota) // Lowercase hexadecimal encoding.
	Base64    = Encoding(iota) // [IETF RFC 4648, section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4).
	NixBase32 = Encoding(iota) // Nix-specific base-32 encoding.
	SRI       = Encoding(iota) // W3C recommendation [Subresource Intergrity](https://www.w3.org/TR/SRI/)
)

Variables

This section is empty.

Functions

func CompressHash

func CompressHash(input []byte, outputSize int) []byte

CompressHash takes an arbitrary long sequence of bytes (usually a hash digest), and returns a sequence of bytes of length newSize. It's calculated by rotating through the bytes in the output buffer (zero-initialized), and XOR'ing with each byte in the passed input It consumes 1 byte at a time, and XOR's it with the current value in the output buffer.

Types

type Algorithm

type Algorithm uint8

Algorithm represent the hashing algorithm used to digest the data.

func ParseAlgorithm

func ParseAlgorithm(s string) (Algorithm, error)

func (Algorithm) Func

func (a Algorithm) Func() crypto.Hash

Func returns the cryptographic hash function for the Algorithm (implementing crypto.Hash) It panics when encountering an invalid Algorithm, as these can only occur by manually filling the struct.

func (Algorithm) String

func (a Algorithm) String() string

type Encoding

type Encoding uint8

Encoding is the string representation of the hashed data.

type Hash

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

func MustNewHash

func MustNewHash(algo Algorithm, digest []byte) *Hash

func MustParseNixBase32

func MustParseNixBase32(s string) *Hash

MustParseNixBase32 returns a new Hash struct, by parsing a hashtype:nixbase32 string, or panics on error.

func NewHash

func NewHash(algo Algorithm, digest []byte) (*Hash, error)

func ParseNixBase32

func ParseNixBase32(s string) (*Hash, error)

ParseNixBase32 returns a new Hash struct, by parsing a hashtype:nixbase32 string, or an error.

func (Hash) Algo

func (h Hash) Algo() Algorithm

func (Hash) Digest

func (h Hash) Digest() []byte

func (Hash) Format

func (h Hash) Format(e Encoding, includeAlgo bool) string

Format converts the hash to a string of the given encoding.

type HashWithEncoding

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

HashWithEncoding stores the original encoding so the user can get error messages with the same encoding.

func MustNewHashWithEncoding

func MustNewHashWithEncoding(algo Algorithm, digest []byte, encoding Encoding, includeAlgo bool) *HashWithEncoding

func NewHashWithEncoding

func NewHashWithEncoding(algo Algorithm, digest []byte, encoding Encoding, includeAlgo bool) (*HashWithEncoding, error)

func ParseAny

func ParseAny(s string, optAlgo *Algorithm) (*HashWithEncoding, error)

Parse the hash from a string representation in the format "[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a Subresource Integrity hash expression). If the 'optAlgo' argument is not present, then the hash algorithm must be specified in the string.

func (HashWithEncoding) String

func (h HashWithEncoding) String() string

String return the previous representation of a given hash.

Jump to

Keyboard shortcuts

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